Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cHashSHA512.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_cHashSHA512_H
7 #define _INC_cHashSHA512_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 #include "cHashBase.h"
12 #include "cHashTypeDef.h"
13 
14 namespace GrayLib
15 {
17 
18  class GRAYLIB_LINK cHashSHA512 : public cHashCodeT<64>
19  {
23 
24  friend class cHashSHA512Builder;
25  public:
26  cHashSHA512() noexcept
27  {}
28  cHashSHA512(const char* pszHexDigest)
29  {
30  put_HexDigest(pszHexDigest);
31  }
32 
33  static size_t GRAYCALL ComputeHash(BYTE pOutput[k_Size], const void* pInput, size_t nInputSize, bool is384);
34  static size_t GRAYCALL ComputeHmac(BYTE pOutput[k_Size], const BYTE* pKey, size_t nKeySize, const void* pInput, size_t nInputSize,bool is384);
35 
37  };
38 
39  class GRAYLIB_LINK cHashSHA384 : public cHashCodeT<48>
40  {
44 
45  friend class cHashSHA512Builder;
46  public:
48  {}
49  cHashSHA384(const char* pszHexDigest)
50  {
51  put_HexDigest(pszHexDigest);
52  }
53  };
54 
56  {
61 
62  typedef cHashBase SUPER_t;
63 
64  public:
65  static const size_t k_HashSize = 64;
66  static const UINT64 k_K[80];
67 
68  UINT64 m_total[2];
69  UINT64 m_state[8];
70  BYTE m_buffer[128];
71 
72  BYTE m_ipad[128];
73  BYTE m_opad[128];
74 
75  bool m_is384;
76 
77  public:
78  cHashSHA512Builder(bool is384=false)
79  : m_is384(is384)
80  {
81  SetZeroHash();
82  }
84  {
85  cMem::ZeroSecure(m_total, sizeof(m_total));
86  cMem::ZeroSecure(m_state, sizeof(m_state));
87  cMem::ZeroSecure(m_buffer, sizeof(m_buffer));
88  }
89 
90  void SetZeroHash()
91  {
92  cMem::Zero(m_buffer, sizeof(m_buffer));
93  cMem::Zero(m_ipad, sizeof(m_ipad));
94  cMem::Zero(m_opad, sizeof(m_opad));
95  ResetHash();
96  }
97 
98  virtual void ResetHash() override;
99  virtual void ProcessHashBuffer(const BYTE pBuffer[128]) override;
100  virtual void AddToHash(const void* pInput, size_t nSizeIn) override;
101  virtual size_t FinalizeHash(BYTE* pOutput) override;
102 
103  virtual void InitHmac(const BYTE* pKey, size_t nKeySize) override;
104  virtual size_t FinalizeHmac(BYTE pOutput[64]) override;
105  virtual void ResetHmac() override;
106  };
107 
109  {
112  public:
113  static const size_t k_HashSize = 48;
114  public:
116  {
117  }
118  };
119 
121  {
123  public:
125  : cHashTypeDef(SSL_Hash_SHA384, "SHA384", cHashSHA384::k_Size)
126  {
127  }
128  virtual cHashBase* AllocHash(void) const override
129  {
130  return new cHashSHA512Builder(true);
131  }
132  virtual size_t ComputeHash(BYTE* pOutput, const BYTE* pInput, size_t nInputSize) const override
133  {
134  return cHashSHA512::ComputeHash(pOutput, pInput, nInputSize, true);
135  }
136  };
137 
139  {
141  public:
143  : cHashTypeDef(SSL_Hash_SHA512, "SHA512", cHashSHA512::k_Size)
144  {
145  }
146  virtual cHashBase* AllocHash(void) const override
147  {
148  return new cHashSHA512Builder(false);
149  }
150  virtual size_t ComputeHash(BYTE* pOutput, const BYTE* pInput, size_t nInputSize) const override
151  {
152  return cHashSHA512::ComputeHash(pOutput, pInput, nInputSize, false);
153  }
154  };
155 
156 }
157 
158 #endif // _INC_cHashSHA512_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cHashBase.h:20
Definition: cHashCode.h:167
Definition: cHashSHA512.h:109
static const size_t k_HashSize
Definition: cHashSHA512.h:113
cHashSHA384Builder()
Definition: cHashSHA512.h:115
Definition: cHashSHA512.h:40
cHashSHA384()
Definition: cHashSHA512.h:47
cHashSHA384(const char *pszHexDigest)
Definition: cHashSHA512.h:49
Definition: cHashSHA512.h:56
virtual ~cHashSHA512Builder()
Definition: cHashSHA512.h:83
cHashSHA512Builder(bool is384=false)
Definition: cHashSHA512.h:78
void SetZeroHash()
Definition: cHashSHA512.h:90
bool m_is384
0 => SHA-512 (64 bytes), else SHA-384 (48 bytes)
Definition: cHashSHA512.h:75
Definition: cHashSHA512.h:19
static size_t GRAYCALL ComputeHash(BYTE pOutput[k_Size], const void *pInput, size_t nInputSize, bool is384)
Definition: cHashSHA512.cpp:27
cHashSHA512() noexcept
Definition: cHashSHA512.h:26
UNITTEST_FRIEND(cHashSHA512)
cHashSHA512(const char *pszHexDigest)
Definition: cHashSHA512.h:28
Definition: cHashTypeDef.h:21
Definition: cHashSHA512.h:121
cHashTypeSHA384()
Definition: cHashSHA512.h:124
virtual size_t ComputeHash(BYTE *pOutput, const BYTE *pInput, size_t nInputSize) const override
Definition: cHashSHA512.h:132
virtual cHashBase * AllocHash(void) const override
Definition: cHashSHA512.h:128
Definition: cHashSHA512.h:139
cHashTypeSHA512()
Definition: cHashSHA512.h:142
virtual size_t ComputeHash(BYTE *pOutput, const BYTE *pInput, size_t nInputSize) const override
Definition: cHashSHA512.h:150
virtual cHashBase * AllocHash(void) const override
Definition: cHashSHA512.h:146
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
@ SSL_Hash_SHA512
Definition: cHashCode.h:36
@ SSL_Hash_SHA384
Definition: cHashCode.h:35
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
static void ZeroSecure(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:110