3 Copyright (c) 2005 Michael D. Leonhard
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 // print out memory in hexadecimal
35 void SHA1::hexPrinter( unsigned char* c, int l )
41 printf( " %02x", *c );
47 // circular left bit rotation. MSB wraps around to LSB
48 Uint32 SHA1::lrot( Uint32 x, int bits )
50 return (x<<bits) | (x>>(32 - bits));
53 // Save a 32-bit unsigned integer to memory, in big-endian order
54 void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num )
57 byte[0] = (unsigned char)(num>>24);
58 byte[1] = (unsigned char)(num>>16);
59 byte[2] = (unsigned char)(num>>8);
60 byte[3] = (unsigned char)num;
64 // Constructor *******************************************************
67 // make sure that the data type is the right size
68 assert( sizeof( Uint32 ) * 5 == 20 );
80 // Destructor ********************************************************
84 H0 = H1 = H2 = H3 = H4 = 0;
85 for( int c = 0; c < 64; c++ ) bytes[c] = 0;
86 unprocessedBytes = size = 0;
89 // process ***********************************************************
92 assert( unprocessedBytes == 64 );
93 //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
95 Uint32 a, b, c, d, e, K, f, W[80];
102 // copy and expand the message block
103 for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
104 +(bytes[t*4 + 1] << 16)
105 +(bytes[t*4 + 2] << 8)
107 for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
111 for( t = 0; t < 80; t++ )
115 f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~
116 } else if( t < 40 ) {
119 } else if( t < 60 ) {
121 f = (b & c) | (b & d) | (c & d);
126 temp = lrot(a,5) + f + e + W[t] + K;
132 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
140 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
141 /* all bytes have been processed */
142 unprocessedBytes = 0;
145 // addBytes **********************************************************
146 void SHA1::addBytes( const char* data, int num )
150 // add these bytes to the running total
152 // repeat until all data is processed
155 // number of bytes required to complete block
156 int needed = 64 - unprocessedBytes;
157 assert( needed > 0 );
158 // number of bytes to copy (use smaller of two)
159 int toCopy = (num < needed) ? num : needed;
161 memcpy( bytes + unprocessedBytes, data, toCopy );
162 // Bytes have been copied
165 unprocessedBytes += toCopy;
167 // there is a full block
168 if( unprocessedBytes == 64 ) process();
172 // digest ************************************************************
173 unsigned char* SHA1::getDigest()
175 // save the message size
176 Uint32 totalBitsL = size << 3;
177 Uint32 totalBitsH = size >> 29;
178 // add 0x80 to the message
179 addBytes( "\x80", 1 );
181 unsigned char footer[64] = {
182 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
186 // block has no room for 8-byte filesize, so finish it
187 if( unprocessedBytes > 56 )
188 addBytes( (char*)footer, 64 - unprocessedBytes);
189 assert( unprocessedBytes <= 56 );
190 // how many zeros do we need
191 int neededZeros = 56 - unprocessedBytes;
192 // store file size (in bits) in big-endian format
193 storeBigEndianUint32( footer + neededZeros , totalBitsH );
194 storeBigEndianUint32( footer + neededZeros + 4, totalBitsL );
195 // finish the final block
196 addBytes( (char*)footer, neededZeros + 8 );
197 // allocate memory for the digest bytes
198 unsigned char* digest = (unsigned char*)malloc( 20 );
199 // copy the digest bytes
200 storeBigEndianUint32( digest, H0 );
201 storeBigEndianUint32( digest + 4, H1 );
202 storeBigEndianUint32( digest + 8, H2 );
203 storeBigEndianUint32( digest + 12, H3 );
204 storeBigEndianUint32( digest + 16, H4 );