MFEM  v4.4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #include "hash.hpp"
13 
14 #ifdef MFEM_USE_GNUTLS
15 #include <gnutls/gnutls.h>
16 #include <gnutls/crypto.h>
17 #if GNUTLS_VERSION_NUMBER >= 0x020a00
18 #define HAVE_GNUTLS_HASH_FUNCTIONS
19 #endif
20 #endif
21 
22 namespace mfem
23 {
24 
25 #ifdef HAVE_GNUTLS_HASH_FUNCTIONS
26 constexpr gnutls_digest_algorithm_t HASH_ALGORITHM = GNUTLS_DIG_SHA256;
27 #endif
28 
30 {
31 #ifndef HAVE_GNUTLS_HASH_FUNCTIONS
32  hash_data = nullptr;
33 #else
34  gnutls_hash_init((gnutls_hash_hd_t*)(&hash_data), HASH_ALGORITHM);
35 #endif
36 }
37 
39 {
40 #ifdef HAVE_GNUTLS_HASH_FUNCTIONS
41  gnutls_hash_deinit((gnutls_hash_hd_t)hash_data, nullptr);
42 #endif
43 }
44 
45 void HashFunction::HashBuffer(const void *buffer, size_t num_bytes)
46 {
47 #ifndef HAVE_GNUTLS_HASH_FUNCTIONS
48  MFEM_CONTRACT_VAR(buffer);
49  MFEM_CONTRACT_VAR(num_bytes);
50 #else
51  gnutls_hash((gnutls_hash_hd_t)hash_data, buffer, num_bytes);
52 #endif
53 }
54 
55 inline constexpr char to_hex(unsigned char u)
56 {
57  return (u < 10) ? '0' + u : (u < 16) ? 'a' + (u - 10) : '?';
58 }
59 
60 std::string HashFunction::GetHash() const
61 {
62  std::string hash;
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)";
67 #else
68  constexpr unsigned max_hash_len = 64;
69  unsigned char hash_bytes[max_hash_len];
70  unsigned hash_len = gnutls_hash_get_len(HASH_ALGORITHM);
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++)
75  {
76  hash += to_hex(hash_bytes[i]/16);
77  hash += to_hex(hash_bytes[i]%16);
78  }
79 #endif
80  return hash;
81 }
82 
83 } // namespace mfem
void * hash_data
Definition: hash.hpp:458
void HashBuffer(const void *buffer, size_t num_bytes)
Add a sequence of bytes for hashing.
Definition: hash.cpp:45
std::string GetHash() const
Return the hash string for the current sequence and reset (clear) the sequence.
Definition: hash.cpp:60
constexpr char to_hex(unsigned char u)
Definition: hash.cpp:55
HashFunction()
Default constructor: initialize the hash function.
Definition: hash.cpp:29
constexpr gnutls_digest_algorithm_t HASH_ALGORITHM
Definition: hash.cpp:26
~HashFunction()
Destructor.
Definition: hash.cpp:38
double u(const Vector &xvec)
Definition: lor_mms.hpp:24