aboutsummaryrefslogtreecommitdiff
path: root/include/Hash.hpp
blob: d7c90b0e9691e028198a808ba7f9279058af044f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

#include "utils.hpp"
#include <sodium/crypto_generichash_blake2b.h>
#include <stdexcept>
#include <unordered_map>

namespace odhtdb
{
    const int HASH_BYTE_SIZE = 32;
    
    class HashException : public std::runtime_error
    {
    public:
        HashException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class Hash
    {
    public:
        Hash();
        // Throws HashException on failure
        Hash(const void *input, const size_t inputSize);
        
        void* getData() const { return (void*)data; }
        size_t getSize() const { return HASH_BYTE_SIZE; }
        
        size_t operator()() const;
        bool operator==(const Hash &other) const;
        
        std::string toString() const;
    private:
        char data[HASH_BYTE_SIZE];
    };
    
    struct HashHasher
    {
        size_t operator()(const Hash &hash) const
        {
            return hash();
        }
    };
    
    template <typename ValueType>
    using MapHashKey = std::unordered_map<Hash, ValueType, HashHasher>;
}