36 Message_Block_Index = 0;
61 for(
int i = 0; i < 20; ++i )
62 sprintf( buf + i * 2,
"%02x", (
unsigned char)( H[i >> 2] >> ( ( 3 - ( i & 3 ) ) << 3 ) ) );
64 return std::string( buf, 40 );
72 unsigned char digest[20];
73 for(
int i = 0; i < 20; ++i )
74 digest[i] = (
unsigned char)( H[i >> 2] >> ( ( 3 - ( i & 3 ) ) << 3 ) );
76 return std::string( (
char*)digest, 20 );
88 void SHA::feed(
const unsigned char* data,
unsigned length )
93 if( m_finished || m_corrupted )
99 while( length-- && !m_corrupted )
101 Message_Block[Message_Block_Index++] = ( *data & 0xFF );
104 Length_Low &= 0xFFFFFFFF;
105 if( Length_Low == 0 )
108 Length_High &= 0xFFFFFFFF;
109 if( Length_High == 0 )
115 if( Message_Block_Index == 64 )
126 feed( (
const unsigned char*)data.c_str(), (int)data.length() );
131 const unsigned K[] = { 0x5A827999,
139 unsigned A, B, C, D, E;
141 for( t = 0; t < 16; t++ )
143 W[t] = ((unsigned) Message_Block[t * 4]) << 24;
144 W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
145 W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
146 W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
149 for( t = 16; t < 80; ++t )
151 W[t] = shift( 1, W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16] );
160 for( t = 0; t < 20; ++t )
162 temp = shift( 5, A ) + ( ( B & C ) | ( ( ~B ) & D ) ) + E + W[t] + K[0];
171 for( t = 20; t < 40; ++t )
173 temp = shift( 5, A ) + ( B ^ C ^ D ) + E + W[t] + K[1];
182 for( t = 40; t < 60; ++t )
184 temp = shift( 5, A ) + ( ( B & C ) | ( B & D ) | ( C & D ) ) + E + W[t] + K[2];
193 for( t = 60; t < 80; ++t )
195 temp = shift( 5, A ) + ( B ^ C ^ D ) + E + W[t] + K[3];
204 H[0] = ( H[0] + A ) & 0xFFFFFFFF;
205 H[1] = ( H[1] + B ) & 0xFFFFFFFF;
206 H[2] = ( H[2] + C ) & 0xFFFFFFFF;
207 H[3] = ( H[3] + D ) & 0xFFFFFFFF;
208 H[4] = ( H[4] + E ) & 0xFFFFFFFF;
210 Message_Block_Index = 0;
215 Message_Block[Message_Block_Index++] = 0x80;
217 if( Message_Block_Index > 56 )
219 while( Message_Block_Index < 64 )
221 Message_Block[Message_Block_Index++] = 0;
227 while( Message_Block_Index < 56 )
229 Message_Block[Message_Block_Index++] = 0;
232 Message_Block[56] =
static_cast<unsigned char>( ( Length_High >> 24 ) & 0xFF );
233 Message_Block[57] =
static_cast<unsigned char>( ( Length_High >> 16 ) & 0xFF );
234 Message_Block[58] =
static_cast<unsigned char>( ( Length_High >> 8 ) & 0xFF );
235 Message_Block[59] =
static_cast<unsigned char>( ( Length_High ) & 0xFF );
236 Message_Block[60] =
static_cast<unsigned char>( ( Length_Low >> 24 ) & 0xFF );
237 Message_Block[61] =
static_cast<unsigned char>( ( Length_Low >> 16 ) & 0xFF );
238 Message_Block[62] =
static_cast<unsigned char>( ( Length_Low >> 8 ) & 0xFF );
239 Message_Block[63] =
static_cast<unsigned char>( ( Length_Low ) & 0xFF );
245 unsigned SHA::shift(
int bits,
unsigned word )
247 return ( ( word << bits ) & 0xFFFFFFFF) | ( ( word & 0xFFFFFFFF ) >> ( 32-bits ) );