DR_SHA1.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. 100% free public domain implementation of the SHA-1 algorithm
  3. by Dominik Reichl <dominik.reichl@t-online.de>
  4. Web: http://www.dominik-reichl.de/
  5. Version 2.1 - 2012-06-19
  6. - Deconstructor (resetting internal variables) is now only
  7. implemented if SHA1_WIPE_VARIABLES is defined (which is the
  8. default).
  9. - Renamed inclusion guard to contain a GUID.
  10. - Demo application is now using C++/STL objects and functions.
  11. - Unicode build of the demo application now outputs the hashes of both
  12. the ANSI and Unicode representations of strings.
  13. - Various other demo application improvements.
  14. Version 2.0 - 2012-06-14
  15. - Added 'limits.h' include.
  16. - Renamed inclusion guard and macros for compliancy (names beginning
  17. with an underscore are reserved).
  18. Version 1.9 - 2011-11-10
  19. - Added Unicode test vectors.
  20. - Improved support for hashing files using the HashFile method that
  21. are larger than 4 GB.
  22. - Improved file hashing performance (by using a larger buffer).
  23. - Disabled unnecessary compiler warnings.
  24. - Internal variables are now private.
  25. Version 1.8 - 2009-03-16
  26. - Converted project files to Visual Studio 2008 format.
  27. - Added Unicode support for HashFile utility method.
  28. - Added support for hashing files using the HashFile method that are
  29. larger than 2 GB.
  30. - HashFile now returns an error code instead of copying an error
  31. message into the output buffer.
  32. - GetHash now returns an error code and validates the input parameter.
  33. - Added ReportHashStl STL utility method.
  34. - Added REPORT_HEX_SHORT reporting mode.
  35. - Improved Linux compatibility of test program.
  36. Version 1.7 - 2006-12-21
  37. - Fixed buffer underrun warning that appeared when compiling with
  38. Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the
  39. patch).
  40. - Breaking change: ReportHash writes the final hash to the start
  41. of the buffer, i.e. it's not appending it to the string anymore.
  42. - Made some function parameters const.
  43. - Added Visual Studio 2005 project files to demo project.
  44. Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
  45. - You can set the endianness in your files, no need to modify the
  46. header file of the CSHA1 class anymore.
  47. - Aligned data support.
  48. - Made support/compilation of the utility functions (ReportHash and
  49. HashFile) optional (useful when bytes count, for example in embedded
  50. environments).
  51. Version 1.5 - 2005-01-01
  52. - 64-bit compiler compatibility added.
  53. - Made variable wiping optional (define SHA1_WIPE_VARIABLES).
  54. - Removed unnecessary variable initializations.
  55. - ROL32 improvement for the Microsoft compiler (using _rotl).
  56. Version 1.4 - 2004-07-22
  57. - CSHA1 now compiles fine with GCC 3.3 under Mac OS X (thanks to Larry
  58. Hastings).
  59. Version 1.3 - 2003-08-17
  60. - Fixed a small memory bug and made a buffer array a class member to
  61. ensure correct working when using multiple CSHA1 class instances at
  62. one time.
  63. Version 1.2 - 2002-11-16
  64. - Borlands C++ compiler seems to have problems with string addition
  65. using sprintf. Fixed the bug which caused the digest report function
  66. not to work properly. CSHA1 is now Borland compatible.
  67. Version 1.1 - 2002-10-11
  68. - Removed two unnecessary header file includes and changed BOOL to
  69. bool. Fixed some minor bugs in the web page contents.
  70. Version 1.0 - 2002-06-20
  71. - First official release.
  72. ================ Test Vectors ================
  73. SHA1("abc" in ANSI) =
  74. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  75. SHA1("abc" in Unicode LE) =
  76. 9F04F41A 84851416 2050E3D6 8C1A7ABB 441DC2B5
  77. SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  78. in ANSI) =
  79. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  80. SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  81. in Unicode LE) =
  82. 51D7D876 9AC72C40 9C5B0E3F 69C60ADC 9A039014
  83. SHA1(A million repetitions of "a" in ANSI) =
  84. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  85. SHA1(A million repetitions of "a" in Unicode LE) =
  86. C4609560 A108A0C6 26AA7F2B 38A65566 739353C5
  87. */
  88. #ifndef SHA1_H_A545E61D43E9404E8D736869AB3CBFE7
  89. #define SHA1_H_A545E61D43E9404E8D736869AB3CBFE7
  90. // KevinJ:
  91. #include "RakMemoryOverride.h"
  92. #include <stdio.h> // Needed for file access
  93. #include <memory.h> // Needed for memset and memcpy
  94. #include <string.h> // Needed for strcat and strcpy
  95. #include "Export.h"
  96. //#define MAX_FILE_READ_BUFFER 8000
  97. #define SHA1_LENGTH 20
  98. #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
  99. #define SHA1_UTILITY_FUNCTIONS
  100. #endif
  101. #if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
  102. #define SHA1_STL_FUNCTIONS
  103. #if !defined(SHA1_UTILITY_FUNCTIONS)
  104. #error STL functions require SHA1_UTILITY_FUNCTIONS.
  105. #endif
  106. #endif
  107. #include <memory.h>
  108. #include <limits.h>
  109. #ifdef SHA1_UTILITY_FUNCTIONS
  110. #include <stdio.h>
  111. #include <string.h>
  112. #endif
  113. #ifdef SHA1_STL_FUNCTIONS
  114. #include <string>
  115. #endif
  116. #ifdef _MSC_VER
  117. #include <stdlib.h>
  118. #endif
  119. // You can define the endian mode in your files without modifying the SHA-1
  120. // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
  121. // in your files, before including the DR_SHA1.h header file. If you don't
  122. // define anything, the class defaults to little endian.
  123. #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
  124. #define SHA1_LITTLE_ENDIAN
  125. #endif
  126. // If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
  127. // #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
  128. // defaults to wiping.
  129. #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
  130. #define SHA1_WIPE_VARIABLES
  131. #endif
  132. #if defined(SHA1_HAS_TCHAR)
  133. #include <tchar.h>
  134. #else
  135. #ifdef _MSC_VER
  136. #include <tchar.h>
  137. #else
  138. #ifndef TCHAR
  139. #define TCHAR char
  140. #endif
  141. #ifndef _T
  142. #define _T(__x) (__x)
  143. #define _tmain main
  144. #define _tprintf printf
  145. #define _getts gets
  146. #define _tcslen strlen
  147. #define _tfopen fopen
  148. #define _tcscpy strcpy
  149. #define _tcscat strcat
  150. #define _sntprintf snprintf
  151. #endif
  152. #endif
  153. #endif
  154. ///////////////////////////////////////////////////////////////////////////
  155. // Define variable types
  156. #ifndef UINT_8
  157. #ifdef _MSC_VER // Compiling with Microsoft compiler
  158. #define UINT_8 unsigned __int8
  159. #else // !_MSC_VER
  160. #define UINT_8 unsigned char
  161. #endif // _MSC_VER
  162. #endif
  163. #ifndef UINT_32
  164. #ifdef _MSC_VER // Compiling with Microsoft compiler
  165. #define UINT_32 unsigned __int32
  166. #else // !_MSC_VER
  167. #if (ULONG_MAX == 0xFFFFFFFFUL)
  168. #define UINT_32 unsigned long
  169. #else
  170. #define UINT_32 unsigned int
  171. #endif
  172. #endif // _MSC_VER
  173. #endif // UINT_32
  174. #ifndef INT_64
  175. #ifdef _MSC_VER // Compiling with Microsoft compiler
  176. #define INT_64 __int64
  177. #else // !_MSC_VER
  178. #define INT_64 long long
  179. #endif // _MSC_VER
  180. #endif // INT_64
  181. #ifndef UINT_64
  182. #ifdef _MSC_VER // Compiling with Microsoft compiler
  183. #define UINT_64 unsigned __int64
  184. #else // !_MSC_VER
  185. #define UINT_64 unsigned long long
  186. #endif // _MSC_VER
  187. #endif // UINT_64
  188. ///////////////////////////////////////////////////////////////////////////
  189. // Declare SHA-1 workspace
  190. typedef union
  191. {
  192. UINT_8 c[64];
  193. UINT_32 l[16];
  194. } SHA1_WORKSPACE_BLOCK;
  195. class RAK_DLL_EXPORT CSHA1
  196. {
  197. public:
  198. #ifdef SHA1_UTILITY_FUNCTIONS
  199. // Different formats for ReportHash(Stl)
  200. enum REPORT_TYPE
  201. {
  202. REPORT_HEX = 0,
  203. REPORT_DIGIT = 1,
  204. REPORT_HEX_SHORT = 2
  205. };
  206. #endif
  207. // Constructor and destructor
  208. CSHA1();
  209. #ifdef SHA1_WIPE_VARIABLES
  210. ~CSHA1();
  211. #endif
  212. void Reset();
  213. // Hash in binary data and strings
  214. void Update(const UINT_8* pbData, UINT_32 uLen);
  215. #ifdef SHA1_UTILITY_FUNCTIONS
  216. // Hash in file contents
  217. bool HashFile(const TCHAR* tszFileName);
  218. #endif
  219. // Finalize hash; call it before using ReportHash(Stl)
  220. void Final();
  221. #ifdef SHA1_UTILITY_FUNCTIONS
  222. bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
  223. #endif
  224. #ifdef SHA1_STL_FUNCTIONS
  225. bool ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType =
  226. REPORT_HEX) const;
  227. #endif
  228. // Get the raw message digest (20 bytes)
  229. bool GetHash(UINT_8* pbDest20) const;
  230. unsigned char * GetHash( void ) const;
  231. // KevinJ: http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf
  232. static void HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]);
  233. private:
  234. // Private SHA-1 transformation
  235. void Transform(UINT_32* pState, const UINT_8* pBuffer);
  236. // Member variables
  237. UINT_32 m_state[5];
  238. UINT_32 m_count[2];
  239. UINT_32 m_reserved0[1]; // Memory alignment padding
  240. UINT_8 m_buffer[64];
  241. UINT_8 m_digest[20];
  242. UINT_32 m_reserved1[3]; // Memory alignment padding
  243. UINT_8 m_workspace[64];
  244. SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
  245. };
  246. #endif // SHA1_H_A545E61D43E9404E8D736869AB3CBFE7
粤ICP备19079148号