37 #ifndef __INCLUDE_SHA__
38 #define __INCLUDE_SHA__
42 #ifndef __INCLUDE_MIST_CONF_H__
46 #ifndef __INCLUDE_MIST_ENDIAN__
50 #ifndef __INCLUDE_HASH_ALGORITHM__
81 void ToCurrentEndian( uint32 *x, size_type len )
83 for( size_type i = 0 ; i < len ; i++ )
89 void FromCurrentEndian( uint32 *x, size_type len )
91 for( size_type i = 0 ; i < len ; i++ )
97 void Round( uint32 &a, uint32 &b, uint32 &c, uint32 &d, uint32 &e, uint32 x[ 16 ] )
102 memcpy( W, x,
sizeof( uint32 ) * 16 );
104 for( i = 16 ; i < 80 ; i++ )
106 W[ i ] = R( W[ i - 3 ] ^ W[ i - 8 ] ^ W[ i - 14 ] ^ W[ i- 16 ], 1 );
116 for( i = 0 ; i < 20 ; i++ )
118 uint32 temp = R( A, 5 ) + ( ( B & C ) | ( ~B & D ) ) + E + W[ i ] + 0x5a827999;
126 for( ; i < 40 ; i++ )
128 uint32 temp = R( A, 5 ) + ( B ^ C ^ D ) + E + W[ i ] + 0x6ed9eba1;
136 for( ; i < 60 ; i++ )
138 uint32 temp = R( A, 5 ) + ( ( B & C ) | ( B & D ) | ( C & D ) ) + E + W[ i ] + 0x8f1bbcdc;
146 for( ; i < 80 ; i++ )
148 uint32 temp = R( A, 5 ) + ( B ^ C ^ D ) + E + W[ i ] + 0xca62c1d6;
165 virtual void compute_hash(
const void *bytes,
uint64 length )
172 uint32 &B = *
reinterpret_cast< uint32 *
>( digest + 4 );
173 uint32 &C = *
reinterpret_cast< uint32 *
>( digest + 8 );
174 uint32 &D = *
reinterpret_cast< uint32 *
>( digest + 12 );
175 uint32 &E = *
reinterpret_cast< uint32 *
>( digest + 16 );
178 const uint32 H0 = 0x67452301;
179 const uint32 H1 = 0xefcdab89;
180 const uint32 H2 = 0x98badcfe;
181 const uint32 H3 = 0x10325476;
182 const uint32 H4 = 0xc3d2e1f0;
193 const uint8 *data =
reinterpret_cast< const uint8 *
>( bytes );
196 for( i = 0 ; i + 64 < len ; i += 64 )
198 memcpy( xx, data + i,
sizeof(
uint8 ) * 64 );
199 ToCurrentEndian( x, 16 );
200 Round( A, B, C, D, E, x );
208 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
209 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 64 - rest ) );
214 ToCurrentEndian( x, 16 );
215 Round( A, B, C, D, E, x );
218 memset( xx, 0,
sizeof(
uint8 ) * 64 );
219 x[ 14 ] =
static_cast< uint32 >( length >> 32 );
220 x[ 15 ] =
static_cast< uint32 >( length );
223 Round( A, B, C, D, E, x );
227 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
228 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 64 - rest ) );
232 ToCurrentEndian( x, 16 );
235 x[ 14 ] =
static_cast< uint32 >( length >> 32 );
236 x[ 15 ] =
static_cast< uint32 >( length );
239 Round( A, B, C, D, E, x );
242 FromCurrentEndian( reinterpret_cast< uint32 * >( digest ), 5 );
247 virtual const std::string
name( )
const{
return(
"SHA1" ); }
251 sha1( ) :
base(
"da39a3ee5e6b4b0d3255bfef95601890afd80709" ){ }
254 sha1(
const std::string &str ) :
base( 20 ) { base::compute_hash( str ); }
279 uint32 R( uint32 a, uint32 s ){
return( a >> s ); }
280 uint32 Ch( uint32 x, uint32 y, uint32 z ){
return( ( x & y ) ^ ( ~x & z ) ); }
281 uint32 Maj( uint32 x, uint32 y, uint32 z ){
return( ( x & y ) ^ ( x & z ) ^ ( y & z ) ); }
282 uint32 S0( uint32 x ){
return( S( x, 2 ) ^ S( x, 13 ) ^ S( x, 22 ) ); }
283 uint32 S1( uint32 x ){
return( S( x, 6 ) ^ S( x, 11 ) ^ S( x, 25 ) ); }
284 uint32 s0( uint32 x ){
return( S( x, 7 ) ^ S( x, 18 ) ^ R( x, 3 ) ); }
285 uint32 s1( uint32 x ){
return( S( x, 17 ) ^ S( x, 19 ) ^ R( x, 10 ) ); }
287 void ToCurrentEndian( uint32 *x, size_type len )
289 for( size_type i = 0 ; i < len ; i++ )
291 x[ i ] =
to_current_endian( byte_array< uint32 >( x[ i ] ),
false ).get_value( );
295 void FromCurrentEndian( uint32 *x, size_type len )
297 for( size_type i = 0 ; i < len ; i++ )
303 void Round( uint32 *H, uint32 x[ 16 ] )
308 memcpy( W, x,
sizeof( uint32 ) * 16 );
310 for( i = 16 ; i < 64 ; i++ )
312 W[ i ] = s1( W[ i - 2 ] ) + W[ i - 7 ] + s0( W[ i - 15 ] ) + W[ i- 16 ];
326 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
327 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
328 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
329 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
330 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
331 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
332 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
333 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
337 for( i = 0 ; i < 64 ; i++ )
339 uint32 T1 = h + S1( e ) + Ch( e, f, g ) + K[ i ] + W[ i ];
340 uint32 T2 = S0( a ) + Maj( a, b, c );
363 virtual void compute_hash(
const void *bytes,
uint64 length )
384 const uint8 *data =
reinterpret_cast< const uint8 *
>( bytes );
387 for( i = 0 ; i + 64 < len ; i += 64 )
389 memcpy( xx, data + i,
sizeof(
uint8 ) * 64 );
390 ToCurrentEndian( x, 16 );
399 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
400 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 64 - rest ) );
405 ToCurrentEndian( x, 16 );
409 memset( xx, 0,
sizeof(
uint8 ) * 64 );
410 x[ 14 ] =
static_cast< uint32 >( length >> 32 );
411 x[ 15 ] =
static_cast< uint32 >( length );
418 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
419 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 64 - rest ) );
423 ToCurrentEndian( x, 16 );
426 x[ 14 ] =
static_cast< uint32 >( length >> 32 );
427 x[ 15 ] =
static_cast< uint32 >( length );
433 FromCurrentEndian( reinterpret_cast< uint32 * >( digest ), 8 );
438 virtual const std::string
name( )
const{
return(
"SHA-256" ); }
442 sha256( ) :
base(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ){ }
445 sha256(
const std::string &str ) :
base( 32 ) { base::compute_hash( str ); }
470 uint64 R(
const uint64 &a, uint32 s ){
return( a >> s ); }
471 uint64 Ch(
const uint64 &x,
const uint64 &y,
const uint64 &z ){
return( ( x & y ) ^ ( ~x & z ) ); }
472 uint64 Maj(
const uint64 &x,
const uint64 &y,
const uint64 &z ){
return( ( x & y ) ^ ( x & z ) ^ ( y & z ) ); }
473 uint64 S0(
const uint64 &x ){
return( S( x, 28 ) ^ S( x, 34 ) ^ S( x, 39 ) ); }
474 uint64 S1(
const uint64 &x ){
return( S( x, 14 ) ^ S( x, 18 ) ^ S( x, 41 ) ); }
475 uint64 s0(
const uint64 &x ){
return( S( x, 1 ) ^ S( x, 8 ) ^ R( x, 7 ) ); }
476 uint64 s1(
const uint64 &x ){
return( S( x, 19 ) ^ S( x, 61 ) ^ R( x, 6 ) ); }
478 void ToCurrentEndian( uint64 *x, size_type len )
480 for( size_type i = 0 ; i < len ; i++ )
482 x[ i ] =
to_current_endian( byte_array< uint64 >( x[ i ] ),
false ).get_value( );
486 void FromCurrentEndian( uint64 *x, size_type len )
488 for( size_type i = 0 ; i < len ; i++ )
494 void Round( uint64 *H, uint64 x[ 16 ] )
499 memcpy( W, x,
sizeof( uint64 ) * 16 );
501 for( i = 16 ; i < 80 ; i++ )
503 W[ i ] = s1( W[ i - 2 ] ) + W[ i - 7 ] + s0( W[ i - 15 ] ) + W[ i- 16 ];
516 static uint64 K[] = { 4794697086780616226ULL, 8158064640168781261ULL, 13096744586834688815ULL, 16840607885511220156ULL,
517 4131703408338449720ULL, 6480981068601479193ULL, 10538285296894168987ULL, 12329834152419229976ULL,
518 15566598209576043074ULL, 1334009975649890238ULL, 2608012711638119052ULL, 6128411473006802146ULL,
519 8268148722764581231ULL, 9286055187155687089ULL, 11230858885718282805ULL, 13951009754708518548ULL,
520 16472876342353939154ULL, 17275323862435702243ULL, 1135362057144423861ULL, 2597628984639134821ULL,
521 3308224258029322869ULL, 5365058923640841347ULL, 6679025012923562964ULL, 8573033837759648693ULL,
522 10970295158949994411ULL, 12119686244451234320ULL, 12683024718118986047ULL, 13788192230050041572ULL,
523 14330467153632333762ULL, 15395433587784984357ULL, 489312712824947311ULL, 1452737877330783856ULL,
524 2861767655752347644ULL, 3322285676063803686ULL, 5560940570517711597ULL, 5996557281743188959ULL,
525 7280758554555802590ULL, 8532644243296465576ULL, 9350256976987008742ULL, 10552545826968843579ULL,
526 11727347734174303076ULL, 12113106623233404929ULL, 14000437183269869457ULL, 14369950271660146224ULL,
527 15101387698204529176ULL, 15463397548674623760ULL, 17586052441742319658ULL, 1182934255886127544ULL,
528 1847814050463011016ULL, 2177327727835720531ULL, 2830643537854262169ULL, 3796741975233480872ULL,
529 4115178125766777443ULL, 5681478168544905931ULL, 6601373596472566643ULL, 7507060721942968483ULL,
530 8399075790359081724ULL, 8693463985226723168ULL, 9568029438360202098ULL, 10144078919501101548ULL,
531 10430055236837252648ULL, 11840083180663258601ULL, 13761210420658862357ULL, 14299343276471374635ULL,
532 14566680578165727644ULL, 15097957966210449927ULL, 16922976911328602910ULL, 17689382322260857208ULL,
533 500013540394364858ULL, 748580250866718886ULL, 1242879168328830382ULL, 1977374033974150939ULL,
534 2944078676154940804ULL, 3659926193048069267ULL, 4368137639120453308ULL, 4836135668995329356ULL,
535 5532061633213252278ULL, 6448918945643986474ULL, 6902733635092675308ULL, 7801388544844847127ULL };
539 for( i = 0 ; i < 80 ; i++ )
541 uint64 T1 = h + S1( e ) + Ch( e, f, g ) + K[ i ] + W[ i ];
542 uint64 T2 = S0( a ) + Maj( a, b, c );
565 virtual void compute_hash(
const void *bytes,
uint64 length )
574 H[ 0 ] = 14680500436340154072ULL;
575 H[ 1 ] = 7105036623409894663ULL;
576 H[ 2 ] = 10473403895298186519ULL;
577 H[ 3 ] = 1526699215303891257ULL;
578 H[ 4 ] = 7436329637833083697ULL;
579 H[ 5 ] = 10282925794625328401ULL;
580 H[ 6 ] = 15784041429090275239ULL;
581 H[ 7 ] = 5167115440072839076ULL;
586 const uint8 *data =
reinterpret_cast< const uint8 *
>( bytes );
589 for( i = 0 ; i + 128 < len ; i += 128 )
591 memcpy( xx, data + i,
sizeof(
uint8 ) * 128 );
592 ToCurrentEndian( x, 16 );
599 if( rest >= 128 - 16 )
601 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
602 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 128 - rest ) );
607 ToCurrentEndian( x, 16 );
611 memset( xx, 0,
sizeof(
uint8 ) * 128 );
620 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
621 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 128 - rest ) );
625 ToCurrentEndian( x, 16 );
635 FromCurrentEndian( H, 8 );
638 memcpy( digest, H, size( ) );
643 virtual const std::string
name( )
const{
return(
"SHA-384" ); }
647 sha384( ) :
base(
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" ){ }
650 sha384(
const std::string &str ) :
base( 48 ) { base::compute_hash( str ); }
675 uint64 R(
const uint64 &a, uint32 s ){
return( a >> s ); }
676 uint64 Ch(
const uint64 &x,
const uint64 &y,
const uint64 &z ){
return( ( x & y ) ^ ( ~x & z ) ); }
677 uint64 Maj(
const uint64 &x,
const uint64 &y,
const uint64 &z ){
return( ( x & y ) ^ ( x & z ) ^ ( y & z ) ); }
678 uint64 S0(
const uint64 &x ){
return( S( x, 28 ) ^ S( x, 34 ) ^ S( x, 39 ) ); }
679 uint64 S1(
const uint64 &x ){
return( S( x, 14 ) ^ S( x, 18 ) ^ S( x, 41 ) ); }
680 uint64 s0(
const uint64 &x ){
return( S( x, 1 ) ^ S( x, 8 ) ^ R( x, 7 ) ); }
681 uint64 s1(
const uint64 &x ){
return( S( x, 19 ) ^ S( x, 61 ) ^ R( x, 6 ) ); }
683 void ToCurrentEndian( uint64 *x, size_type len )
685 for( size_type i = 0 ; i < len ; i++ )
687 x[ i ] =
to_current_endian( byte_array< uint64 >( x[ i ] ),
false ).get_value( );
691 void FromCurrentEndian( uint64 *x, size_type len )
693 for( size_type i = 0 ; i < len ; i++ )
699 void Round( uint64 *H, uint64 x[ 16 ] )
704 memcpy( W, x,
sizeof( uint64 ) * 16 );
706 for( i = 16 ; i < 80 ; i++ )
708 W[ i ] = s1( W[ i - 2 ] ) + W[ i - 7 ] + s0( W[ i - 15 ] ) + W[ i- 16 ];
721 static uint64 K[] = { 4794697086780616226ULL, 8158064640168781261ULL, 13096744586834688815ULL, 16840607885511220156ULL,
722 4131703408338449720ULL, 6480981068601479193ULL, 10538285296894168987ULL, 12329834152419229976ULL,
723 15566598209576043074ULL, 1334009975649890238ULL, 2608012711638119052ULL, 6128411473006802146ULL,
724 8268148722764581231ULL, 9286055187155687089ULL, 11230858885718282805ULL, 13951009754708518548ULL,
725 16472876342353939154ULL, 17275323862435702243ULL, 1135362057144423861ULL, 2597628984639134821ULL,
726 3308224258029322869ULL, 5365058923640841347ULL, 6679025012923562964ULL, 8573033837759648693ULL,
727 10970295158949994411ULL, 12119686244451234320ULL, 12683024718118986047ULL, 13788192230050041572ULL,
728 14330467153632333762ULL, 15395433587784984357ULL, 489312712824947311ULL, 1452737877330783856ULL,
729 2861767655752347644ULL, 3322285676063803686ULL, 5560940570517711597ULL, 5996557281743188959ULL,
730 7280758554555802590ULL, 8532644243296465576ULL, 9350256976987008742ULL, 10552545826968843579ULL,
731 11727347734174303076ULL, 12113106623233404929ULL, 14000437183269869457ULL, 14369950271660146224ULL,
732 15101387698204529176ULL, 15463397548674623760ULL, 17586052441742319658ULL, 1182934255886127544ULL,
733 1847814050463011016ULL, 2177327727835720531ULL, 2830643537854262169ULL, 3796741975233480872ULL,
734 4115178125766777443ULL, 5681478168544905931ULL, 6601373596472566643ULL, 7507060721942968483ULL,
735 8399075790359081724ULL, 8693463985226723168ULL, 9568029438360202098ULL, 10144078919501101548ULL,
736 10430055236837252648ULL, 11840083180663258601ULL, 13761210420658862357ULL, 14299343276471374635ULL,
737 14566680578165727644ULL, 15097957966210449927ULL, 16922976911328602910ULL, 17689382322260857208ULL,
738 500013540394364858ULL, 748580250866718886ULL, 1242879168328830382ULL, 1977374033974150939ULL,
739 2944078676154940804ULL, 3659926193048069267ULL, 4368137639120453308ULL, 4836135668995329356ULL,
740 5532061633213252278ULL, 6448918945643986474ULL, 6902733635092675308ULL, 7801388544844847127ULL };
744 for( i = 0 ; i < 80 ; i++ )
746 uint64 T1 = h + S1( e ) + Ch( e, f, g ) + K[ i ] + W[ i ];
747 uint64 T2 = S0( a ) + Maj( a, b, c );
770 virtual void compute_hash(
const void *bytes,
uint64 length )
779 H[ 0 ] = 7640891576956012808ULL;
780 H[ 1 ] = 13503953896175478587ULL;
781 H[ 2 ] = 4354685564936845355ULL;
782 H[ 3 ] = 11912009170470909681ULL;
783 H[ 4 ] = 5840696475078001361ULL;
784 H[ 5 ] = 11170449401992604703ULL;
785 H[ 6 ] = 2270897969802886507ULL;
786 H[ 7 ] = 6620516959819538809ULL;
791 const uint8 *data =
reinterpret_cast< const uint8 *
>( bytes );
794 for( i = 0 ; i + 128 < len ; i += 128 )
796 memcpy( xx, data + i,
sizeof(
uint8 ) * 128 );
797 ToCurrentEndian( x, 16 );
804 if( rest >= 128 - 16 )
806 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
807 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 128 - rest ) );
812 ToCurrentEndian( x, 16 );
816 memset( xx, 0,
sizeof(
uint8 ) * 128 );
825 memcpy( xx, data + i,
sizeof(
uint8 ) * rest );
826 memset( xx + rest, 0,
sizeof(
uint8 ) * ( 128 - rest ) );
830 ToCurrentEndian( x, 16 );
840 FromCurrentEndian( reinterpret_cast< uint64 * >( digest ), 8 );
845 virtual const std::string
name( )
const{
return(
"SHA-512" ); }
849 sha512( ) :
base(
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ){ }
852 sha512(
const std::string &str ) :
base( 64 ) { base::compute_hash( str ); }
869 #endif // __INCLUDE_SHA__