From e1c287a45b0fb650092457c216f9dc5c2bfe51f8 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 Jul 2024 16:12:43 +0200 Subject: [PATCH] libluna: Return a new Digest structure instead of a Buffer from SHA256 --- apps/sha256sum.cpp | 4 ++-- libluna/include/luna/SHA.h | 23 +++++++++++++++++++---- libluna/src/SHA.cpp | 24 ++++++++++++------------ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/apps/sha256sum.cpp b/apps/sha256sum.cpp index 682cd786..22b15040 100644 --- a/apps/sha256sum.cpp +++ b/apps/sha256sum.cpp @@ -20,9 +20,9 @@ Result luna_main(int argc, char** argv) SHA256 sha; TRY(sha.append(data.data(), data.size())); - auto hash = TRY(sha.digest()); + auto digest = TRY(sha.digest()); + auto string = TRY(digest.to_string()); - auto string = TRY(sha.hash_to_string(hash)); os::println("%s %s", string.chars(), path.chars()); } diff --git a/libluna/include/luna/SHA.h b/libluna/include/luna/SHA.h index 78fde77d..e3a01fe9 100644 --- a/libluna/include/luna/SHA.h +++ b/libluna/include/luna/SHA.h @@ -9,6 +9,7 @@ #pragma once #include +#include #include /** @@ -26,14 +27,28 @@ class SHA256 */ Result append(const u8* data, usize size); + class Digest + { + public: + const Slice bytes() + { + return { m_bytes, 32 }; + } + + Result to_string(); + + private: + u8 m_bytes[32]; + + friend class SHA256; + }; + /** * @brief Calculate the final hash. * - * @return Buffer The calculated hash. + * @return Digest The calculated hash digest. */ - Result digest(); - - static Result hash_to_string(const Buffer& buffer); + Result digest(); private: Buffer m_message; diff --git a/libluna/src/SHA.cpp b/libluna/src/SHA.cpp index 04bcd443..538c2c09 100644 --- a/libluna/src/SHA.cpp +++ b/libluna/src/SHA.cpp @@ -42,7 +42,7 @@ Result SHA256::append(const u8* data, usize size) // Thank you to https://sha256algorithm.com/ for showing me how the SHA256 algorithm works, and effectively, how to // implement it! -Result SHA256::digest() +Result SHA256::digest() { usize message_block_length = m_message.size() + 1 + sizeof(u64); usize message_block_chunks = ceil_div(message_block_length, 64ul); @@ -160,26 +160,26 @@ Result SHA256::digest() h6 = __builtin_bswap32(h6); h7 = __builtin_bswap32(h7); - Buffer result; - TRY(result.append_data((const u8*)&h0, sizeof(u32))); - TRY(result.append_data((const u8*)&h1, sizeof(u32))); - TRY(result.append_data((const u8*)&h2, sizeof(u32))); - TRY(result.append_data((const u8*)&h3, sizeof(u32))); - TRY(result.append_data((const u8*)&h4, sizeof(u32))); - TRY(result.append_data((const u8*)&h5, sizeof(u32))); - TRY(result.append_data((const u8*)&h6, sizeof(u32))); - TRY(result.append_data((const u8*)&h7, sizeof(u32))); + Digest result; + memcpy(&result.m_bytes[0], &h0, sizeof(u32)); + memcpy(&result.m_bytes[4], &h1, sizeof(u32)); + memcpy(&result.m_bytes[8], &h2, sizeof(u32)); + memcpy(&result.m_bytes[12], &h3, sizeof(u32)); + memcpy(&result.m_bytes[16], &h4, sizeof(u32)); + memcpy(&result.m_bytes[20], &h5, sizeof(u32)); + memcpy(&result.m_bytes[24], &h6, sizeof(u32)); + memcpy(&result.m_bytes[28], &h7, sizeof(u32)); m_message.try_resize(0); // Don't need this anymore. return result; } -Result SHA256::hash_to_string(const Buffer& buffer) +Result SHA256::Digest::to_string() { StringBuilder sb; - for (usize i = 0; i < buffer.size(); i++) { sb.format("%02x", buffer.data()[i]); } + for (usize i = 0; i < sizeof(m_bytes); i++) { sb.format("%02x", m_bytes[i]); } return sb.string(); }