ProteoWizard
Main Page
Namespaces
Classes
Files
File List
File Members
pwiz
utility
misc
SHA1.h
Go to the documentation of this file.
1
/* $Id: SHA1.h 3300 2012-02-15 22:38:20Z chambm $
2
3
100% free public domain implementation of the SHA-1 algorithm
4
by Dominik Reichl <dominik.reichl@t-online.de>
5
Web: http://www.dominik-reichl.de/
6
7
Version 1.8 - 2008-03-16
8
- Converted project files to Visual Studio 2008 format.
9
- Added Unicode support for HashFile utility method.
10
- Added support for hashing files using the HashFile method that are
11
larger than 2 GB.
12
- HashFile now returns an error code instead of copying an error
13
message into the output buffer.
14
- GetHash now returns an error code and validates the input parameter.
15
- Added ReportHashStl STL utility method.
16
- Added REPORT_HEX_SHORT reporting mode.
17
- Improved Linux compatibility of test program.
18
19
Version 1.7 - 2006-12-21
20
- Fixed buffer underrun warning that appeared when compiling with
21
Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the
22
patch).
23
- Breaking change: ReportHash writes the final hash to the start
24
of the buffer, i.e. it's not appending it to the string anymore.
25
- Made some function parameters const.
26
- Added Visual Studio 2005 project files to demo project.
27
28
Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
29
- You can set the endianness in your files, no need to modify the
30
header file of the CSHA1 class anymore.
31
- Aligned data support.
32
- Made support/compilation of the utility functions (ReportHash and
33
HashFile) optional (useful when bytes count, for example in embedded
34
environments).
35
36
Version 1.5 - 2005-01-01
37
- 64-bit compiler compatibility added.
38
- Made variable wiping optional (define SHA1_WIPE_VARIABLES).
39
- Removed unnecessary variable initializations.
40
- ROL32 improvement for the Microsoft compiler (using _rotl).
41
42
Version 1.4 - 2004-07-22
43
- CSHA1 now compiles fine with GCC 3.3 under MacOS X (thanks to Larry
44
Hastings).
45
46
Version 1.3 - 2003-08-17
47
- Fixed a small memory bug and made a buffer array a class member to
48
ensure correct working when using multiple CSHA1 class instances at
49
one time.
50
51
Version 1.2 - 2002-11-16
52
- Borlands C++ compiler seems to have problems with string addition
53
using sprintf. Fixed the bug which caused the digest report function
54
not to work properly. CSHA1 is now Borland compatible.
55
56
Version 1.1 - 2002-10-11
57
- Removed two unnecessary header file includes and changed BOOL to
58
bool. Fixed some minor bugs in the web page contents.
59
60
Version 1.0 - 2002-06-20
61
- First official release.
62
63
======== Test Vectors (from FIPS PUB 180-1) ========
64
65
SHA1("abc") =
66
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
67
68
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
69
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
70
71
SHA1(A million repetitions of "a") =
72
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
73
*/
74
75
#ifndef ___SHA1_HDR___
76
#define ___SHA1_HDR___
77
78
#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
79
#define SHA1_UTILITY_FUNCTIONS
80
#endif
81
82
#if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
83
#define SHA1_STL_FUNCTIONS
84
#if !defined(SHA1_UTILITY_FUNCTIONS)
85
#error STL functions require SHA1_UTILITY_FUNCTIONS.
86
#endif
87
#endif
88
89
#include <memory.h>
90
91
#ifdef SHA1_UTILITY_FUNCTIONS
92
#include <stdio.h>
93
#include <string.h>
94
#endif
95
96
#ifdef SHA1_STL_FUNCTIONS
97
#include <string>
98
#endif
99
100
#ifdef _MSC_VER
101
#include <stdlib.h>
102
#endif
103
104
// You can define the endian mode in your files without modifying the SHA-1
105
// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
106
// in your files, before including the SHA1.h header file. If you don't
107
// define anything, the class defaults to little endian.
108
#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
109
#define SHA1_LITTLE_ENDIAN
110
#endif
111
112
// If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
113
// #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
114
// defaults to wiping.
115
#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
116
#define SHA1_WIPE_VARIABLES
117
#endif
118
119
#if defined(SHA1_HAS_TCHAR)
120
#include <tchar.h>
121
#else
122
#ifdef _MSC_VER
123
#include <tchar.h>
124
#else
125
#ifndef TCHAR
126
#define TCHAR char
127
#endif
128
#ifndef _T
129
#define _T(__x) (__x)
130
#define _tmain main
131
#define _tprintf printf
132
#define _getts gets
133
#define _tcslen strlen
134
#define _tfopen fopen
135
#define _tcscpy strcpy
136
#define _tcscat strcat
137
#define _sntprintf snprintf
138
#endif
139
#endif
140
#endif
141
142
#ifdef _MSC_VER // Compiling with Microsoft compiler
143
#define _fseeki64 _fseeki64
144
#define _ftelli64 _ftelli64
145
#elif __MINGW || defined(__MINGW32__)
146
#define _fseeki64 fseeko64
147
#define _ftelli64 ftello64
148
#else // assume POSIX
149
#define _fseeki64 fseeko
150
#define _ftelli64 ftello
151
#endif
152
153
///////////////////////////////////////////////////////////////////////////
154
// Define variable types
155
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
164
#ifndef UINT_32
165
#ifdef _MSC_VER // Compiling with Microsoft compiler
166
#define UINT_32 unsigned __int32
167
#else // !_MSC_VER
168
#if (ULONG_MAX == 0xFFFFFFFF)
169
#define UINT_32 unsigned long
170
#else
171
#define UINT_32 unsigned int
172
#endif
173
#endif // _MSC_VER
174
#endif // UINT_32
175
176
#ifndef INT_64
177
#ifdef _MSC_VER // Compiling with Microsoft compiler
178
#define INT_64 __int64
179
#else // !_MSC_VER
180
#define INT_64 long long
181
#endif // _MSC_VER
182
#endif // INT_64
183
184
#ifndef UINT_64
185
#ifdef _MSC_VER // Compiling with Microsoft compiler
186
#define UINT_64 unsigned __int64
187
#else // !_MSC_VER
188
#define UINT_64 unsigned long long
189
#endif // _MSC_VER
190
#endif // UINT_64
191
192
///////////////////////////////////////////////////////////////////////////
193
// Declare SHA-1 workspace
194
195
typedef
union
196
{
197
UINT_8
c[64];
198
UINT_32
l[16];
199
}
SHA1_WORKSPACE_BLOCK
;
200
201
class
CSHA1
202
{
203
public
:
204
#ifdef SHA1_UTILITY_FUNCTIONS
205
// Different formats for ReportHash
206
enum
REPORT_TYPE
207
{
208
REPORT_HEX
= 0,
209
REPORT_DIGIT
= 1,
210
REPORT_HEX_SHORT
= 2
211
};
212
#endif
213
214
// Constructor and destructor
215
CSHA1
();
216
~CSHA1
();
217
218
UINT_32
m_state
[5];
219
UINT_32
m_count
[2];
220
UINT_32
m_reserved0
[1];
// Memory alignment padding
221
UINT_8
m_buffer
[64];
222
UINT_8
m_digest
[20];
223
UINT_32
m_reserved1
[3];
// Memory alignment padding
224
225
void
Reset
();
226
227
// Update the hash value
228
void
Update
(
const
UINT_8
* pbData,
UINT_32
uLen);
229
230
#ifdef SHA1_UTILITY_FUNCTIONS
231
// Hash in file contents
232
bool
HashFile
(
const
TCHAR
* tszFileName);
233
#endif
234
235
// Finalize hash, call before using ReportHash(Stl)
236
void
Final
();
237
238
#ifdef SHA1_UTILITY_FUNCTIONS
239
bool
ReportHash
(
TCHAR
* tszReport,
REPORT_TYPE
rtReportType =
REPORT_HEX
)
const
;
240
#endif
241
242
#ifdef SHA1_STL_FUNCTIONS
243
bool
ReportHashStl
(std::basic_string<TCHAR>& strOut,
REPORT_TYPE
rtReportType =
244
REPORT_HEX
)
const
;
245
#endif
246
247
bool
GetHash
(
UINT_8
* pbDest)
const
;
248
249
private
:
250
// Private SHA-1 transformation
251
void
Transform
(
UINT_32
* pState,
const
UINT_8
* pBuffer);
252
253
// Member variables
254
UINT_8
m_workspace
[64];
255
SHA1_WORKSPACE_BLOCK
*
m_block
;
// SHA1 pointer to the byte array above
256
};
257
258
#endif // ___SHA1_HDR___
Generated on Mon Nov 26 2012 18:05:49 for ProteoWizard by
1.8.1.1