diff options
Diffstat (limited to 'src/lib/util/hashing.h')
-rw-r--r-- | src/lib/util/hashing.h | 64 |
1 files changed, 60 insertions, 4 deletions
diff --git a/src/lib/util/hashing.h b/src/lib/util/hashing.h index 11bf7fd655e..f2f06af686a 100644 --- a/src/lib/util/hashing.h +++ b/src/lib/util/hashing.h @@ -19,6 +19,7 @@ #include <array> #include <functional> #include <string> +#include <string_view> namespace util { @@ -36,7 +37,7 @@ struct sha1_t bool operator==(const sha1_t &rhs) const { return memcmp(m_raw, rhs.m_raw, sizeof(m_raw)) == 0; } bool operator!=(const sha1_t &rhs) const { return memcmp(m_raw, rhs.m_raw, sizeof(m_raw)) != 0; } operator uint8_t *() { return m_raw; } - bool from_string(const char *string, int length = -1); + bool from_string(std::string_view string); std::string as_string() const; uint8_t m_raw[20]; static const sha1_t null; @@ -82,7 +83,7 @@ struct md5_t bool operator==(const md5_t &rhs) const { return memcmp(m_raw, rhs.m_raw, sizeof(m_raw)) == 0; } bool operator!=(const md5_t &rhs) const { return memcmp(m_raw, rhs.m_raw, sizeof(m_raw)) != 0; } operator uint8_t *() { return m_raw; } - bool from_string(const char *string, int length = -1); + bool from_string(std::string_view string); std::string as_string() const; uint8_t m_raw[16]; static const md5_t null; @@ -141,7 +142,7 @@ struct crc32_t constexpr operator uint32_t() const { return m_raw; } - bool from_string(const char *string, int length = -1); + bool from_string(std::string_view string); std::string as_string() const; uint32_t m_raw; @@ -197,7 +198,7 @@ struct crc16_t constexpr operator uint16_t() const { return m_raw; } - bool from_string(const char *string, int length = -1); + bool from_string(std::string_view string); std::string as_string() const; uint16_t m_raw; @@ -235,6 +236,61 @@ protected: }; + +// ======================> SUM-16 + +// final digest +struct sum16_t +{ + sum16_t() { } + constexpr sum16_t(const sum16_t &rhs) = default; + constexpr sum16_t(const uint16_t sum) : m_raw(sum) { } + + constexpr bool operator==(const sum16_t &rhs) const { return m_raw == rhs.m_raw; } + constexpr bool operator!=(const sum16_t &rhs) const { return m_raw != rhs.m_raw; } + + sum16_t &operator=(const sum16_t &rhs) = default; + sum16_t &operator=(const uint16_t sum) { m_raw = sum; return *this; } + + constexpr operator uint16_t() const { return m_raw; } + + bool from_string(std::string_view string); + std::string as_string() const; + + uint16_t m_raw; + + static const sum16_t null; +}; + +// creation helper +class sum16_creator +{ +public: + // construction/destruction + sum16_creator() { reset(); } + + // reset + void reset() { m_accum.m_raw = 0; } + + // append data + void append(const void *data, uint32_t length); + + // finalize and compute the final digest + sum16_t finish() { return m_accum; } + + // static wrapper to just get the digest from a block + static sum16_t simple(const void *data, uint32_t length) + { + sum16_creator creator; + creator.append(data, length); + return creator.finish(); + } + +protected: + // internal state + sum16_t m_accum; // internal accumulator +}; + } // namespace util namespace std { |