diff options
| author | LillyJadeKatrin <lilly.kitty.1988@gmail.com> | 2024-06-26 20:05:54 -0400 |
|---|---|---|
| committer | Admiral H. Curtiss <pikachu025@gmail.com> | 2024-07-04 22:12:20 +0200 |
| commit | 360f899f68a97f4bae5eaf898dc32dcd063cdf1d (patch) | |
| tree | 771844af6ee917106b5d9b724b88321bd8df557a /Source/Core | |
| parent | bb4e8d0d01c0e980769ea94b4dd9097adef1b640 (diff) | |
Common/Crypto/SHA1: Add DigestToString() utility function
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Common/Crypto/SHA1.cpp | 16 | ||||
| -rw-r--r-- | Source/Core/Common/Crypto/SHA1.h | 2 |
2 files changed, 18 insertions, 0 deletions
diff --git a/Source/Core/Common/Crypto/SHA1.cpp b/Source/Core/Common/Crypto/SHA1.cpp index f87bbd2c6d..8c4aa646bd 100644 --- a/Source/Core/Common/Crypto/SHA1.cpp +++ b/Source/Core/Common/Crypto/SHA1.cpp @@ -385,4 +385,20 @@ Digest CalculateDigest(const u8* msg, size_t len) ctx->Update(msg, len); return ctx->Finish(); } + +std::string DigestToString(const Digest& digest) +{ + static constexpr std::array<char, 16> lookup = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + std::string hash; + hash.reserve(digest.size() * 2); + for (size_t i = 0; i < digest.size(); ++i) + { + const u8 upper = static_cast<u8>((digest[i] >> 4) & 0xf); + const u8 lower = static_cast<u8>(digest[i] & 0xf); + hash.push_back(lookup[upper]); + hash.push_back(lookup[lower]); + } + return hash; +} } // namespace Common::SHA1 diff --git a/Source/Core/Common/Crypto/SHA1.h b/Source/Core/Common/Crypto/SHA1.h index 83c9875a71..b828c28f15 100644 --- a/Source/Core/Common/Crypto/SHA1.h +++ b/Source/Core/Common/Crypto/SHA1.h @@ -51,4 +51,6 @@ inline Digest CalculateDigest(const std::array<T, Size>& msg) static_assert(std::is_trivially_copyable_v<T>); return CalculateDigest(reinterpret_cast<const u8*>(msg.data()), sizeof(msg)); } + +std::string DigestToString(const Digest& digest); } // namespace Common::SHA1 |
