OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
hashmap.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_COMMON_HASHMAP_H_
7#define OPENCBDC_TX_SRC_COMMON_HASHMAP_H_
8
9#include "buffer.hpp"
10#include "crypto/siphash.h"
11#include "hash.hpp"
12
13#include <array>
14#include <cstring>
15
16namespace cbdc::hashing {
26 template<class T>
28 static_assert(std::is_trivial_v<T>);
29 auto operator()(T const& tx) const noexcept -> size_t {
30 // TODO: pick the initialization key at random.
31 static constexpr std::array<uint64_t, 2> siphash_key{0x1337,
32 0x1337};
33 CSipHasher hasher(siphash_key[0], siphash_key[1]);
34 std::array<unsigned char, sizeof(tx)> tx_arr{};
35 std::memcpy(tx_arr.data(), &tx, sizeof(tx));
36 hasher.Write(tx_arr.data(), sizeof(tx));
37 return hasher.Finalize();
38 }
39 };
40
41 template<>
43 auto operator()(const buffer& buf) const noexcept -> size_t {
44 // TODO: pick the initialization key at random.
45 static constexpr std::array<uint64_t, 2> siphash_key{0x1337,
46 0x1337};
47 CSipHasher hasher(siphash_key[0], siphash_key[1]);
48 std::vector<unsigned char> arr(buf.size());
49 std::memcpy(arr.data(), buf.data(), buf.size());
50 hasher.Write(arr.data(), arr.size());
51 return hasher.Finalize();
52 }
53 };
54
61 struct null {
62 auto operator()(const hash_t& hash) const noexcept -> size_t;
63 };
64}
65
66#endif // OPENCBDC_TX_SRC_COMMON_HASHMAP_H_
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
auto operator()(const buffer &buf) const noexcept -> size_t
Definition hashmap.hpp:43
SipHash function to generate STL data structure hash keys for system IDs.
Definition hashmap.hpp:27
auto operator()(T const &tx) const noexcept -> size_t
Definition hashmap.hpp:29
Uses the raw value of a provided hash as an STL data structure hash key.
Definition hashmap.hpp:61
auto operator()(const hash_t &hash) const noexcept -> size_t
Definition hashmap.cpp:9