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