107 throw std::runtime_error(
"Unable to create EVP context.");
109 if (EVP_DigestInit_ex(mdctx.handle, EVP_sha256(),
nullptr) != 1) {
110 throw std::runtime_error(
"Unable to init SHA256-EVP context.");
116 file.exceptions(ios_base::eofbit | ios_base::failbit | ios_base::badbit);
117 file.open(testFilePath(
path), ios_base::in | ios_base::binary);
119 char readBuffer[4096];
122 file.read(readBuffer,
sizeof(readBuffer));
123 if (EVP_DigestUpdate(mdctx.handle, readBuffer,
static_cast<std::size_t
>(file.gcount())) != 1) {
124 throw std::runtime_error(
"Unable to update SHA256-EVP.");
127 }
catch (
const std::ios_base::failure &) {
128 if (file.eof() && !file.bad()) {
129 if (EVP_DigestUpdate(mdctx.handle, readBuffer,
static_cast<std::size_t
>(file.gcount())) != 1) {
130 throw std::runtime_error(
"Unable to update SHA256-EVP.");
139 unsigned char hash[SHA256_DIGEST_LENGTH];
140 auto length =
static_cast<unsigned int>(SHA256_DIGEST_LENGTH);
141 if (EVP_DigestFinal_ex(mdctx.handle, hash, &length) != 1) {
142 throw std::runtime_error(
"Unable to finalize SHA256-EVP.");
147 char *hexStringIterator = hexString.
checksum;
148 for (
unsigned char hashNumber : hash) {
149 const string digits = numberToString(hashNumber,
static_cast<unsigned char>(16));
150 *(hexStringIterator++) = digits.size() < 2 ?
'0' : digits.front();
151 *(hexStringIterator++) = digits.back();
153 *hexStringIterator =
'\0';