#include "../include/odhtdb/Hash.hpp" #include "../include/odhtdb/bin2hex.hpp" #include #include #include namespace odhtdb { struct SodiumInitializer { SodiumInitializer() { if(sodium_init() < 0) throw std::runtime_error("Failed to initialize libsodium"); } }; static SodiumInitializer __sodiumInitializer; static const Hash EMPTY_HASH; Hash::Hash() { memset(data, 0, HASH_BYTE_SIZE); } Hash::Hash(const void *input, const size_t inputSize) { int result = crypto_generichash_blake2b((unsigned char*)data, HASH_BYTE_SIZE, (const unsigned char*)input, inputSize, nullptr, 0); if(result < 0) throw HashException("Failed to hash data using blake2b"); } Hash::Hash(const Hash &other) { memmove(data, other.data, HASH_BYTE_SIZE); } size_t Hash::operator()() const { return fnvHash((const unsigned char*)data, HASH_BYTE_SIZE); } bool Hash::operator==(const Hash &other) const { return memcmp(data, other.data, HASH_BYTE_SIZE) == 0; } bool Hash::operator!=(const Hash &other) const { return !operator==(other); } std::string Hash::toString() const { return bin2hex(data, HASH_BYTE_SIZE); } bool Hash::isEmpty() const { return *this == EMPTY_HASH; } }