From 9ab4547491e1bc0ba0564581a9390ed63b09348e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=98=95=EC=A3=BC/MDE=20Lab=28SR=29/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 24 Jul 2023 13:31:33 +0900 Subject: [PATCH] Fix deprecated SHA* to EVP_MD* (#473) * Fix deprecated SHA* to EVP_MD* --- NativeLauncher/util/utils.cc | 63 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index 963cb7f..9f34fb4 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -23,7 +23,10 @@ #include #include #include -#include + +#include +#include + #include #include @@ -707,36 +710,68 @@ std::string getFileName(const std::string& path) std::string SHA256(const std::string& path) { + int bytesRead = 0; + const int bufSize = 32768; + + unsigned int digest_len = 0; + unsigned char* digest = NULL; + + std::stringstream ss; + EVP_MD_CTX *mdctx = NULL; std::string output = ""; + FILE *file = fopen(path.c_str(), "rb"); if (!file) { return output; } - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha256; - SHA256_Init(&sha256); - int bytesRead = 0; - const int bufSize = 32768; char *buffer = (char*)malloc(bufSize); if (!buffer) { - fclose(file); - return output; + goto cleanup4; + } + + mdctx = EVP_MD_CTX_new(); + if (mdctx == NULL) { + _ERR("Message Digest Context creation NULL"); + goto cleanup3; + } + + digest = (unsigned char*)OPENSSL_malloc(EVP_MD_size(EVP_sha256())); + if (!digest) { + _ERR("Memory Allocation for SHA256 failed"); + goto cleanup2; + } + + if (!EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) { + _ERR("Message Digest init failed"); + goto cleanup1; } while ((bytesRead = fread(buffer, 1, bufSize, file))) { - SHA256_Update(&sha256, buffer, bytesRead); + if (!EVP_DigestUpdate(mdctx, buffer, bytesRead)) { + _ERR("Message Digest update failed"); + goto cleanup1; + } } - SHA256_Final(hash, &sha256); - std::stringstream ss; - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; + if (!EVP_DigestFinal_ex(mdctx, digest, &digest_len)) { + _ERR("Message Digest Finalization falied"); + goto cleanup1; + } + + for (unsigned int i = 0; i < digest_len; i++) { + ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i]; } output = ss.str(); - fclose(file); +cleanup1: + EVP_MD_CTX_free(mdctx); +cleanup2: + OPENSSL_free(digest); +cleanup3: free(buffer); +cleanup4: + fclose(file); return output; } -- 2.7.4