@ -1,20 +1,32 @@
/ *
* [ js - sha1 ] { @ link https : //github.com/emn178/js-sha1}
*
* @ version 0. 4 . 0
* @ version 0. 7 . 0
* @ author Chen , Yi - Cyuan [ emn178 @ gmail . com ]
* @ copyright Chen , Yi - Cyuan 2014 - 20 16
* @ copyright Chen , Yi - Cyuan 2014 - 20 24
* @ license MIT
* /
( function ( root ) {
/*jslint bitwise: true */
( function ( ) {
'use strict' ;
var NODE _JS = typeof process == 'object' && process . versions && process . versions . node ;
var INPUT _ERROR = 'input is invalid type' ;
var FINALIZE _ERROR = 'finalize already called' ;
var WINDOW = typeof window === 'object' ;
var root = WINDOW ? window : { } ;
if ( root . JS _SHA1 _NO _WINDOW ) {
WINDOW = false ;
}
var WEB _WORKER = ! WINDOW && typeof self === 'object' ;
var NODE _JS = ! root . JS _SHA1 _NO _NODE _JS && typeof process === 'object' && process . versions && process . versions . node ;
if ( NODE _JS ) {
root = global ;
} else if ( WEB _WORKER ) {
root = self ;
}
var COMMON _JS = ! root . JS _SHA1 _TEST && typeof module == 'object' && module . exports ;
var AMD = typeof define == 'function' && define . amd ;
var COMMON _JS = ! root . JS _SHA1 _NO _COMMON _JS && typeof module === 'object' && module . exports ;
var AMD = typeof define === 'function' && define . amd ;
var ARRAY _BUFFER = ! root . JS _SHA1 _NO _ARRAY _BUFFER && typeof ArrayBuffer !== 'undefined' ;
var HEX _CHARS = '0123456789abcdef' . split ( '' ) ;
var EXTRA = [ - 2147483648 , 8388608 , 32768 , 128 ] ;
var SHIFT = [ 24 , 16 , 8 , 0 ] ;
@ -22,6 +34,38 @@
var blocks = [ ] ;
var isArray = Array . isArray ;
if ( root . JS _SHA1 _NO _NODE _JS || ! isArray ) {
isArray = function ( obj ) {
return Object . prototype . toString . call ( obj ) === '[object Array]' ;
} ;
}
var isView = ArrayBuffer . isView ;
if ( ARRAY _BUFFER && ( root . JS _SHA1 _NO _ARRAY _BUFFER _IS _VIEW || ! isView ) ) {
isView = function ( obj ) {
return typeof obj === 'object' && obj . buffer && obj . buffer . constructor === ArrayBuffer ;
} ;
}
// [message: string, isString: bool]
var formatMessage = function ( message ) {
var type = typeof message ;
if ( type === 'string' ) {
return [ message , true ] ;
}
if ( type !== 'object' || message === null ) {
throw new Error ( INPUT _ERROR ) ;
}
if ( ARRAY _BUFFER && message . constructor === ArrayBuffer ) {
return [ new Uint8Array ( message ) , false ] ;
}
if ( ! isArray ( message ) && ! isView ( message ) ) {
throw new Error ( INPUT _ERROR ) ;
}
return [ message , false ] ;
}
var createOutputMethod = function ( outputType ) {
return function ( message ) {
return new Sha1 ( true ) . update ( message ) [ outputType ] ( ) ;
@ -47,30 +91,57 @@
} ;
var nodeWrap = function ( method ) {
var crypto , Buffer ;
try {
if ( root . JS _SHA1 _TEST ) {
throw 'JS_SHA1_TEST' ;
}
crypto = require ( 'crypto' ) ;
Buffer = require ( 'buffer' ) . Buffer ;
} catch ( e ) {
console . log ( e ) ;
return method ;
var crypto = require ( 'crypto' )
var Buffer = require ( 'buffer' ) . Buffer ;
var bufferFrom ;
if ( Buffer . from && ! root . JS _SHA1 _NO _BUFFER _FROM ) {
bufferFrom = Buffer . from ;
} else {
bufferFrom = function ( message ) {
return new Buffer ( message ) ;
} ;
}
var nodeMethod = function ( message ) {
if ( typeof message == 'string' ) {
if ( typeof message == = 'string' ) {
return crypto . createHash ( 'sha1' ) . update ( message , 'utf8' ) . digest ( 'hex' ) ;
} else if ( message . constructor == ArrayBuffer ) {
} else {
if ( message === null || message === undefined ) {
throw new Error ( INPUT _ERROR ) ;
} else if ( message . constructor === ArrayBuffer ) {
message = new Uint8Array ( message ) ;
} else if ( message . length === undefined ) {
}
}
if ( isArray ( message ) || isView ( message ) ||
message . constructor === Buffer ) {
return crypto . createHash ( 'sha1' ) . update ( bufferFrom ( message ) ) . digest ( 'hex' ) ;
} else {
return method ( message ) ;
}
return crypto . createHash ( 'sha1' ) . update ( new Buffer ( message ) ) . digest ( 'hex' ) ;
} ;
return nodeMethod ;
} ;
var createHmacOutputMethod = function ( outputType ) {
return function ( key , message ) {
return new HmacSha1 ( key , true ) . update ( message ) [ outputType ] ( ) ;
} ;
} ;
var createHmacMethod = function ( ) {
var method = createHmacOutputMethod ( 'hex' ) ;
method . create = function ( key ) {
return new HmacSha1 ( key ) ;
} ;
method . update = function ( key , message ) {
return method . create ( key ) . update ( message ) ;
} ;
for ( var i = 0 ; i < OUTPUT _TYPES . length ; ++ i ) {
var type = OUTPUT _TYPES [ i ] ;
method [ type ] = createHmacOutputMethod ( type ) ;
}
return method ;
} ;
function Sha1 ( sharedMemory ) {
if ( sharedMemory ) {
blocks [ 0 ] = blocks [ 16 ] = blocks [ 1 ] = blocks [ 2 ] = blocks [ 3 ] =
@ -88,54 +159,54 @@
this . h3 = 0x10325476 ;
this . h4 = 0xC3D2E1F0 ;
this . block = this . start = this . bytes = 0 ;
this . block = this . start = this . bytes = this . hBytes = 0 ;
this . finalized = this . hashed = false ;
this . first = true ;
}
Sha1 . prototype . update = function ( message ) {
if ( this . finalized ) {
return ;
}
var notString = typeof ( message ) != 'string' ;
if ( notString && message . constructor == root . ArrayBuffer ) {
message = new Uint8Array ( message ) ;
throw new Error ( FINALIZE _ERROR ) ;
}
var result = formatMessage ( message ) ;
message = result [ 0 ] ;
var isString = result [ 1 ] ;
var code , index = 0 , i , length = message . length || 0 , blocks = this . blocks ;
while ( index < length ) {
if ( this . hashed ) {
this . hashed = false ;
blocks [ 0 ] = this . block ;
blocks [ 16 ] = blocks [ 1 ] = blocks [ 2 ] = blocks [ 3 ] =
this . block = blocks [ 16 ] = blocks [ 1 ] = blocks [ 2 ] = blocks [ 3 ] =
blocks [ 4 ] = blocks [ 5 ] = blocks [ 6 ] = blocks [ 7 ] =
blocks [ 8 ] = blocks [ 9 ] = blocks [ 10 ] = blocks [ 11 ] =
blocks [ 12 ] = blocks [ 13 ] = blocks [ 14 ] = blocks [ 15 ] = 0 ;
}
if ( notString ) {
for ( i = this . start ; index < length && i < 64 ; ++ index ) {
blocks [ i >> 2 ] |= message [ index ] << SHIFT [ i ++ & 3 ] ;
}
} else {
if ( isString ) {
for ( i = this . start ; index < length && i < 64 ; ++ index ) {
code = message . charCodeAt ( index ) ;
if ( code < 0x80 ) {
blocks [ i >> 2 ] |= code << SHIFT [ i ++ & 3 ] ;
blocks [ i >>> 2 ] |= code << SHIFT [ i ++ & 3 ] ;
} else if ( code < 0x800 ) {
blocks [ i >> 2 ] |= ( 0xc0 | ( code >> 6 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> > 2 ] |= ( 0xc0 | ( code > >> 6 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> > 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
} else if ( code < 0xd800 || code >= 0xe000 ) {
blocks [ i >> 2 ] |= ( 0xe0 | ( code >> 12 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( ( code >> 6 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> > 2 ] |= ( 0xe0 | ( code > >> 12 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> > 2 ] |= ( 0x80 | ( ( code > >> 6 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> > 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
} else {
code = 0x10000 + ( ( ( code & 0x3ff ) << 10 ) | ( message . charCodeAt ( ++ index ) & 0x3ff ) ) ;
blocks [ i >> 2 ] |= ( 0xf0 | ( code >> 18 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( ( code >> 12 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( ( code >> 6 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >> 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >>> 2 ] |= ( 0xf0 | ( code >>> 18 ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >>> 2 ] |= ( 0x80 | ( ( code >>> 12 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >>> 2 ] |= ( 0x80 | ( ( code >>> 6 ) & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
blocks [ i >>> 2 ] |= ( 0x80 | ( code & 0x3f ) ) << SHIFT [ i ++ & 3 ] ;
}
}
} else {
for ( i = this . start ; index < length && i < 64 ; ++ index ) {
blocks [ i >>> 2 ] |= message [ index ] << SHIFT [ i ++ & 3 ] ;
}
}
@ -150,6 +221,10 @@
this . start = i ;
}
}
if ( this . bytes > 4294967295 ) {
this . hBytes += this . bytes / 4294967296 << 0 ;
this . bytes = this . bytes % 4294967296 ;
}
return this ;
} ;
@ -160,7 +235,7 @@
this . finalized = true ;
var blocks = this . blocks , i = this . lastByteIndex ;
blocks [ 16 ] = this . block ;
blocks [ i >> 2 ] |= EXTRA [ i & 3 ] ;
blocks [ i >> > 2 ] |= EXTRA [ i & 3 ] ;
this . block = blocks [ 16 ] ;
if ( i >= 56 ) {
if ( ! this . hashed ) {
@ -172,6 +247,7 @@
blocks [ 8 ] = blocks [ 9 ] = blocks [ 10 ] = blocks [ 11 ] =
blocks [ 12 ] = blocks [ 13 ] = blocks [ 14 ] = blocks [ 15 ] = 0 ;
}
blocks [ 14 ] = this . hBytes << 3 | this . bytes >>> 29 ;
blocks [ 15 ] = this . bytes << 3 ;
this . hash ( ) ;
} ;
@ -305,26 +381,26 @@
var h0 = this . h0 , h1 = this . h1 , h2 = this . h2 , h3 = this . h3 , h4 = this . h4 ;
return HEX _CHARS [ ( h0 >> 28 ) & 0x0F ] + HEX _CHARS [ ( h0 >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> 20 ) & 0x0F ] + HEX _CHARS [ ( h0 >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> 12 ) & 0x0F ] + HEX _CHARS [ ( h0 >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> 4 ) & 0x0F ] + HEX _CHARS [ h0 & 0x0F ] +
HEX _CHARS [ ( h1 >> 28 ) & 0x0F ] + HEX _CHARS [ ( h1 >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> 20 ) & 0x0F ] + HEX _CHARS [ ( h1 >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> 12 ) & 0x0F ] + HEX _CHARS [ ( h1 >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> 4 ) & 0x0F ] + HEX _CHARS [ h1 & 0x0F ] +
HEX _CHARS [ ( h2 >> 28 ) & 0x0F ] + HEX _CHARS [ ( h2 >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> 20 ) & 0x0F ] + HEX _CHARS [ ( h2 >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> 12 ) & 0x0F ] + HEX _CHARS [ ( h2 >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> 4 ) & 0x0F ] + HEX _CHARS [ h2 & 0x0F ] +
HEX _CHARS [ ( h3 >> 28 ) & 0x0F ] + HEX _CHARS [ ( h3 >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> 20 ) & 0x0F ] + HEX _CHARS [ ( h3 >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> 12 ) & 0x0F ] + HEX _CHARS [ ( h3 >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> 4 ) & 0x0F ] + HEX _CHARS [ h3 & 0x0F ] +
HEX _CHARS [ ( h4 >> 28 ) & 0x0F ] + HEX _CHARS [ ( h4 >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> 20 ) & 0x0F ] + HEX _CHARS [ ( h4 >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> 12 ) & 0x0F ] + HEX _CHARS [ ( h4 >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> 4 ) & 0x0F ] + HEX _CHARS [ h4 & 0x0F ] ;
return HEX _CHARS [ ( h0 >> > 28 ) & 0x0F ] + HEX _CHARS [ ( h0 > >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> > 20 ) & 0x0F ] + HEX _CHARS [ ( h0 > >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> > 12 ) & 0x0F ] + HEX _CHARS [ ( h0 > >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h0 >> > 4 ) & 0x0F ] + HEX _CHARS [ h0 & 0x0F ] +
HEX _CHARS [ ( h1 >> > 28 ) & 0x0F ] + HEX _CHARS [ ( h1 > >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> > 20 ) & 0x0F ] + HEX _CHARS [ ( h1 > >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> > 12 ) & 0x0F ] + HEX _CHARS [ ( h1 > >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h1 >> > 4 ) & 0x0F ] + HEX _CHARS [ h1 & 0x0F ] +
HEX _CHARS [ ( h2 >> > 28 ) & 0x0F ] + HEX _CHARS [ ( h2 > >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> > 20 ) & 0x0F ] + HEX _CHARS [ ( h2 > >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> > 12 ) & 0x0F ] + HEX _CHARS [ ( h2 > >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h2 >> > 4 ) & 0x0F ] + HEX _CHARS [ h2 & 0x0F ] +
HEX _CHARS [ ( h3 >> > 28 ) & 0x0F ] + HEX _CHARS [ ( h3 > >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> > 20 ) & 0x0F ] + HEX _CHARS [ ( h3 > >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> > 12 ) & 0x0F ] + HEX _CHARS [ ( h3 > >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h3 >> > 4 ) & 0x0F ] + HEX _CHARS [ h3 & 0x0F ] +
HEX _CHARS [ ( h4 >> > 28 ) & 0x0F ] + HEX _CHARS [ ( h4 > >> 24 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> > 20 ) & 0x0F ] + HEX _CHARS [ ( h4 > >> 16 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> > 12 ) & 0x0F ] + HEX _CHARS [ ( h4 > >> 8 ) & 0x0F ] +
HEX _CHARS [ ( h4 >> > 4 ) & 0x0F ] + HEX _CHARS [ h4 & 0x0F ] ;
} ;
Sha1 . prototype . toString = Sha1 . prototype . hex ;
@ -335,11 +411,11 @@
var h0 = this . h0 , h1 = this . h1 , h2 = this . h2 , h3 = this . h3 , h4 = this . h4 ;
return [
( h0 >> 24 ) & 0xFF , ( h0 >> 16 ) & 0xFF , ( h0 >> 8 ) & 0xFF , h0 & 0xFF ,
( h1 >> 24 ) & 0xFF , ( h1 >> 16 ) & 0xFF , ( h1 >> 8 ) & 0xFF , h1 & 0xFF ,
( h2 >> 24 ) & 0xFF , ( h2 >> 16 ) & 0xFF , ( h2 >> 8 ) & 0xFF , h2 & 0xFF ,
( h3 >> 24 ) & 0xFF , ( h3 >> 16 ) & 0xFF , ( h3 >> 8 ) & 0xFF , h3 & 0xFF ,
( h4 >> 24 ) & 0xFF , ( h4 >> 16 ) & 0xFF , ( h4 >> 8 ) & 0xFF , h4 & 0xFF
( h0 >> > 24 ) & 0xFF , ( h0 > >> 16 ) & 0xFF , ( h0 > >> 8 ) & 0xFF , h0 & 0xFF ,
( h1 >> > 24 ) & 0xFF , ( h1 > >> 16 ) & 0xFF , ( h1 > >> 8 ) & 0xFF , h1 & 0xFF ,
( h2 >> > 24 ) & 0xFF , ( h2 > >> 16 ) & 0xFF , ( h2 > >> 8 ) & 0xFF , h2 & 0xFF ,
( h3 >> > 24 ) & 0xFF , ( h3 > >> 16 ) & 0xFF , ( h3 > >> 8 ) & 0xFF , h3 & 0xFF ,
( h4 >> > 24 ) & 0xFF , ( h4 > >> 16 ) & 0xFF , ( h4 > >> 8 ) & 0xFF , h4 & 0xFF
] ;
} ;
@ -358,7 +434,68 @@
return buffer ;
} ;
function HmacSha1 ( key , sharedMemory ) {
var i , result = formatMessage ( key ) ;
key = result [ 0 ] ;
if ( result [ 1 ] ) {
var bytes = [ ] , length = key . length , index = 0 , code ;
for ( i = 0 ; i < length ; ++ i ) {
code = key . charCodeAt ( i ) ;
if ( code < 0x80 ) {
bytes [ index ++ ] = code ;
} else if ( code < 0x800 ) {
bytes [ index ++ ] = ( 0xc0 | ( code >>> 6 ) ) ;
bytes [ index ++ ] = ( 0x80 | ( code & 0x3f ) ) ;
} else if ( code < 0xd800 || code >= 0xe000 ) {
bytes [ index ++ ] = ( 0xe0 | ( code >>> 12 ) ) ;
bytes [ index ++ ] = ( 0x80 | ( ( code >>> 6 ) & 0x3f ) ) ;
bytes [ index ++ ] = ( 0x80 | ( code & 0x3f ) ) ;
} else {
code = 0x10000 + ( ( ( code & 0x3ff ) << 10 ) | ( key . charCodeAt ( ++ i ) & 0x3ff ) ) ;
bytes [ index ++ ] = ( 0xf0 | ( code >>> 18 ) ) ;
bytes [ index ++ ] = ( 0x80 | ( ( code >>> 12 ) & 0x3f ) ) ;
bytes [ index ++ ] = ( 0x80 | ( ( code >>> 6 ) & 0x3f ) ) ;
bytes [ index ++ ] = ( 0x80 | ( code & 0x3f ) ) ;
}
}
key = bytes ;
}
if ( key . length > 64 ) {
key = ( new Sha1 ( true ) ) . update ( key ) . array ( ) ;
}
var oKeyPad = [ ] , iKeyPad = [ ] ;
for ( i = 0 ; i < 64 ; ++ i ) {
var b = key [ i ] || 0 ;
oKeyPad [ i ] = 0x5c ^ b ;
iKeyPad [ i ] = 0x36 ^ b ;
}
Sha1 . call ( this , sharedMemory ) ;
this . update ( iKeyPad ) ;
this . oKeyPad = oKeyPad ;
this . inner = true ;
this . sharedMemory = sharedMemory ;
}
HmacSha1 . prototype = new Sha1 ( ) ;
HmacSha1 . prototype . finalize = function ( ) {
Sha1 . prototype . finalize . call ( this ) ;
if ( this . inner ) {
this . inner = false ;
var innerHash = this . array ( ) ;
Sha1 . call ( this , this . sharedMemory ) ;
this . update ( this . oKeyPad ) ;
this . update ( innerHash ) ;
Sha1 . prototype . finalize . call ( this ) ;
}
} ;
var exports = createMethod ( ) ;
exports . sha1 = exports ;
exports . sha1 . hmac = createHmacMethod ( ) ;
if ( COMMON _JS ) {
module . exports = exports ;
@ -370,4 +507,4 @@
} ) ;
}
}
} (this ) ) ;
} )( ) ;