33 Message_Block_Index = 0;
62 for(
int i = 0; i < 20; ++i )
63 sprintf( buf + i * 2,
"%02x", (
unsigned char)( H[i >> 2] >> ( ( 3 - ( i & 3 ) ) << 3 ) ) );
65 return std::string( buf, 40 );
78 void SHA::feed(
const unsigned char *data,
unsigned length )
83 if( m_finished || m_corrupted )
89 while( length-- && !m_corrupted )
91 Message_Block[Message_Block_Index++] = ( *data & 0xFF );
94 Length_Low &= 0xFFFFFFFF;
98 Length_High &= 0xFFFFFFFF;
99 if( Length_High == 0 )
105 if( Message_Block_Index == 64 )
116 feed( (
const unsigned char*)data.c_str(), data.length() );
121 const unsigned K[] = { 0x5A827999,
129 unsigned A, B, C, D, E;
131 for( t = 0; t < 16; t++ )
133 W[t] = ((unsigned) Message_Block[t * 4]) << 24;
134 W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
135 W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
136 W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
139 for( t = 16; t < 80; ++t )
141 W[t] = shift( 1, W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16] );
150 for( t = 0; t < 20; ++t )
152 temp = shift( 5, A ) + ( ( B & C ) | ( ( ~B ) & D ) ) + E + W[t] + K[0];
161 for( t = 20; t < 40; ++t )
163 temp = shift( 5, A ) + ( B ^ C ^ D ) + E + W[t] + K[1];
172 for( t = 40; t < 60; ++t )
174 temp = shift( 5, A ) + ( ( B & C ) | ( B & D ) | ( C & D ) ) + E + W[t] + K[2];
183 for( t = 60; t < 80; ++t )
185 temp = shift( 5, A ) + ( B ^ C ^ D ) + E + W[t] + K[3];
194 H[0] = ( H[0] + A ) & 0xFFFFFFFF;
195 H[1] = ( H[1] + B ) & 0xFFFFFFFF;
196 H[2] = ( H[2] + C ) & 0xFFFFFFFF;
197 H[3] = ( H[3] + D ) & 0xFFFFFFFF;
198 H[4] = ( H[4] + E ) & 0xFFFFFFFF;
200 Message_Block_Index = 0;
205 if( Message_Block_Index > 55 )
207 Message_Block[Message_Block_Index++] = 0x80;
208 while( Message_Block_Index < 64 )
210 Message_Block[Message_Block_Index++] = 0;
215 while( Message_Block_Index < 56 )
217 Message_Block[Message_Block_Index++] = 0;
222 Message_Block[Message_Block_Index++] = 0x80;
223 while( Message_Block_Index < 56 )
225 Message_Block[Message_Block_Index++] = 0;
230 Message_Block[56] = ( Length_High >> 24 ) & 0xFF;
231 Message_Block[57] = ( Length_High >> 16 ) & 0xFF;
232 Message_Block[58] = ( Length_High >> 8 ) & 0xFF;
233 Message_Block[59] = ( Length_High ) & 0xFF;
234 Message_Block[60] = ( Length_Low >> 24 ) & 0xFF;
235 Message_Block[61] = ( Length_Low >> 16 ) & 0xFF;
236 Message_Block[62] = ( Length_Low >> 8 ) & 0xFF;
237 Message_Block[63] = ( Length_Low ) & 0xFF;
243 unsigned SHA::shift(
int bits,
unsigned word )
245 return ( ( word << bits ) & 0xFFFFFFFF) | ( ( word & 0xFFFFFFFF ) >> ( 32-bits ) );