Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cHashCode.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cHashCode_H
7 #define _INC_cHashCode_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
16 #include "GrayCore/include/cHeap.h"
19 
20 namespace GrayLib
21 {
22  enum SSL_Hash_TYPE // BYTE
23  {
29 
30  SSL_Hash_NONE = 0, // Choose no hashing ?
31  SSL_Hash_MD5 = 1, // 16 bytes
32  SSL_Hash_SHA1 = 2, // 20 bytes
34  SSL_Hash_SHA256 = 4, // 32 bytes
35  SSL_Hash_SHA384 = 5, // 48 bytes
36  SSL_Hash_SHA512 = 6, // 64 bytes
37 
38  // SSL_Hash_X = 10, // Add NON standard hashes here. MD2, MD4 ?
40  };
41 
43  {
48 
49  typedef cHeapBlock SUPER_t;
50  typedef cHashCode THIS_t;
51 
52  public:
53 
54  // k_HashSizeMax
55 
56  cHashCode() noexcept
57  {
58  }
59  cHashCode(size_t nSize)
60  : cHeapBlock(nSize)
61  {
62  InitZeros();
63  }
64  cHashCode(const void* p, size_t nSize)
65  : cHeapBlock(p, nSize)
66  {
67  }
68  cHashCode(const SUPER_t& h1)
69  : cHeapBlock(h1)
70  {
71  }
73  {
75  SetErase();
76  }
77 
78  bool isValidHash() const noexcept
79  {
82  return !this->IsZeros(); // all zeros = bad.
83  }
84  size_t get_HashSize() const noexcept
85  {
87  if (!isValidHash())
88  return 0;
89  return get_DataSize();
90  }
91  bool isMatch(const SUPER_t& match) const
92  {
93  size_t nSize1 = get_HashSize();
94  size_t nSize2 = (match.IsZeros()) ? 0 : match.get_DataSize();
95  if (nSize1 != nSize2 || nSize2 == 0)
96  return false;
97  return !cMem::Compare(get_DataBytes(), match.get_DataBytes(), nSize1);
98  }
99  BYTE* SetRawDigest(const BYTE* pData, size_t nSize)
100  {
101  ASSERT(nSize > 0);
102  SUPER_t::Alloc(pData, nSize);
103  return SUPER_t::get_DataBytes();
104  }
105  BYTE* put_RawDigest(const SUPER_t& m)
106  {
107  return SetRawDigest(m.get_DataBytes(), m.get_DataSize());
108  }
110  {
112  return SUPER_t::get_DataBytes();
113  }
114 
115  void SetErase()
116  {
118  if (SUPER_t::isValidPtr())
119  {
120  InitZeros();
121  SUPER_t::Free();
122  }
123  }
124  THIS_t& operator = (const THIS_t& h1)
125  {
126  SetCopy(h1);
127  return *this;
128  }
129 
131  {
133  return cMem::GetHexDigestSize(get_DataSize());
134  }
135  StrLen_t GetHexDigest(OUT char* pszHexString) const
136  {
138  return cMem::GetHexDigest(pszHexString, get_DataBytes(), get_HashSize());
139  }
141  {
144  cStringA sTmp;
145  StrLen_t nLen = GetHexDigest(sTmp.GetBuffer(get_HexSize()));
146  sTmp.ReleaseBuffer(nLen);
147  return sTmp;
148  }
149  bool isMatchHexDigest(const char* pszHexStr) const
150  {
152  cHashCode h2(get_DataSize());
153  HRESULT hRes = h2.put_HexDigest(pszHexStr);
154  if (FAILED(hRes))
155  return false;
156  return isMatch(h2);
157  }
158 
159  HRESULT put_HexDigest(const char* pszInp)
160  {
161  return cMem::SetHexDigest(pszInp, ref_RawDigest(), get_DataSize());
162  }
163  };
164 
165  template<int TYPE_SIZE>
166  class GRAYLIB_LINK cHashCodeT : public cMemStatic<TYPE_SIZE>
167  {
170 
172 
173  public:
174  bool isValidHash() const noexcept
175  {
177  return !cMem::IsZeros(this->m_Data, TYPE_SIZE);
178  }
179  size_t get_HashSize() const noexcept
180  {
181  if (!isValidHash())
182  return 0;
183  return TYPE_SIZE;
184  }
186  {
187  return cHashCode(this->get_DataBytes(), this->get_HashSize());
188  }
189 
190  StrLen_t GetHexDigest(OUT char* pszHexString) const
191  {
193  return cMem::GetHexDigest(pszHexString, this->m_Data, this->get_HashSize());
194  }
196  {
199  cStringA sTmp;
200  StrLen_t nLen = GetHexDigest(sTmp.GetBuffer(SUPER_t::k_SizeHexDigest));
201  sTmp.ReleaseBuffer(nLen);
202  return sTmp;
203  }
204  HRESULT put_HexDigest(const char* pszInp)
205  {
206  return cMem::SetHexDigest(pszInp, this->m_Data, TYPE_SIZE);
207  }
208  };
209 
210 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
211  // Force 16, 20, 28, 32, 48, 64
212  template class GRAYLIB_LINK cHashCodeT<16>;
213  template class GRAYLIB_LINK cHashCodeT<20>;
214  template class GRAYLIB_LINK cHashCodeT<28>;
215  template class GRAYLIB_LINK cHashCodeT<32>;
216  template class GRAYLIB_LINK cHashCodeT<48>;
217  template class GRAYLIB_LINK cHashCodeT<64>;
218 #endif
219 }
220 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cHashCode.h:167
HRESULT put_HexDigest(const char *pszInp)
Definition: cHashCode.h:204
cHashCode get_HashCode() const
Definition: cHashCode.h:185
StrLen_t GetHexDigest(OUT char *pszHexString) const
Definition: cHashCode.h:190
bool isValidHash() const noexcept
Definition: cHashCode.h:174
size_t get_HashSize() const noexcept
Definition: cHashCode.h:179
cStringA get_HexDigest() const
Definition: cHashCode.h:195
Definition: cHashCode.h:43
void SetErase()
Definition: cHashCode.h:115
HRESULT put_HexDigest(const char *pszInp)
Definition: cHashCode.h:159
bool isValidHash() const noexcept
Definition: cHashCode.h:78
BYTE * SetRawDigest(const BYTE *pData, size_t nSize)
Definition: cHashCode.h:99
bool isMatch(const SUPER_t &match) const
Definition: cHashCode.h:91
~cHashCode()
Definition: cHashCode.h:72
cHashCode(const SUPER_t &h1)
Definition: cHashCode.h:68
cStringA get_HexDigest() const
Definition: cHashCode.h:140
BYTE * ref_RawDigest()
Definition: cHashCode.h:109
cHashCode() noexcept
Definition: cHashCode.h:56
cHashCode(size_t nSize)
Definition: cHashCode.h:59
StrLen_t get_HexSize() const
Definition: cHashCode.h:130
StrLen_t GetHexDigest(OUT char *pszHexString) const
Definition: cHashCode.h:135
BYTE * put_RawDigest(const SUPER_t &m)
Definition: cHashCode.h:105
bool isMatchHexDigest(const char *pszHexStr) const
Definition: cHashCode.h:149
size_t get_HashSize() const noexcept
Definition: cHashCode.h:84
cHashCode(const void *p, size_t nSize)
Definition: cHashCode.h:64
void ReleaseBuffer(StrLen_t nNewLength=k_StrLen_UNK)
Definition: cString.cpp:108
_TYPE_CH * GetBuffer(StrLen_t iMinLength)
Definition: cString.cpp:138
Definition: cHeap.h:156
Definition: cMem.h:311
BYTE * get_DataBytes() const noexcept
Definition: cMem.h:354
bool IsZeros() const noexcept
Definition: cMem.h:400
size_t get_DataSize() const noexcept
Definition: cMem.h:344
Definition: cMem.h:284
Definition: cMesh.h:22
@ Free
Definition: cLicense.h:22
SSL_Hash_TYPE
Definition: cHashCode.h:23
@ SSL_Hash_SHA512
Definition: cHashCode.h:36
@ SSL_Hash_NONE
Definition: cHashCode.h:30
@ SSL_Hash_SHA1
Definition: cHashCode.h:32
@ SSL_Hash_QTY
Definition: cHashCode.h:39
@ SSL_Hash_SHA384
Definition: cHashCode.h:35
@ SSL_Hash_SHA256
Definition: cHashCode.h:34
@ SSL_Hash_MD5
Definition: cHashCode.h:31
@ SSL_Hash_SHA224
Definition: cHashCode.h:33
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
static bool IsZeros(const void *pData, size_t nSize) noexcept
Definition: cMem.h:65
static StrLen_t GetHexDigestSize(size_t nSize) noexcept
Definition: cMem.h:236
static StrLen_t __stdcall GetHexDigest(OUT char *pszHexString, const BYTE *pData, size_t nSizeData)
Definition: cMem.cpp:205
static HRESULT __stdcall SetHexDigest(const char *pszHexString, OUT BYTE *pData, size_t nSizeData)
Definition: cMem.cpp:221
static COMPARE_t Compare(const void *p1, const void *p2, size_t nSizeBlock) noexcept
Definition: cMem.h:78