Stop using deprecated SHA256 functions

This commit is contained in:
Bartosz Wieczorek 2024-09-03 21:01:14 +02:00 committed by unknown
parent a5d33d00f7
commit 1b202b68e7

View File

@ -8,18 +8,39 @@
namespace rublon {
inline StaticString< SHA256_DIGEST_LENGTH * 2> SHA256(const char * const path) {
inline StaticString< SHA256_DIGEST_LENGTH * 2 > fileSHA256(const char * const path) {
std::string fileContent;
readFile(path, fileContent);
StaticString< SHA256_DIGEST_LENGTH * 2 > xRublon{};
std::array< unsigned char, SHA256_DIGEST_LENGTH + 1 > hash{};
int ret{};
SHA256_CTX ctx;
SHA256_Init(&ctx);
EVP_MD_CTX * ctx;
ctx = EVP_MD_CTX_new();
SHA256_Update(&ctx, fileContent.data(), fileContent.size());
SHA256_Final(hash.data(), &ctx);
return 0;
if(ctx == NULL)
goto out;
// EVP_X methods return 1 on success, so does this function
// Any values other than 1 denote error
ret = EVP_DigestInit(ctx, EVP_sha256());
if(!ret)
goto out;
ret = EVP_DigestUpdate(ctx, fileContent.data(), fileContent.size());
if(!ret)
goto out;
// Provide uint* instead of NULL to get nBytes written, 32 for SHA256
ret = EVP_DigestFinal(ctx, hash.data(), NULL);
if(!ret)
goto out;
out:
if(ctx != NULL)
EVP_MD_CTX_free(ctx);
for(unsigned int i = 0; i < SHA256_DIGEST_LENGTH; i++)
sprintf(&xRublon[i * 2], "%02x", ( unsigned int ) hash[i]);
@ -28,8 +49,8 @@ inline StaticString< SHA256_DIGEST_LENGTH * 2> SHA256(const char * const path) {
}
// +1 for \0
inline StaticString< 64> signData(std::string_view data, std::string_view secretKey) {
StaticString< 64 > xRublon;
inline StaticString< SHA256_DIGEST_LENGTH * 2 > signData(std::string_view data, std::string_view secretKey) {
StaticString< SHA256_DIGEST_LENGTH * 2 > xRublon;
std::array< unsigned char, EVP_MAX_MD_SIZE > md;
unsigned int md_len{};