Belle II Software  release-08-01-10
Hash.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/utilities/sha3hash/Hash.h>
10 #include <framework/logging/Logger.h>
11 
12 extern "C" {
13 #include "KeccakHash.h"
14 }
17 struct KeccakHashInstance: public Keccak_HashInstance {};
18 
19 namespace Belle2 {
25  SHA3Hash::SHA3Hash(EHashMode mode): m_mode(mode)
26  {
28  clear();
29  }
30 
32  {
33  delete m_instance;
34  }
35 
37  {
38  //Init the hash structure
39  switch (m_mode) {
40  case c_SHA3_224:
41  Keccak_HashInitialize_SHA3_224(m_instance);
42  break;
43  case c_SHA3_256:
44  Keccak_HashInitialize_SHA3_256(m_instance);
45  break;
46  case c_SHA3_384:
47  Keccak_HashInitialize_SHA3_384(m_instance);
48  break;
49  case c_SHA3_512:
50  Keccak_HashInitialize_SHA3_512(m_instance);
51  break;
52  default:
53  B2FATAL("Unknown mode for SHA3Hash");
54  }
55  }
56 
57  void SHA3Hash::update(int n, unsigned char* buff)
58  {
59  Keccak_HashUpdate(m_instance, buff, n * 8);
60  }
61 
62  void SHA3Hash::getHash(unsigned char* buff)
63  {
64  Keccak_HashFinal(m_instance, buff);
65  }
66 
67  std::vector<unsigned char> SHA3Hash::getHash()
68  {
69  std::vector<unsigned char> buff(m_mode);
70  getHash(buff.data());
71  return buff;
72  }
73 
74  ShakeHash::ShakeHash(EHashMode mode): m_mode(mode)
75  {
77  clear();
78  }
79 
81  {
82  delete m_instance;
83  }
84 
86  {
87  //Init the hash structure
88  switch (m_mode) {
89  case c_SHAKE128:
90  Keccak_HashInitialize_SHAKE128(m_instance);
91  break;
92  case c_SHAKE256:
93  Keccak_HashInitialize_SHAKE256(m_instance);
94  break;
95  default:
96  B2FATAL("Unknown mode for ShakeHash");
97  }
98  }
99 
100  void ShakeHash::update(int n, unsigned char* buff)
101  {
102  Keccak_HashUpdate(m_instance, buff, n * 8);
103  }
104 
105  void ShakeHash::getHash(int n, unsigned char* buff)
106  {
107  Keccak_HashSqueeze(m_instance, buff, n * 8);
108  }
109 
111 } //Belle2 namespace
EHashMode m_mode
chosen hash mode
Definition: Hash.h:53
KeccakHashInstance * m_instance
memory structure to calculate the hash value
Definition: Hash.h:51
EHashMode
Available hash modes according to FIPS 202 draft.
Definition: Hash.h:26
@ c_SHA3_512
384bit output size
Definition: Hash.h:30
@ c_SHA3_384
256bit output size
Definition: Hash.h:29
@ c_SHA3_256
224bit output size
Definition: Hash.h:28
EHashMode m_mode
chosen hash mode
Definition: Hash.h:81
KeccakHashInstance * m_instance
memory structure to calculate the hash value
Definition: Hash.h:79
EHashMode
Available hash modes according to FIPS 202 draft.
Definition: Hash.h:61
@ c_SHAKE128
variable hash size with up to 128 bit collision resistance
Definition: Hash.h:62
@ c_SHAKE256
variable hash size with up to 256 bit collision resistance
Definition: Hash.h:63
~SHA3Hash()
destructor freeing the memory
Definition: Hash.cc:31
void update(int n, unsigned char *buff)
update the internal state by adding n bytes of data from buff
Definition: Hash.cc:57
void getHash(int n, unsigned char *buff)
obtain the hash value with a length of n bytes into buff
Definition: Hash.cc:105
SHA3Hash(EHashMode length)
Constructor initializing the hash structure with a given output size.
Definition: Hash.cc:25
~ShakeHash()
destructor freeing the memory
Definition: Hash.cc:80
void clear()
reinit the hash structure to create a new hash sum
Definition: Hash.cc:36
std::vector< unsigned char > getHash()
obtain the hash value as a vector of unsigned char
Definition: Hash.cc:67
ShakeHash(EHashMode mode)
constructor initializing the hash structure
Definition: Hash.cc:74
Abstract base class for different kinds of events.
Since we cannot forward declare the memory structure directly due to some typedeffing we inherit from...
Definition: Hash.cc:17