3 Copyright (c) 2005 Michael D. Leonhard
7 This file is licensed under the terms described in the
8 accompanying LICENSE file.
18 // print out memory in hexadecimal
19 void SHA1::hexPrinter( unsigned char* c, int l )
25 printf( " %02x", *c );
31 // circular left bit rotation. MSB wraps around to LSB
32 Uint32 SHA1::lrot( Uint32 x, int bits )
34 return (x<<bits) | (x>>(32 - bits));
37 // Save a 32-bit unsigned integer to memory, in big-endian order
38 void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num )
41 byte[0] = (unsigned char)(num>>24);
42 byte[1] = (unsigned char)(num>>16);
43 byte[2] = (unsigned char)(num>>8);
44 byte[3] = (unsigned char)num;
48 // Constructor *******************************************************
51 // make sure that the data type is the right size
52 assert( sizeof( Uint32 ) * 5 == 20 );
64 // Destructor ********************************************************
68 H0 = H1 = H2 = H3 = H4 = 0;
69 for( int c = 0; c < 64; c++ ) bytes[c] = 0;
70 unprocessedBytes = size = 0;
73 // process ***********************************************************
76 assert( unprocessedBytes == 64 );
77 //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
79 Uint32 a, b, c, d, e, K, f, W[80];
86 // copy and expand the message block
87 for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
88 +(bytes[t*4 + 1] << 16)
89 +(bytes[t*4 + 2] << 8)
91 for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
95 for( t = 0; t < 80; t++ )
99 f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~
100 } else if( t < 40 ) {
103 } else if( t < 60 ) {
105 f = (b & c) | (b & d) | (c & d);
110 temp = lrot(a,5) + f + e + W[t] + K;
116 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
124 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
125 /* all bytes have been processed */
126 unprocessedBytes = 0;
129 // addBytes **********************************************************
130 void SHA1::addBytes( const char* data, int num )
134 // add these bytes to the running total
136 // repeat until all data is processed
139 // number of bytes required to complete block
140 int needed = 64 - unprocessedBytes;
141 assert( needed > 0 );
142 // number of bytes to copy (use smaller of two)
143 int toCopy = (num < needed) ? num : needed;
145 memcpy( bytes + unprocessedBytes, data, toCopy );
146 // Bytes have been copied
149 unprocessedBytes += toCopy;
151 // there is a full block
152 if( unprocessedBytes == 64 ) process();
156 // digest ************************************************************
157 unsigned char* SHA1::getDigest()
159 // save the message size
160 Uint32 totalBitsL = size << 3;
161 Uint32 totalBitsH = size >> 29;
162 // add 0x80 to the message
163 addBytes( "\x80", 1 );
165 unsigned char footer[64] = {
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
170 // block has no room for 8-byte filesize, so finish it
171 if( unprocessedBytes > 56 )
172 addBytes( (char*)footer, 64 - unprocessedBytes);
173 assert( unprocessedBytes <= 56 );
174 // how many zeros do we need
175 int neededZeros = 56 - unprocessedBytes;
176 // store file size (in bits) in big-endian format
177 storeBigEndianUint32( footer + neededZeros , totalBitsH );
178 storeBigEndianUint32( footer + neededZeros + 4, totalBitsL );
179 // finish the final block
180 addBytes( (char*)footer, neededZeros + 8 );
181 // allocate memory for the digest bytes
182 unsigned char* digest = (unsigned char*)malloc( 20 );
183 // copy the digest bytes
184 storeBigEndianUint32( digest, H0 );
185 storeBigEndianUint32( digest + 4, H1 );
186 storeBigEndianUint32( digest + 8, H2 );
187 storeBigEndianUint32( digest + 12, H3 );
188 storeBigEndianUint32( digest + 16, H4 );