Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cHashMD5.h
Go to the documentation of this file.
1 //
5 #ifndef _INC_cHashMD5_H
6 #define _INC_cHashMD5_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 #include "cHashBase.h"
11 #include "cHashTypeDef.h"
12 
13 namespace GrayLib
14 {
16 
17  class GRAYLIB_LINK cHashMD5 : public cHashCodeT<16>
18  {
24 
25  friend class cHashMD5Builder;
26  public:
27  cHashMD5() noexcept
28  {}
29  cHashMD5(const char* pszHexDigest)
30  {
31  put_HexDigest(pszHexDigest);
32  }
33 
34  static size_t GRAYCALL ComputeHash(BYTE pOutput[16], const void* pInput, size_t nSize);
35  void SetHashFrom(const void* pInput, size_t nInputSize)
36  {
37  ComputeHash(m_Data, pInput, nInputSize);
38  }
39  void SetHashFromStr(const char* pszString)
40  {
41  // Built a hash (and finalize) for a COMPLETE string.
42  if (pszString == nullptr)
43  return;
44  ComputeHash(m_Data, pszString, StrT::Len(pszString));
45  }
46 
47  static size_t GRAYCALL ComputeHmac(BYTE pOutput[16],const BYTE* pKey, size_t nKeySize, const void* pInput, size_t nSize);
48  void SetHmacFrom(const BYTE* pKey, size_t nKeySize, const void* pInput, size_t nInputSize)
49  {
50  ComputeHmac(m_Data, pKey, nKeySize, pInput, nInputSize);
51  }
52 
54  };
55 
57  {
60 
61  typedef cHashBase SUPER_t;
62 
63  public:
64  static const size_t k_HashSize = 16; // size bytes.
65 
66  UINT32 m_total[2];
67  UINT32 m_state[4];
68  BYTE m_buffer[64];
69 
70  BYTE m_ipad[64];
71  BYTE m_opad[64];
72 
73  public:
74  cHashMD5Builder() noexcept
75  {
76  cMem::Zero(m_buffer, sizeof(m_buffer));
77  cMem::Zero(m_ipad, sizeof(m_ipad));
78  cMem::Zero(m_opad, sizeof(m_opad));
79  ResetHash();
80  }
81  virtual ~cHashMD5Builder() noexcept
82  {
83  cMem::ZeroSecure(m_total, sizeof(m_total));
84  cMem::ZeroSecure(m_state, sizeof(m_state));
85  cMem::ZeroSecure(m_buffer, sizeof(m_buffer));
86  }
87 
88  virtual void ResetHash() override;
89  virtual void ProcessHashBuffer(const BYTE pBuffer[64]) override;
90  virtual void AddToHash(const void* pInput, size_t nSize) override;
91  virtual size_t FinalizeHash(BYTE* pOutput) override;
92 
94  virtual void InitHmac(const BYTE* pKey, size_t nKeySize) override;
95  virtual size_t FinalizeHmac(BYTE pOutput[16]) override;
96  virtual void ResetHmac() override;
97  };
98 
100  {
103  public:
104  cHashTypeMD5() noexcept
105  : cHashTypeDef(SSL_Hash_MD5, "MD5", cHashMD5::k_Size)
106  {
107  }
108  virtual cHashBase* AllocHash(void) const override
109  {
110  return new cHashMD5Builder();
111  }
112  virtual size_t ComputeHash(BYTE* pOutput, const BYTE* pInput, size_t nInputSize) const override
113  {
114  return cHashMD5::ComputeHash(pOutput, pInput, nInputSize);
115  }
116  };
117 }
118 
119 #endif // _INC_cHashMD5_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: cHashMD5.h:57
cHashMD5Builder() noexcept
Definition: cHashMD5.h:74
virtual ~cHashMD5Builder() noexcept
Definition: cHashMD5.h:81
Definition: cHashMD5.h:18
cHashMD5() noexcept
Definition: cHashMD5.h:27
static size_t GRAYCALL ComputeHash(BYTE pOutput[16], const void *pInput, size_t nSize)
Definition: cHashMD5.cpp:14
void SetHashFromStr(const char *pszString)
Definition: cHashMD5.h:39
cHashMD5(const char *pszHexDigest)
Definition: cHashMD5.h:29
UNITTEST_FRIEND(cHashMD5)
void SetHmacFrom(const BYTE *pKey, size_t nKeySize, const void *pInput, size_t nInputSize)
Definition: cHashMD5.h:48
void SetHashFrom(const void *pInput, size_t nInputSize)
Definition: cHashMD5.h:35
Definition: cHashTypeDef.h:21
Definition: cHashMD5.h:100
virtual cHashBase * AllocHash(void) const override
Definition: cHashMD5.h:108
cHashTypeMD5() noexcept
Definition: cHashMD5.h:104
virtual size_t ComputeHash(BYTE *pOutput, const BYTE *pInput, size_t nInputSize) const override
Definition: cHashMD5.h:112
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
@ SSL_Hash_MD5
Definition: cHashCode.h:31
static StrLen_t Len(const TYPE *pszStr) noexcept
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