Hash.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /******************************************************************************/
  2. struct CRC32 // Cyclic Redundancy Check
  3. {
  4. void reset ( ) ; // reset status
  5. void update (CPtr data, Int size) ; // update with next portion of data
  6. UInt operator()( )C; // get current hash value
  7. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  8. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  9. CRC32() {reset();}
  10. private:
  11. UInt _crc;
  12. };
  13. /******************************************************************************/
  14. struct xxHash32 // xxHash 32-bit
  15. {
  16. void reset ( ); // reset status
  17. void update (CPtr data, Int size); // update with next portion of data
  18. UInt operator()( ); // finalize and get hash result
  19. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  20. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  21. xxHash32() {reset();}
  22. private:
  23. ULong _buffer[6];
  24. Bool _finalized;
  25. UInt _hash;
  26. #if EE_PRIVATE
  27. void finalize();
  28. #endif
  29. };
  30. /******************************************************************************/
  31. struct xxHash64 // xxHash 64-bit
  32. {
  33. void reset ( ); // reset status
  34. void update (CPtr data, Int size); // update with next portion of data
  35. UInt hash32 ( ); // finalize and get 32-bit hash result
  36. ULong hash64 ( ); // finalize and get 64-bit hash result
  37. ULong operator()( ) {return hash64();} // finalize and get 64-bit hash result
  38. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  39. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  40. xxHash64() {reset();}
  41. private:
  42. ULong _buffer[11];
  43. Bool _finalized;
  44. ULong _hash;
  45. #if EE_PRIVATE
  46. void finalize();
  47. #endif
  48. };
  49. /******************************************************************************/
  50. struct SpookyHash // Spooky Hash 32/64/128-bit
  51. {
  52. void reset ( ); // reset status
  53. void update (CPtr data, Int size); // update with next portion of data
  54. UInt hash32 ( ); // finalize and get 32-bit hash result
  55. ULong hash64 ( ); // finalize and get 64-bit hash result
  56. C UID& hash128( ); // finalize and get 128-bit hash result
  57. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  58. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  59. SpookyHash() {reset();}
  60. private:
  61. ULong _data[X64 ? 38 : 37];
  62. Bool _finalized;
  63. UID _hash;
  64. #if EE_PRIVATE
  65. void finalize();
  66. #endif
  67. };
  68. /******************************************************************************/
  69. struct MetroHash64 // Metro Hash 64-bit
  70. {
  71. void reset ( ); // reset status
  72. void update (CPtr data, Int size); // update with next portion of data
  73. ULong operator()( ); // finalize and get hash result
  74. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  75. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  76. MetroHash64() {reset();}
  77. private:
  78. ULong _data[10];
  79. Bool _finalized;
  80. ULong _hash;
  81. };
  82. /******************************************************************************/
  83. struct MetroHash128 // Metro Hash 128-bit
  84. {
  85. void reset ( ); // reset status
  86. void update (CPtr data, Int size); // update with next portion of data
  87. C UID& operator()( ); // finalize and get hash result
  88. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  89. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  90. MetroHash128() {reset();}
  91. private:
  92. ULong _data[9];
  93. Bool _finalized;
  94. UID _hash;
  95. };
  96. /******************************************************************************/
  97. struct MD5 // Message-Digest Algorithm
  98. {
  99. void reset ( ); // reset status
  100. void update (CPtr data, Int size); // update with next portion of data
  101. C UID& operator()( ); // finalize and get hash result
  102. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  103. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  104. MD5() {reset();}
  105. private:
  106. Byte _buffer[64];
  107. UInt _count [2];
  108. Bool _finalized;
  109. UID _hash;
  110. #if EE_PRIVATE
  111. void finalize ();
  112. void transform(const Byte block[64]);
  113. #endif
  114. };
  115. /******************************************************************************/
  116. struct SHA1 // Secure Hash Algorithm-1, with 160-bits
  117. {
  118. union Hash
  119. {
  120. struct{Byte b[20];};
  121. struct{UInt i[ 5];};
  122. void zero() {Zero(T);}
  123. };
  124. void reset ( ); // reset status
  125. void update (CPtr data, Int size); // update with next portion of data
  126. C Hash& operator()( ); // finalize and get hash result
  127. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  128. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  129. SHA1() {reset();}
  130. #if !EE_PRIVATE
  131. private:
  132. #endif
  133. Byte _buffer[64];
  134. UInt _count [2];
  135. Bool _finalized;
  136. Hash _hash;
  137. #if EE_PRIVATE
  138. void finalize ();
  139. void transform(const Byte block[64]);
  140. #endif
  141. };
  142. /******************************************************************************/
  143. struct SHA2 // Secure Hash Algorithm-2, with 256-bits
  144. {
  145. union Hash
  146. {
  147. struct{Byte b[32];};
  148. struct{UInt i[ 8];};
  149. struct{ULong l[ 4];};
  150. void zero() {Zero(T);}
  151. };
  152. void reset ( ); // reset status
  153. void update (CPtr data, Int size); // update with next portion of data
  154. C Hash& operator()( ); // finalize and get hash result
  155. void update(C Str8 &s) {update(s(), s.length()*SIZE(Char8));}
  156. void update(C Str &s) {update(s(), s.length()*SIZE(Char ));}
  157. SHA2() {reset();}
  158. #if !EE_PRIVATE
  159. private:
  160. #endif
  161. Byte _buffer[64];
  162. UInt _count [2];
  163. Bool _finalized;
  164. Hash _hash;
  165. #if EE_PRIVATE
  166. void finalize ();
  167. void transform(const Byte block[64]);
  168. #endif
  169. };
  170. /******************************************************************************/
  171. UInt CRC32Mem(CPtr data, Int size); // calculate CRC32 for given memory
  172. UInt xxHash32Mem(CPtr data, Int size); // calculate xxHash32 for given memory
  173. UInt xxHash64_32Mem(CPtr data, Int size); // calculate xxHash64-32 for given memory
  174. ULong xxHash64Mem(CPtr data, Int size); // calculate xxHash64 for given memory
  175. UInt SpookyHash32Mem(CPtr data, Int size); // calculate SpookyHash32 for given memory
  176. ULong SpookyHash64Mem(CPtr data, Int size); // calculate SpookyHash64 for given memory
  177. UID SpookyHash128Mem(CPtr data, Int size); // calculate SpookyHash128 for given memory
  178. ULong MetroHash64Mem(CPtr data, Int size); // calculate MetroHash64 for given memory
  179. UID MetroHash128Mem(CPtr data, Int size); // calculate MetroHash128 for given memory
  180. UID MD5Mem(CPtr data, Int size); // calculate MD5 for given memory
  181. SHA1::Hash SHA1Mem(CPtr data, Int size); // calculate SHA1 for given memory
  182. SHA2::Hash SHA2Mem(CPtr data, Int size); // calculate SHA2 for given memory
  183. SHA2::Hash HMAC_SHA2(CPtr key, Int key_size, CPtr data, Int data_size); // calculate HMAC-SHA2 for given key and data
  184. /******************************************************************************/
  185. #if EE_PRIVATE
  186. void InitHash();
  187. #endif
  188. /******************************************************************************/