1 | // [License] |
---|
2 | // 100% free public domain implementation of the SHA-1 algorithm |
---|
3 | // by Dominik Reichl <dominik.reichl@t-online.de> |
---|
4 | // [License] |
---|
5 | /* |
---|
6 | Version 1.5 - 2005-01-01 |
---|
7 | - 64-bit compiler compatibility added |
---|
8 | - Made variable wiping optional (define SHA1_WIPE_VARIABLES) |
---|
9 | - Removed unnecessary variable initializations |
---|
10 | - ROL32 improvement for the Microsoft compiler (using _rotl) |
---|
11 | |
---|
12 | ======== Test Vectors (from FIPS PUB 180-1) ======== |
---|
13 | |
---|
14 | SHA1("abc") = |
---|
15 | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
---|
16 | |
---|
17 | SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = |
---|
18 | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
---|
19 | |
---|
20 | SHA1(A million repetitions of "a") = |
---|
21 | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef ___SHA1_HDR___ |
---|
25 | #define ___SHA1_HDR___ |
---|
26 | |
---|
27 | #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS) |
---|
28 | #define SHA1_UTILITY_FUNCTIONS |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <memory.h> // Needed for memset and memcpy |
---|
32 | #include <boost/cstdint.hpp> |
---|
33 | |
---|
34 | #ifdef SHA1_UTILITY_FUNCTIONS |
---|
35 | #include <stdio.h> // Needed for file access and sprintf |
---|
36 | #include <string.h> // Needed for strcat and strcpy |
---|
37 | #endif |
---|
38 | |
---|
39 | |
---|
40 | // You can define the endian mode in your files, without modifying the SHA1 |
---|
41 | // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN |
---|
42 | // in your files, before including the SHA1.h header file. If you don't |
---|
43 | // define anything, the class defaults to little endian. |
---|
44 | |
---|
45 | #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN) |
---|
46 | #define SHA1_LITTLE_ENDIAN |
---|
47 | #endif |
---|
48 | |
---|
49 | // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if |
---|
50 | // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it |
---|
51 | // defaults to wiping. |
---|
52 | |
---|
53 | #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES) |
---|
54 | #define SHA1_WIPE_VARIABLES |
---|
55 | #endif |
---|
56 | |
---|
57 | ///////////////////////////////////////////////////////////////////////////// |
---|
58 | // Define 8- and 32-bit variables |
---|
59 | |
---|
60 | #define UINT_8 uint8_t |
---|
61 | #define UINT_32 uint32_t |
---|
62 | |
---|
63 | ///////////////////////////////////////////////////////////////////////////// |
---|
64 | // Declare SHA1 workspace |
---|
65 | |
---|
66 | typedef union |
---|
67 | { |
---|
68 | UINT_8 c[64]; |
---|
69 | UINT_32 l[16]; |
---|
70 | } SHA1_WORKSPACE_BLOCK; |
---|
71 | |
---|
72 | class CSHA1 |
---|
73 | { |
---|
74 | public: |
---|
75 | #ifdef SHA1_UTILITY_FUNCTIONS |
---|
76 | // Two different formats for ReportHash(...) |
---|
77 | enum |
---|
78 | { |
---|
79 | REPORT_HEX = 0, |
---|
80 | REPORT_DIGIT = 1 |
---|
81 | }; |
---|
82 | #endif |
---|
83 | |
---|
84 | // Constructor and Destructor |
---|
85 | CSHA1(); |
---|
86 | ~CSHA1(); |
---|
87 | |
---|
88 | UINT_32 m_state[5]; |
---|
89 | UINT_32 m_count[2]; |
---|
90 | UINT_32 __reserved1[1]; |
---|
91 | UINT_8 m_buffer[64]; |
---|
92 | UINT_8 m_digest[20]; |
---|
93 | UINT_32 __reserved2[3]; |
---|
94 | |
---|
95 | void Reset(); |
---|
96 | |
---|
97 | // Update the hash value |
---|
98 | void Update(UINT_8 *data, UINT_32 len); |
---|
99 | #ifdef SHA1_UTILITY_FUNCTIONS |
---|
100 | |
---|
101 | bool HashFile(char *szFileName); |
---|
102 | #endif |
---|
103 | |
---|
104 | // Finalize hash and report |
---|
105 | void Final(); |
---|
106 | |
---|
107 | // Report functions: as pre-formatted and raw data |
---|
108 | #ifdef SHA1_UTILITY_FUNCTIONS |
---|
109 | |
---|
110 | void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX); |
---|
111 | #endif |
---|
112 | |
---|
113 | void GetHash(UINT_8 *puDest); |
---|
114 | |
---|
115 | private: |
---|
116 | // Private SHA-1 transformation |
---|
117 | void Transform(UINT_32 *state, UINT_8 *buffer); |
---|
118 | |
---|
119 | // Member variables |
---|
120 | UINT_8 m_workspace[64]; |
---|
121 | SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above |
---|
122 | }; |
---|
123 | |
---|
124 | #endif |
---|