15#include <gnutls/gnutls.h>
16#include <gnutls/crypto.h>
17#if GNUTLS_VERSION_NUMBER >= 0x020a00
18#define HAVE_GNUTLS_HASH_FUNCTIONS
25#ifdef HAVE_GNUTLS_HASH_FUNCTIONS
31#ifndef HAVE_GNUTLS_HASH_FUNCTIONS
40#ifdef HAVE_GNUTLS_HASH_FUNCTIONS
41 gnutls_hash_deinit((gnutls_hash_hd_t)
hash_data,
nullptr);
47#ifndef HAVE_GNUTLS_HASH_FUNCTIONS
48 MFEM_CONTRACT_VAR(buffer);
49 MFEM_CONTRACT_VAR(num_bytes);
51 gnutls_hash((gnutls_hash_hd_t)
hash_data, buffer, num_bytes);
55inline constexpr char to_hex(
unsigned char u)
57 return (
u < 10) ?
'0' +
u : (
u < 16) ?
'a' + (
u - 10) :
'?';
63#ifndef MFEM_USE_GNUTLS
64 hash =
"(GnuTLS is required for hashing)";
65#elif !defined (HAVE_GNUTLS_HASH_FUNCTIONS)
66 hash =
"(Old GnuTLS version: does not support hashing)";
68 constexpr unsigned max_hash_len = 64;
69 unsigned char hash_bytes[max_hash_len];
71 MFEM_VERIFY(hash_len <= max_hash_len,
"internal error");
72 hash.reserve(2*hash_len);
73 gnutls_hash_output((gnutls_hash_hd_t)
hash_data, hash_bytes);
74 for (
unsigned i = 0; i < hash_len; i++)
76 hash +=
to_hex(hash_bytes[i]/16);
77 hash +=
to_hex(hash_bytes[i]%16);
83constexpr static uint64_t rotl64(uint64_t x,
int r)
85 return (x << r) | (x >> (64 - r));
95void Hasher::add_block(uint64_t k1, uint64_t k2)
97 constexpr uint64_t c1 = 0x87c37b91114253d5ull;
98 constexpr uint64_t c2 = 0x4cf5ad432745937full;
107 data[0] =
data[0] * 5 + 0x52dce729ull;
116 data[1] =
data[1] * 5 + 0x38495ab5ull;
119static uint64_t fmix64(uint64_t k)
124 k *= 0xbf58476d1ce4e5b9ull;
126 k *= 0x94d049bb133111ebull;
137 auto rem = nbytes % 16;
139 uint8_t *tmp =
reinterpret_cast<uint8_t *
>(buf_);
142 if (bytes + rem >= 16)
144 std::copy(vs, vs + 16 - rem, tmp + rem);
145 add_block(buf_[0], buf_[1]);
152 std::copy(vs, vs + bytes, tmp + rem);
160 auto rem = nbytes % 16;
189 constexpr uint64_t c1 = 0x87c37b91114253d5ull;
190 constexpr uint64_t c2 = 0x4cf5ad432745937full;
212 constexpr uint64_t c1 = 0x87c37b91114253d5ull;
213 constexpr uint64_t c2 = 0x4cf5ad432745937full;
HashFunction()
Default constructor: initialize the hash function.
std::string GetHash() const
Return the hash string for the current sequence and reset (clear) the sequence.
~HashFunction()
Destructor.
void HashBuffer(const void *buffer, size_t num_bytes)
Add a sequence of bytes for hashing.
real_t u(const Vector &xvec)
constexpr gnutls_digest_algorithm_t HASH_ALGORITHM
constexpr char to_hex(unsigned char u)
void init(uint64_t seed=0)
resets this hasher back to an initial seed
void append(const uint8_t *vs, uint64_t bytes)