@ -1,22 +1,16 @@
/ *
/ *
* js - sha256 v0 . 1. 1
* js - sha256 v0 . 1. 2
* https : //github.com/emn178/js-sha256
* https : //github.com/emn178/js-sha256
*
*
* Copyright 2014 , emn178 @ gmail . com
* Copyright 2014 - 2015 , emn178 @ gmail . com
*
*
* Licensed under the MIT license :
* Licensed under the MIT license :
* http : //www.opensource.org/licenses/MIT
* http : //www.opensource.org/licenses/MIT
* /
* /
; ( function ( root , undefined ) {
( function ( root , undefined ) {
'use strict' ;
'use strict' ;
var HEX _CHARS = [ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] ;
var HEX _CHARS = '0123456789abcdef' . split ( '' ) ;
var HEX _TABLE = {
'0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 , '8' : 8 , '9' : 9 ,
'a' : 10 , 'b' : 11 , 'c' : 12 , 'd' : 13 , 'e' : 14 , 'f' : 15 ,
'A' : 10 , 'B' : 11 , 'C' : 12 , 'D' : 13 , 'E' : 14 , 'F' : 15
} ;
var K = [ 0x428a2f98 , 0x71374491 , 0xb5c0fbcf , 0xe9b5dba5 , 0x3956c25b , 0x59f111f1 , 0x923f82a4 , 0xab1c5ed5 ,
var K = [ 0x428a2f98 , 0x71374491 , 0xb5c0fbcf , 0xe9b5dba5 , 0x3956c25b , 0x59f111f1 , 0x923f82a4 , 0xab1c5ed5 ,
0xd807aa98 , 0x12835b01 , 0x243185be , 0x550c7dc3 , 0x72be5d74 , 0x80deb1fe , 0x9bdc06a7 , 0xc19bf174 ,
0xd807aa98 , 0x12835b01 , 0x243185be , 0x550c7dc3 , 0x72be5d74 , 0x80deb1fe , 0x9bdc06a7 , 0xc19bf174 ,
@ -27,49 +21,51 @@
0x19a4c116 , 0x1e376c08 , 0x2748774c , 0x34b0bcb5 , 0x391c0cb3 , 0x4ed8aa4a , 0x5b9cca4f , 0x682e6ff3 ,
0x19a4c116 , 0x1e376c08 , 0x2748774c , 0x34b0bcb5 , 0x391c0cb3 , 0x4ed8aa4a , 0x5b9cca4f , 0x682e6ff3 ,
0x748f82ee , 0x78a5636f , 0x84c87814 , 0x8cc70208 , 0x90befffa , 0xa4506ceb , 0xbef9a3f7 , 0xc67178f2 ] ;
0x748f82ee , 0x78a5636f , 0x84c87814 , 0x8cc70208 , 0x90befffa , 0xa4506ceb , 0xbef9a3f7 , 0xc67178f2 ] ;
var sha256 = function ( message ) {
var sha256 = function ( message , asciiOnly ) {
return sha2 ( message , true );
return sha2 ( message , true , asciiOnly );
} ;
} ;
var sha224 = function ( message ) {
var sha224 = function ( message , asciiOnly ) {
return sha2 ( message , false );
return sha2 ( message , false , asciiOnly );
} ;
} ;
var sha2 = function ( message , is256 ) {
var sha2 = function ( message , is256 , asciiOnly ) {
if ( is256 === undefined )
if ( is256 === undefined ) {
is256 = true ;
is256 = true ;
}
var blocks = hasUTF8 ( message ) ? UTF8toBlocks ( message ) : ASCIItoBlocks ( message ) ;
var blocks , h0 , h1 , h2 , h3 , h4 , h5 , h6 , h7 ;
if ( is256 )
if ( ! asciiOnly && /[^\x00-\x7F]/ . test ( message ) ) {
{
blocks = getBlocksFromUtf8 ( message ) ;
var h0 = 0x6a09e667 ;
} else {
var h1 = 0xbb67ae85 ;
blocks = getBlocksFromAscii ( message ) ;
var h2 = 0x3c6ef372 ;
var h3 = 0xa54ff53a ;
var h4 = 0x510e527f ;
var h5 = 0x9b05688c ;
var h6 = 0x1f83d9ab ;
var h7 = 0x5be0cd19 ;
}
}
else // 224
if ( is256 ) {
{
h0 = 0x6a09e667 ;
var h0 = 0xc1059ed8 ;
h1 = 0xbb67ae85 ;
var h1 = 0x367cd507 ;
h2 = 0x3c6ef372 ;
var h2 = 0x3070dd17 ;
h3 = 0xa54ff53a ;
var h3 = 0xf70e5939 ;
h4 = 0x510e527f ;
var h4 = 0xffc00b31 ;
h5 = 0x9b05688c ;
var h5 = 0x68581511 ;
h6 = 0x1f83d9ab ;
var h6 = 0x64f98fa7 ;
h7 = 0x5be0cd19 ;
var h7 = 0xbefa4fa4 ;
} else { // 224
h0 = 0xc1059ed8 ;
h1 = 0x367cd507 ;
h2 = 0x3070dd17 ;
h3 = 0xf70e5939 ;
h4 = 0xffc00b31 ;
h5 = 0x68581511 ;
h6 = 0x64f98fa7 ;
h7 = 0xbefa4fa4 ;
}
}
for ( var i = 0 , length = blocks . length ; i < length ; i += 16 )
for ( var i = 0 , length = blocks . length ; i < length ; i += 16 ) {
{
var w = [ ] , s0 , s1 , j ;
var w = [ ] , s0 , s1 ;
for ( j = 0 ; j < 16 ; ++ j ) {
for ( var j = 0 ; j < 16 ; ++ j )
w [ j ] = blocks [ i + j ] ;
w [ j ] = blocks [ i + j ] ;
for ( var j = 16 ; j < 64 ; ++ j )
}
{
for ( j = 16 ; j < 64 ; ++ j ) {
s0 = rightrotate ( w [ j - 15 ] , 7 ) ^ rightrotate ( w [ j - 15 ] , 18 ) ^ ( w [ j - 15 ] >>> 3 ) ;
s0 = rightrotate ( w [ j - 15 ] , 7 ) ^ rightrotate ( w [ j - 15 ] , 18 ) ^ ( w [ j - 15 ] >>> 3 ) ;
s1 = rightrotate ( w [ j - 2 ] , 17 ) ^ rightrotate ( w [ j - 2 ] , 19 ) ^ ( w [ j - 2 ] >>> 10 ) ;
s1 = rightrotate ( w [ j - 2 ] , 17 ) ^ rightrotate ( w [ j - 2 ] , 19 ) ^ ( w [ j - 2 ] >>> 10 ) ;
w [ j ] = w [ j - 16 ] + s0 + w [ j - 7 ] + s1 ;
w [ j ] = w [ j - 16 ] + s0 + w [ j - 7 ] + s1 ;
@ -85,8 +81,7 @@
var h = h7 ;
var h = h7 ;
var maj , t1 , t2 , ch ;
var maj , t1 , t2 , ch ;
for ( var j = 0 ; j < 64 ; ++ j )
for ( j = 0 ; j < 64 ; ++ j ) {
{
s0 = rightrotate ( a , 2 ) ^ rightrotate ( a , 13 ) ^ rightrotate ( a , 22 ) ;
s0 = rightrotate ( a , 2 ) ^ rightrotate ( a , 13 ) ^ rightrotate ( a , 22 ) ;
maj = ( a & b ) ^ ( a & c ) ^ ( b & c ) ;
maj = ( a & b ) ^ ( a & c ) ^ ( b & c ) ;
t2 = s0 + maj ;
t2 = s0 + maj ;
@ -115,8 +110,9 @@
}
}
var hex = toHexString ( h0 ) + toHexString ( h1 ) + toHexString ( h2 ) + toHexString ( h3 ) + toHexString ( h4 ) + toHexString ( h5 ) + toHexString ( h6 ) ;
var hex = toHexString ( h0 ) + toHexString ( h1 ) + toHexString ( h2 ) + toHexString ( h3 ) + toHexString ( h4 ) + toHexString ( h5 ) + toHexString ( h6 ) ;
if ( is256 )
if ( is256 ) {
hex += toHexString ( h7 ) ;
hex += toHexString ( h7 ) ;
}
return hex ;
return hex ;
} ;
} ;
@ -125,69 +121,77 @@
} ;
} ;
var toHexString = function ( num ) {
var toHexString = function ( num ) {
var hex = "" ;
var hex = '' ;
for ( var i = 0 ; i < 4 ; i ++ )
for ( var i = 0 ; i < 4 ; i ++ ) {
{
var offset = 3 - i << 3 ;
var offset = 3 - i << 3 ;
hex += HEX _CHARS [ ( num >> ( offset + 4 ) ) & 0x0F ] + HEX _CHARS [ ( num >> offset ) & 0x0F ] ;
hex += HEX _CHARS [ ( num >> ( offset + 4 ) ) & 0x0F ] + HEX _CHARS [ ( num >> offset ) & 0x0F ] ;
}
}
return hex ;
return hex ;
} ;
} ;
var hasUTF8 = function ( message ) {
var getBytesFromUtf8 = function ( str ) {
var i = message . length ;
var bytes = [ ] , index = 0 ;
while ( i -- )
for ( var i = 0 ; i < str . length ; i ++ ) {
if ( message . charCodeAt ( i ) > 127 )
var c = str . charCodeAt ( i ) ;
return true ;
if ( c < 0x80 ) {
return false ;
bytes [ index ++ ] = c ;
} else if ( c < 0x800 ) {
bytes [ index ++ ] = 0xc0 | ( c >> 6 ) ;
bytes [ index ++ ] = 0x80 | ( c & 0x3f ) ;
} else if ( c < 0xd800 || c >= 0xe000 ) {
bytes [ index ++ ] = 0xe0 | ( c >> 12 ) ;
bytes [ index ++ ] = 0x80 | ( ( c >> 6 ) & 0x3f ) ;
bytes [ index ++ ] = 0x80 | ( c & 0x3f ) ;
} else {
c = 0x10000 + ( ( ( c & 0x3ff ) << 10 ) | ( str . charCodeAt ( ++ i ) & 0x3ff ) ) ;
bytes [ index ++ ] = 0xf0 | ( c >> 18 ) ;
bytes [ index ++ ] = 0x80 | ( ( c >> 12 ) & 0x3f ) ;
bytes [ index ++ ] = 0x80 | ( ( c >> 6 ) & 0x3f ) ;
bytes [ index ++ ] = 0x80 | ( c & 0x3f ) ;
}
}
return bytes ;
} ;
} ;
var ASCIItoBlocks = function ( message ) {
var getBlocksFromAscii = function ( message ) {
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
var length = message . length ;
var length = message . length ;
var chunkCount = ( ( length + 8 ) >> 6 ) + 1 ;
var chunkCount = ( ( length + 8 ) >> 6 ) + 1 ;
var blockCount = chunkCount << 4 ; // chunkCount * 16
var blockCount = chunkCount << 4 ; // chunkCount * 16
var blocks = [ ] ;
var blocks = [ ] , i ;
var i ;
for ( i = 0 ; i < blockCount ; ++ i ) {
for ( i = 0 ; i < blockCount ; ++ i )
blocks [ i ] = 0 ;
blocks [ i ] = 0 ;
for ( i = 0 ; i < length ; ++ i )
}
blocks [ i >> 2 ] |= message . charCodeAt ( i ) << ( 3 - ( i % 4 ) << 3 ) ;
for ( i = 0 ; i < length ; ++ i ) {
blocks [ i >> 2 ] |= 0x80 << ( 3 - ( i % 4 ) << 3 ) ;
blocks [ i >> 2 ] |= message . charCodeAt ( i ) << ( 3 - ( i & 3 ) << 3 ) ;
}
blocks [ i >> 2 ] |= 0x80 << ( 3 - ( i & 3 ) << 3 ) ;
blocks [ blockCount - 1 ] = length << 3 ; // length * 8
blocks [ blockCount - 1 ] = length << 3 ; // length * 8
return blocks ;
return blocks ;
} ;
} ;
var UTF8toBlocks = function ( message ) {
var getBlocksFromUtf8 = function ( message ) {
var uri = encodeURIComponent ( message ) ;
var bytes = getBytesFromUtf8 ( message ) ;
var blocks = [ ] ;
var length = bytes . length ;
for ( var i = 0 , bytes = 0 , length = uri . length ; i < length ; ++ i )
var chunkCount = ( ( length + 8 ) >> 6 ) + 1 ;
{
var c = uri . charCodeAt ( i ) ;
if ( c == 37 ) // %
blocks [ bytes >> 2 ] |= ( ( HEX _TABLE [ uri . charAt ( ++ i ) ] << 4 ) | HEX _TABLE [ uri . charAt ( ++ i ) ] ) << ( 3 - ( bytes % 4 ) << 3 ) ;
else
blocks [ bytes >> 2 ] |= c << ( 3 - ( bytes % 4 ) << 3 ) ;
++ bytes ;
}
var chunkCount = ( ( bytes + 8 ) >> 6 ) + 1 ;
var blockCount = chunkCount << 4 ; // chunkCount * 16
var blockCount = chunkCount << 4 ; // chunkCount * 16
var index = bytes >> 2 ;
var blocks = [ ] , i ;
blocks [ index ] |= 0x80 << ( 3 - ( bytes % 4 ) << 3 ) ;
for ( i = 0 ; i < blockCount ; ++ i ) {
for ( var i = index + 1 ; i < blockCount ; ++ i )
blocks [ i ] = 0 ;
blocks [ i ] = 0 ;
blocks [ blockCount - 1 ] = bytes << 3 ; // bytes * 8
}
for ( i = 0 ; i < length ; ++ i ) {
blocks [ i >> 2 ] |= bytes [ i ] << ( 3 - ( i & 3 ) << 3 ) ;
}
blocks [ i >> 2 ] |= 0x80 << ( 3 - ( i & 3 ) << 3 ) ;
blocks [ blockCount - 1 ] = length << 3 ; // length * 8
return blocks ;
return blocks ;
} ;
} ;
if ( typeof ( module ) != 'undefined' )
if ( typeof ( module ) != 'undefined' ) {
{
sha256 . sha256 = sha256 ;
sha256 . sha256 = sha256 ;
sha256 . sha224 = sha224 ;
sha256 . sha224 = sha224 ;
module . exports = sha256 ;
module . exports = sha256 ;
}
} else if ( root ) {
else if ( root )
{
root . sha256 = sha256 ;
root . sha256 = sha256 ;
root . sha224 = sha224 ;
root . sha224 = sha224 ;
}
}