SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DR_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 /*
106 * This file was taken from RakNet 4.082.
107 *
108 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
109 *
110 * This source code was modified by SLikeSoft. Modifications in this file are put under the public domain.
111 * Alternatively you are permitted to license the modifications under the MIT license, if you so desire. The
112 * license can be found in the license.txt file in the root directory of this source tree.
113 */
114
115 #ifndef SHA1_H_A545E61D43E9404E8D736869AB3CBFE7
116 #define SHA1_H_A545E61D43E9404E8D736869AB3CBFE7
117
118 // KevinJ:
119 #include "memoryoverride.h"
120 #include <stdio.h> // Needed for file access
121
122 #include <memory.h> // Needed for memset and memcpy
123
124 #include <string.h> // Needed for strcat and strcpy
125 #include "Export.h"
126 //#define MAX_FILE_READ_BUFFER 8000
127 #define SHA1_LENGTH 20
128
129
130
131 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
132 #define SHA1_UTILITY_FUNCTIONS
133 #endif
134
135 #if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
136 #define SHA1_STL_FUNCTIONS
137 #if !defined(SHA1_UTILITY_FUNCTIONS)
138 #error STL functions require SHA1_UTILITY_FUNCTIONS.
139 #endif
140 #endif
141
142
143 #include <memory.h>
144
145 #include <limits.h>
146
147 #ifdef SHA1_UTILITY_FUNCTIONS
148 #include <stdio.h>
149 #include <string.h>
150 #endif
151
152 #ifdef SHA1_STL_FUNCTIONS
153 #include <string>
154 #endif
155
156 #ifdef _MSC_VER
157 #include <stdlib.h>
158 #endif
159
160 // You can define the endian mode in your files without modifying the SHA-1
161 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
162 // in your files, before including the DR_SHA1.h header file. If you don't
163 // define anything, the class defaults to little endian.
164 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
165 #define SHA1_LITTLE_ENDIAN
166 #endif
167
168 // If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
169 // #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
170 // defaults to wiping.
171 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
172 #define SHA1_WIPE_VARIABLES
173 #endif
174
175 #if defined(SHA1_HAS_TCHAR)
176 #include <tchar.h>
177 #else
178 #ifdef _MSC_VER
179 #include <tchar.h>
180 #else
181 #ifndef TCHAR
182 #define TCHAR char
183 #endif
184 #ifndef _T
185 #define _T(__x) (__x)
186 #define _tmain main
187 #define _tprintf printf
188 #define _getts gets
189 #define _tcslen strlen
190 #define _tfopen fopen
191 #define _tcscpy strcpy
192 #define _tcscat strcat
193 #define _sntprintf snprintf
194 #endif
195 #endif
196 #endif
197
199 // Define variable types
200
201 #ifndef UINT_8
202 #ifdef _MSC_VER // Compiling with Microsoft compiler
203 #define UINT_8 unsigned __int8
204 #else // !_MSC_VER
205 #define UINT_8 unsigned char
206 #endif // _MSC_VER
207 #endif
208
209 #ifndef UINT_32
210 #ifdef _MSC_VER // Compiling with Microsoft compiler
211 #define UINT_32 unsigned __int32
212 #else // !_MSC_VER
213 #if (ULONG_MAX == 0xFFFFFFFFUL)
214 #define UINT_32 unsigned long
215 #else
216 #define UINT_32 unsigned int
217 #endif
218 #endif // _MSC_VER
219 #endif // UINT_32
220
221 #ifndef INT_64
222 #ifdef _MSC_VER // Compiling with Microsoft compiler
223 #define INT_64 __int64
224 #else // !_MSC_VER
225 #define INT_64 long long
226 #endif // _MSC_VER
227 #endif // INT_64
228
229 #ifndef UINT_64
230 #ifdef _MSC_VER // Compiling with Microsoft compiler
231 #define UINT_64 unsigned __int64
232 #else // !_MSC_VER
233 #define UINT_64 unsigned long long
234 #endif // _MSC_VER
235 #endif // UINT_64
236
238 // Declare SHA-1 workspace
239
240 typedef union
241 {
242 UINT_8 c[64];
243 UINT_32 l[16];
244 } SHA1_WORKSPACE_BLOCK;
245
246 class RAK_DLL_EXPORT CSHA1
247 {
248 public:
249 #ifdef SHA1_UTILITY_FUNCTIONS
250 // Different formats for ReportHash(Stl)
251 enum REPORT_TYPE
252 {
253 REPORT_HEX = 0,
254 REPORT_DIGIT = 1,
255 REPORT_HEX_SHORT = 2
256 };
257 #endif
258
259 // Constructor and destructor
260 CSHA1();
261
262 #ifdef SHA1_WIPE_VARIABLES
263 ~CSHA1();
264 #endif
265
266 void Reset();
267
268 // Hash in binary data and strings
269 void Update(const UINT_8* pbData, UINT_32 uLen);
270
271 #ifdef SHA1_UTILITY_FUNCTIONS
272 // Hash in file contents
273 bool HashFile(const TCHAR* tszFileName);
274 #endif
275
276 // Finalize hash; call it before using ReportHash(Stl)
277 void Final();
278
279 #ifdef SHA1_UTILITY_FUNCTIONS
280 bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
281 #endif
282
283 #ifdef SHA1_STL_FUNCTIONS
284 bool ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType =
285 REPORT_HEX) const;
286 #endif
287
288 // Get the raw message digest (20 bytes)
289 bool GetHash(UINT_8* pbDest20) const;
290
291 unsigned char * GetHash( void ) const;
292 // KevinJ: http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf
293 static void HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]);
294
295 private:
296 // Private SHA-1 transformation
297 void Transform(UINT_32* pState, const UINT_8* pBuffer);
298
299 // Member variables
300 UINT_32 m_state[5];
301 UINT_32 m_count[2];
302 UINT_32 m_reserved0[1]; // Memory alignment padding
303 UINT_8 m_buffer[64];
304 UINT_8 m_digest[20];
305 UINT_32 m_reserved1[3]; // Memory alignment padding
306
307 UINT_8 m_workspace[64];
308 SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
309 };
310
311 #endif // SHA1_H_A545E61D43E9404E8D736869AB3CBFE7