2 100% free public domain implementation of the SHA-1 algorithm
\r
3 by Dominik Reichl <dominik.reichl@t-online.de>
\r
4 Web: http://www.dominik-reichl.de/
\r
6 Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
\r
7 - You can set the endianness in your files, no need to modify the
\r
8 header file of the CSHA1 class any more
\r
9 - Aligned data support
\r
10 - Made support/compilation of the utility functions (ReportHash
\r
11 and HashFile) optional (useful, if bytes count, for example in
\r
12 embedded environments)
\r
14 Version 1.5 - 2005-01-01
\r
15 - 64-bit compiler compatibility added
\r
16 - Made variable wiping optional (define SHA1_WIPE_VARIABLES)
\r
17 - Removed unnecessary variable initializations
\r
18 - ROL32 improvement for the Microsoft compiler (using _rotl)
\r
20 ======== Test Vectors (from FIPS PUB 180-1) ========
\r
23 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
\r
25 SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
\r
26 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
\r
28 SHA1(A million repetitions of "a") =
\r
29 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
\r
32 #ifndef ___SHA1_HDR___
\r
33 #define ___SHA1_HDR___
\r
35 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
\r
36 #define SHA1_UTILITY_FUNCTIONS
\r
39 #include <memory.h> // Needed for memset and memcpy
\r
41 #ifdef SHA1_UTILITY_FUNCTIONS
\r
42 #include <stdio.h> // Needed for file access and sprintf
\r
43 #include <string.h> // Needed for strcat and strcpy
\r
50 // You can define the endian mode in your files, without modifying the SHA1
\r
51 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
\r
52 // in your files, before including the SHA1.h header file. If you don't
\r
53 // define anything, the class defaults to little endian.
\r
55 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
\r
56 #define SHA1_LITTLE_ENDIAN
\r
59 // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
\r
60 // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
\r
61 // defaults to wiping.
\r
63 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
\r
64 #define SHA1_WIPE_VARIABLES
\r
67 /////////////////////////////////////////////////////////////////////////////
\r
68 // Define 8- and 32-bit variables
\r
74 #define UINT_8 unsigned __int8
\r
75 #define UINT_32 unsigned __int32
\r
79 #define UINT_8 unsigned char
\r
81 #if (ULONG_MAX == 0xFFFFFFFF)
\r
82 #define UINT_32 unsigned long
\r
84 #define UINT_32 unsigned int
\r
90 /////////////////////////////////////////////////////////////////////////////
\r
91 // Declare SHA1 workspace
\r
97 } SHA1_WORKSPACE_BLOCK;
\r
102 #ifdef SHA1_UTILITY_FUNCTIONS
\r
103 // Two different formats for ReportHash(...)
\r
111 // Constructor and Destructor
\r
115 UINT_32 m_state[5];
\r
116 UINT_32 m_count[2];
\r
117 UINT_32 __reserved1[1];
\r
118 UINT_8 m_buffer[64];
\r
119 UINT_8 m_digest[20];
\r
120 UINT_32 __reserved2[3];
\r
124 // Update the hash value
\r
125 void Update(UINT_8 *data, UINT_32 len);
\r
126 #ifdef SHA1_UTILITY_FUNCTIONS
\r
127 bool HashFile(char *szFileName);
\r
130 // Finalize hash and report
\r
133 // Report functions: as pre-formatted and raw data
\r
134 #ifdef SHA1_UTILITY_FUNCTIONS
\r
135 void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
\r
137 void GetHash(UINT_8 *puDest);
\r
140 // Private SHA-1 transformation
\r
141 void Transform(UINT_32 *state, UINT_8 *buffer);
\r
143 // Member variables
\r
144 UINT_8 m_workspace[64];
\r
145 SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
\r