openssl: stop using deprecated SHA512_* functions
The openssl/sha.h header is deprecated for OpenSSL 3 along with all the
functions in it. Switch to use the EVP_MD interfaces.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I17f9cd3652d92caab1baf29829e8e0cd6f781142
diff --git a/src/utils.cpp b/src/utils.cpp
index 5889b6b..d4b5291 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -2,7 +2,7 @@
#include "utils.hpp"
-#include <openssl/sha.h>
+#include <openssl/evp.h>
#include <algorithm>
#include <fstream>
@@ -129,20 +129,23 @@
return {};
}
- unsigned char digest[SHA512_DIGEST_LENGTH];
- SHA512_CTX ctx;
- SHA512_Init(&ctx);
- SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
- SHA512_Final(digest, &ctx);
- char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
- for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
- {
- snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
- }
+ using EVP_MD_CTX_Ptr =
+ std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
+
+ std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
+ EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free);
+
+ EVP_DigestInit(ctx.get(), EVP_sha512());
+ EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str()));
+ EVP_DigestFinal(ctx.get(), digest.data(), nullptr);
// Only need 8 hex digits.
- std::string hexId = std::string(mdString);
- return (hexId.substr(0, 8));
+ char mdString[9];
+ snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x",
+ (unsigned int)digest[0], (unsigned int)digest[1],
+ (unsigned int)digest[2], (unsigned int)digest[3]);
+
+ return mdString;
}
std::string Utils::getVersion(const std::string& inventoryPath) const