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/
6 Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
7 - You can set the endianness in your files, no need to modify the
8 header file of the CSHA1 class any more
10 - Made support/compilation of the utility functions (ReportHash
11 and HashFile) optional (useful, if bytes count, for example in
12 embedded environments)
14 Version 1.5 - 2005-01-01
15 - 64-bit compiler compatibility added
16 - Made variable wiping optional (define SHA1_WIPE_VARIABLES)
17 - Removed unnecessary variable initializations
18 - ROL32 improvement for the Microsoft compiler (using _rotl)
20 ======== Test Vectors (from FIPS PUB 180-1) ========
23 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
25 SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
26 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
28 SHA1(A million repetitions of "a") =
29 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
32 #ifndef ___SHA1_HDR___
33 #define ___SHA1_HDR___
35 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
36 #define SHA1_UTILITY_FUNCTIONS
39 #include <memory.h> // Needed for memset and memcpy
42 #ifdef SHA1_UTILITY_FUNCTIONS
43 #include <stdio.h> // Needed for file access and sprintf
44 #include <string.h> // Needed for strcat and strcpy
51 // You can define the endian mode in your files, without modifying the SHA1
52 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
53 // in your files, before including the SHA1.h header file. If you don't
54 // define anything, the class defaults to little endian.
56 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
57 #define SHA1_LITTLE_ENDIAN
60 // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
61 // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
62 // defaults to wiping.
64 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
65 #define SHA1_WIPE_VARIABLES
68 /////////////////////////////////////////////////////////////////////////////
69 // Define 8- and 32-bit variables
75 //#define uint8_t unsigned __int8
76 //#define uint32_t unsigned __int32
80 //#define uint8_t unsigned char
82 //#if (ULONG_MAX == 0xFFFFFFFF)
83 //#define uint32_t unsigned long
85 //#define uint32_t unsigned int
91 /////////////////////////////////////////////////////////////////////////////
92 // Declare SHA1 workspace
98 } SHA1_WORKSPACE_BLOCK;
103 #ifdef SHA1_UTILITY_FUNCTIONS
104 // Two different formats for ReportHash(...)
112 // Constructor and Destructor
118 uint32_t __reserved1[1];
119 uint8_t m_buffer[64];
120 uint8_t m_digest[20];
121 uint32_t __reserved2[3];
125 // Update the hash value
126 void Update(const uint8_t *data, uint32_t len);
127 #ifdef SHA1_UTILITY_FUNCTIONS
128 bool HashFile(char *szFileName);
131 // Finalize hash and report
134 // Report functions: as pre-formatted and raw data
135 #ifdef SHA1_UTILITY_FUNCTIONS
136 void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
138 void GetHash(uint8_t *puDest);
141 // Private SHA-1 transformation
142 void Transform(uint32_t *state, const uint8_t *buffer);
145 uint8_t m_workspace[64];
146 SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above