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 );
71 // Destructor ********************************************************
75 H0
= H1
= H2
= H3
= H4
= 0;
76 for( int c
= 0; c
< 64; c
++ ) bytes
[c
] = 0;
77 unprocessedBytes
= size
= 0;
80 // process ***********************************************************
83 assert( unprocessedBytes
== 64 );
84 //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
86 Uint32 a
, b
, c
, d
, e
, K
, f
, W
[80];
93 // copy and expand the message block
94 for( t
= 0; t
< 16; t
++ ) W
[t
] = (bytes
[t
*4] << 24)
95 +(bytes
[t
*4 + 1] << 16)
96 +(bytes
[t
*4 + 2] << 8)
98 for(; t
< 80; t
++ ) W
[t
] = lrot( W
[t
-3]^W
[t
-8]^W
[t
-14]^W
[t
-16], 1 );
102 for( t
= 0; t
< 80; t
++ )
106 f
= (b
& c
) | ((b
^ 0xFFFFFFFF) & d
);//TODO: try using ~
107 } else if( t
< 40 ) {
110 } else if( t
< 60 ) {
112 f
= (b
& c
) | (b
& d
) | (c
& d
);
117 temp
= lrot(a
,5) + f
+ e
+ W
[t
] + K
;
123 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
131 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
132 /* all bytes have been processed */
133 unprocessedBytes
= 0;
136 // addBytes **********************************************************
137 void SHA1::addBytes( const char* data
, int num
)
141 // add these bytes to the running total
143 // repeat until all data is processed
146 // number of bytes required to complete block
147 int needed
= 64 - unprocessedBytes
;
148 assert( needed
> 0 );
149 // number of bytes to copy (use smaller of two)
150 int toCopy
= (num
< needed
) ? num
: needed
;
152 memcpy( bytes
+ unprocessedBytes
, data
, toCopy
);
153 // Bytes have been copied
156 unprocessedBytes
+= toCopy
;
158 // there is a full block
159 if( unprocessedBytes
== 64 ) process();
163 // digest ************************************************************
164 unsigned char* SHA1::getDigest()
166 // save the message size
167 Uint32 totalBitsL
= size
<< 3;
168 Uint32 totalBitsH
= size
>> 29;
169 // add 0x80 to the message
170 addBytes( "\x80", 1 );
172 unsigned char footer
[64] = {
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
177 // block has no room for 8-byte filesize, so finish it
178 if( unprocessedBytes
> 56 )
179 addBytes( (char*)footer
, 64 - unprocessedBytes
);
180 assert( unprocessedBytes
<= 56 );
181 // how many zeros do we need
182 int neededZeros
= 56 - unprocessedBytes
;
183 // store file size (in bits) in big-endian format
184 storeBigEndianUint32( footer
+ neededZeros
, totalBitsH
);
185 storeBigEndianUint32( footer
+ neededZeros
+ 4, totalBitsL
);
186 // finish the final block
187 addBytes( (char*)footer
, neededZeros
+ 8 );
188 // allocate memory for the digest bytes
189 unsigned char* digest
= (unsigned char*)malloc( 20 );
190 // copy the digest bytes
191 storeBigEndianUint32( digest
, H0
);
192 storeBigEndianUint32( digest
+ 4, H1
);
193 storeBigEndianUint32( digest
+ 8, H2
);
194 storeBigEndianUint32( digest
+ 12, H3
);
195 storeBigEndianUint32( digest
+ 16, H4
);