}
Hash&
-Hash::hash(nonstd::span<const uint8_t> data, HashType hash_type)
+Hash::hash(nonstd::span<const uint8_t> data)
{
hash_buffer(data);
-
- switch (hash_type) {
- case HashType::binary:
- add_debug_text(util::format_base16(data));
- break;
-
- case HashType::text:
- add_debug_text(util::to_string_view(data));
- break;
- }
-
+ add_debug_text(data);
add_debug_text("\n");
return *this;
}
Hash&
Hash::hash(const char* data, size_t size)
{
- hash(util::to_span({data, size}), HashType::text);
+ hash(util::to_span({data, size}));
return *this;
}
Hash&
Hash::hash(std::string_view data)
{
- hash(util::to_span(data), HashType::text);
+ hash(util::to_span(data));
return *this;
}
}
void
-Hash::add_debug_text(std::string_view text)
+Hash::add_debug_text(nonstd::span<const uint8_t> text)
{
if (!text.empty() && m_debug_text) {
- (void)fwrite(text.data(), 1, text.length(), m_debug_text);
+ (void)fwrite(text.data(), 1, text.size(), m_debug_text);
}
}
+
+void
+Hash::add_debug_text(std::string_view text)
+{
+ add_debug_text(util::to_span(text));
+}
class Hash
{
public:
- enum class HashType { binary, text };
-
using Digest = std::array<uint8_t, 20>;
Hash();
// set, there should never be a hash collision risk).
Hash& hash_delimiter(std::string_view type);
- // Add bytes to the hash.
- //
- // If hash debugging is enabled:
- //
- // - If `hash_type` is `HashType::binary`, the buffer content is written in
- // hex format to the text input file.
- // - If `hash_type` is `HashType::text`, the buffer content is written
- // verbatim to the text input file.
- //
- // In both cases a newline character is added as well.
- Hash& hash(nonstd::span<const uint8_t> data,
- HashType hash_type = HashType::text);
-
- // Add string data to the hash.
+ // Add data to the hash.
//
- // If hash debugging is enabled, the string is written to the text input file
- // followed by a newline.
+ // If hash debugging is enabled the bytes will be written verbatim to the text
+ // input file, plus a final newline character.
+ Hash& hash(nonstd::span<const uint8_t> data);
Hash& hash(const char* data, size_t size);
-
- // Add string data to the hash.
- //
- // If hash debugging is enabled, the string is written to the text input file
- // followed by a newline.
Hash& hash(std::string_view data);
// Add an integer to the hash.
// input file.
nonstd::expected<void, std::string> hash_fd(int fd);
- // Add `text` to the text debug file.
- void add_debug_text(std::string_view text);
-
private:
blake3_hasher m_hasher;
FILE* m_debug_binary = nullptr;
void hash_buffer(nonstd::span<const uint8_t> buffer);
void hash_buffer(std::string_view buffer);
+
+ void add_debug_text(nonstd::span<const uint8_t> text);
+ void add_debug_text(std::string_view text);
};