libluna: Return a new Digest structure instead of a Buffer from SHA256
All checks were successful
Build and test / build (push) Successful in 2m30s
All checks were successful
Build and test / build (push) Successful in 2m30s
This commit is contained in:
parent
db2f91b1fb
commit
e1c287a45b
@ -20,9 +20,9 @@ Result<int> 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());
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <luna/Buffer.h>
|
||||
#include <luna/Slice.h>
|
||||
#include <luna/String.h>
|
||||
|
||||
/**
|
||||
@ -26,14 +27,28 @@ class SHA256
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @return Buffer The calculated hash.
|
||||
* @return Digest The calculated hash digest.
|
||||
*/
|
||||
Result<Buffer> digest();
|
||||
|
||||
static Result<String> hash_to_string(const Buffer& buffer);
|
||||
Result<Digest> digest();
|
||||
|
||||
private:
|
||||
Buffer m_message;
|
||||
|
@ -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
|
||||
// implement it!
|
||||
Result<Buffer> SHA256::digest()
|
||||
Result<SHA256::Digest> 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<Buffer> 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<String> SHA256::hash_to_string(const Buffer& buffer)
|
||||
Result<String> 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user