libluna: Return a new Digest structure instead of a Buffer from SHA256
All checks were successful
Build and test / build (push) Successful in 2m30s

This commit is contained in:
apio 2024-07-20 16:12:43 +02:00
parent db2f91b1fb
commit e1c287a45b
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 33 additions and 18 deletions

View File

@ -20,9 +20,9 @@ Result<int> luna_main(int argc, char** argv)
SHA256 sha; SHA256 sha;
TRY(sha.append(data.data(), data.size())); 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()); os::println("%s %s", string.chars(), path.chars());
} }

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <luna/Buffer.h> #include <luna/Buffer.h>
#include <luna/Slice.h>
#include <luna/String.h> #include <luna/String.h>
/** /**
@ -26,14 +27,28 @@ class SHA256
*/ */
Result<void> append(const u8* data, usize size); Result<void> append(const u8* data, usize size);
class Digest
{
public:
const Slice<u8> bytes()
{
return { m_bytes, 32 };
}
Result<String> to_string();
private:
u8 m_bytes[32];
friend class SHA256;
};
/** /**
* @brief Calculate the final hash. * @brief Calculate the final hash.
* *
* @return Buffer The calculated hash. * @return Digest The calculated hash digest.
*/ */
Result<Buffer> digest(); Result<Digest> digest();
static Result<String> hash_to_string(const Buffer& buffer);
private: private:
Buffer m_message; Buffer m_message;

View File

@ -42,7 +42,7 @@ Result<void> 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 // Thank you to https://sha256algorithm.com/ for showing me how the SHA256 algorithm works, and effectively, how to
// implement it! // implement it!
Result<Buffer> SHA256::digest() Result<SHA256::Digest> SHA256::digest()
{ {
usize message_block_length = m_message.size() + 1 + sizeof(u64); usize message_block_length = m_message.size() + 1 + sizeof(u64);
usize message_block_chunks = ceil_div(message_block_length, 64ul); usize message_block_chunks = ceil_div(message_block_length, 64ul);
@ -160,26 +160,26 @@ Result<Buffer> SHA256::digest()
h6 = __builtin_bswap32(h6); h6 = __builtin_bswap32(h6);
h7 = __builtin_bswap32(h7); h7 = __builtin_bswap32(h7);
Buffer result; Digest result;
TRY(result.append_data((const u8*)&h0, sizeof(u32))); memcpy(&result.m_bytes[0], &h0, sizeof(u32));
TRY(result.append_data((const u8*)&h1, sizeof(u32))); memcpy(&result.m_bytes[4], &h1, sizeof(u32));
TRY(result.append_data((const u8*)&h2, sizeof(u32))); memcpy(&result.m_bytes[8], &h2, sizeof(u32));
TRY(result.append_data((const u8*)&h3, sizeof(u32))); memcpy(&result.m_bytes[12], &h3, sizeof(u32));
TRY(result.append_data((const u8*)&h4, sizeof(u32))); memcpy(&result.m_bytes[16], &h4, sizeof(u32));
TRY(result.append_data((const u8*)&h5, sizeof(u32))); memcpy(&result.m_bytes[20], &h5, sizeof(u32));
TRY(result.append_data((const u8*)&h6, sizeof(u32))); memcpy(&result.m_bytes[24], &h6, sizeof(u32));
TRY(result.append_data((const u8*)&h7, sizeof(u32))); memcpy(&result.m_bytes[28], &h7, sizeof(u32));
m_message.try_resize(0); // Don't need this anymore. m_message.try_resize(0); // Don't need this anymore.
return result; return result;
} }
Result<String> SHA256::hash_to_string(const Buffer& buffer) Result<String> SHA256::Digest::to_string()
{ {
StringBuilder sb; 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(); return sb.string();
} }