diff --git a/src/Assistants/SHA1.cpp b/src/Assistants/SHA1.cpp index 697a437..b55e6ab 100644 --- a/src/Assistants/SHA1.cpp +++ b/src/Assistants/SHA1.cpp @@ -210,6 +210,25 @@ void SHA1::update(std::istream& is) { } } +void SHA1::update(std::span data) { + std::size_t offset{}; + + while (offset < data.size()) { + const auto chunksize{ std::min(BLOCK_BYTES - buffer.size(), data.size() - offset) }; + + buffer.append(data.data() + offset, chunksize); + offset += chunksize; + + // If buffer is full, process the block + if (buffer.size() == BLOCK_BYTES) { + std::uint32_t block[BLOCK_INTS]{}; + buffer_to_block(buffer, block); + transform(digest, block, transforms); + buffer.clear(); + } + } +} + std::string SHA1::final() { // total number of hashed bits const std::uint64_t total_bits{ (transforms * BLOCK_BYTES + buffer.size()) * 8 }; @@ -253,3 +272,9 @@ std::string SHA1::from_file(const std::filesystem::path& filePath) { checksum.update(stream); return checksum.final(); } + +std::string SHA1::from_span(const std::span fileData) { + SHA1 checksum; + checksum.update(fileData); + return checksum.final(); +} diff --git a/src/Assistants/SHA1.hpp b/src/Assistants/SHA1.hpp index 343f8ed..b7182ef 100644 --- a/src/Assistants/SHA1.hpp +++ b/src/Assistants/SHA1.hpp @@ -9,8 +9,10 @@ #pragma once +#include #include #include +#include #include class SHA1 { @@ -22,7 +24,9 @@ class SHA1 { SHA1(); void update(const std::string& s); void update(std::istream& is); + void update(std::span data); std::string final(); static std::string from_file(const std::filesystem::path& filePath); + static std::string from_span(const std::span fileData); };