From 228e3059c95f3638f335de263e83d4edbac0df2e Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Sun, 16 Jul 2023 22:02:13 +0200 Subject: [PATCH] Core/Crypto: moved away from deprecated SHA1 methods in CalculateSHA1Hash --- src/common/Cryptography/SHA1.cpp | 14 ++++++++++---- src/common/Cryptography/SHA1.h | 16 ++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/common/Cryptography/SHA1.cpp b/src/common/Cryptography/SHA1.cpp index 81992ca029..a9eab8ed0a 100644 --- a/src/common/Cryptography/SHA1.cpp +++ b/src/common/Cryptography/SHA1.cpp @@ -20,8 +20,9 @@ #include "Util.h" #include #include +#include -SHA1Hash::SHA1Hash() +SHA1Hash::SHA1Hash() noexcept { m_ctx = EVP_MD_CTX_new(); EVP_DigestInit_ex(m_ctx, EVP_sha1(), nullptr); @@ -33,7 +34,7 @@ SHA1Hash::SHA1Hash(const SHA1Hash &other) : SHA1Hash() // copy std::memcpy(m_digest, other.m_digest, SHA_DIGEST_LENGTH); } -SHA1Hash::SHA1Hash(SHA1Hash &&other) : SHA1Hash() // move +SHA1Hash::SHA1Hash(SHA1Hash &&other) noexcept : SHA1Hash() // move { Swap(other); } @@ -93,8 +94,13 @@ void SHA1Hash::Finalize(void) std::string CalculateSHA1Hash(std::string const& content) { - unsigned char digest[SHA_DIGEST_LENGTH]; - SHA1((unsigned char*)content.c_str(), content.length(), (unsigned char*)&digest); + EVP_MD_CTX* mdctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(mdctx, EVP_sha1(), nullptr); + EVP_DigestUpdate(mdctx, content.c_str(), content.size()); + uint8 digest[SHA_DIGEST_LENGTH]; + uint32 shaDigestLength = SHA_DIGEST_LENGTH; + EVP_DigestFinal_ex(mdctx, digest, &shaDigestLength); + EVP_MD_CTX_free(mdctx); return ByteArrayToHexStr(digest, SHA_DIGEST_LENGTH); } diff --git a/src/common/Cryptography/SHA1.h b/src/common/Cryptography/SHA1.h index db003978d5..e6ccf88cbe 100644 --- a/src/common/Cryptography/SHA1.h +++ b/src/common/Cryptography/SHA1.h @@ -28,19 +28,19 @@ class BigNumber; class TC_COMMON_API SHA1Hash { public: - SHA1Hash(); - SHA1Hash(SHA1Hash const &other); // copy - SHA1Hash(SHA1Hash &&other); // move + SHA1Hash() noexcept; + SHA1Hash(SHA1Hash const& other); // copy + SHA1Hash(SHA1Hash&& other) noexcept; // move SHA1Hash &operator=(SHA1Hash other); // assign ~SHA1Hash(); - void Swap(SHA1Hash &other) throw(); - friend void Swap(SHA1Hash &left, SHA1Hash &right) { left.Swap(right); } + void Swap(SHA1Hash& other) throw(); + friend void Swap(SHA1Hash& left, SHA1Hash &right) { left.Swap(right); } void UpdateBigNumbers(BigNumber* bn0, ...); - void UpdateData(const uint8 *dta, int len); - void UpdateData(const std::string &str); + void UpdateData(const uint8* dta, int len); + void UpdateData(const std::string& str); void Initialize(); void Finalize(); @@ -49,7 +49,7 @@ class TC_COMMON_API SHA1Hash int GetLength() const { return SHA_DIGEST_LENGTH; } private: - EVP_MD_CTX *m_ctx; + EVP_MD_CTX* m_ctx; uint8 m_digest[SHA_DIGEST_LENGTH]; };