@@ -6,8 +6,21 @@ type HashMethod = (method: string, data: HashInput, outputEncoding?: BinaryToTex
66
77const nativeHash = 'hash' in crypto ? crypto . hash as HashMethod : null ;
88
9+ function sortObject ( o : any ) {
10+ if ( ! o || Array . isArray ( o ) || typeof o !== 'object' ) {
11+ return o ;
12+ }
13+ const keys = Object . keys ( o ) ;
14+ keys . sort ( ) ;
15+ const values : any [ ] = [ ] ;
16+ for ( const k of keys ) {
17+ values . push ( [ k , sortObject ( o [ k ] ) ] ) ;
18+ }
19+ return values ;
20+ }
21+
922/**
10- * hash
23+ * Hash
1124 *
1225 * @param {String } method hash method, e.g.: 'md5', 'sha1'
1326 * @param {String|Buffer|ArrayBuffer|TypedArray|DataView|Object } s input value
@@ -16,27 +29,23 @@ const nativeHash = 'hash' in crypto ? crypto.hash as HashMethod : null;
1629 * @public
1730 */
1831export function hash ( method : string , s : HashInput , format ?: BinaryToTextEncoding ) : string {
19- if ( s instanceof ArrayBuffer ) {
20- s = Buffer . from ( s ) ;
21- }
22- const isBuffer = Buffer . isBuffer ( s ) || ArrayBuffer . isView ( s ) ;
23- if ( ! isBuffer && typeof s === 'object' ) {
24- s = JSON . stringify ( sortObject ( s ) ) ;
25- }
32+ const input : HashInput = s instanceof ArrayBuffer ? Buffer . from ( s ) : s ;
33+ const isBuffer = Buffer . isBuffer ( input ) || ArrayBuffer . isView ( input ) ;
34+ const processedInput = ( ! isBuffer && typeof input === 'object' ) ? JSON . stringify ( sortObject ( input ) ) : input ;
2635
2736 if ( nativeHash ) {
28- // try to use crypto.hash first
37+ // Try to use crypto.hash first
2938 // https://nodejs.org/en/blog/release/v21.7.0#crypto-implement-cryptohash
30- return nativeHash ( method , s , format ) ;
39+ return nativeHash ( method , processedInput , format ) ;
3140 }
3241
3342 const sum = createHash ( method ) ;
34- sum . update ( s as string , isBuffer ? 'binary' : 'utf8' ) ;
43+ sum . update ( processedInput as string , isBuffer ? 'binary' : 'utf8' ) ;
3544 return sum . digest ( format || 'hex' ) ;
3645}
3746
3847/**
39- * md5 hash
48+ * Md5 hash
4049 *
4150 * @param {String|Buffer|Object } s input value
4251 * @param {String } [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
@@ -48,7 +57,7 @@ export function md5(s: HashInput, format?: BinaryToTextEncoding): string {
4857}
4958
5059/**
51- * sha1 hash
60+ * Sha1 hash
5261 *
5362 * @param {String|Buffer|Object } s input value
5463 * @param {String } [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
@@ -60,7 +69,7 @@ export function sha1(s: HashInput, format?: BinaryToTextEncoding): string {
6069}
6170
6271/**
63- * sha256 hash
72+ * Sha256 hash
6473 *
6574 * @param {String|Buffer|Object } s input value
6675 * @param {String } [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
@@ -72,7 +81,7 @@ export function sha256(s: HashInput, format?: BinaryToTextEncoding): string {
7281}
7382
7483/**
75- * sha512 hash
84+ * Sha512 hash
7685 *
7786 * @param {String|Buffer|Object } s input value
7887 * @param {String } [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
@@ -101,10 +110,9 @@ export function sha512(s: HashInput, format?: BinaryToTextEncoding): string {
101110 * @return {String } digest string.
102111 */
103112export function hmac ( algorithm : string , key : string , data : string | Buffer , encoding ?: BinaryToTextEncoding ) : string {
104- encoding = encoding || 'base64' ;
105- const hmac = createHmac ( algorithm , key ) ;
106- hmac . update ( data as string , Buffer . isBuffer ( data ) ? 'binary' : 'utf8' ) ;
107- return hmac . digest ( encoding ) ;
113+ const mac = createHmac ( algorithm , key ) ;
114+ mac . update ( data as string , Buffer . isBuffer ( data ) ? 'binary' : 'utf8' ) ;
115+ return mac . digest ( encoding || 'base64' ) ;
108116}
109117
110118/**
@@ -116,10 +124,8 @@ export function hmac(algorithm: string, key: string, data: string | Buffer, enco
116124 * @return {String } base64 encode format string.
117125 */
118126export function base64encode ( s : string | Buffer , urlSafe ?: boolean ) : string {
119- if ( ! Buffer . isBuffer ( s ) ) {
120- s = Buffer . from ( s ) ;
121- }
122- let encode = s . toString ( 'base64' ) ;
127+ const buf = Buffer . isBuffer ( s ) ? s : Buffer . from ( s ) ;
128+ let encode = buf . toString ( 'base64' ) ;
123129 if ( urlSafe ) {
124130 encode = encode . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) ;
125131 }
@@ -136,25 +142,10 @@ export function base64encode(s: string | Buffer, urlSafe?: boolean): string {
136142 * @return {String|Buffer } plain text.
137143 */
138144export function base64decode ( encodeStr : string , urlSafe ?: boolean , encoding ?: BufferEncoding | 'buffer' ) : string | Buffer {
139- if ( urlSafe ) {
140- encodeStr = encodeStr . replace ( / \- / g, '+' ) . replace ( / _ / g, '/' ) ;
141- }
142- const buf = Buffer . from ( encodeStr , 'base64' ) ;
145+ const str = urlSafe ? encodeStr . replace ( / \- / g, '+' ) . replace ( / _ / g, '/' ) : encodeStr ;
146+ const buf = Buffer . from ( str , 'base64' ) ;
143147 if ( encoding === 'buffer' ) {
144148 return buf ;
145149 }
146150 return buf . toString ( encoding || 'utf8' ) ;
147151}
148-
149- function sortObject ( o : any ) {
150- if ( ! o || Array . isArray ( o ) || typeof o !== 'object' ) {
151- return o ;
152- }
153- const keys = Object . keys ( o ) ;
154- keys . sort ( ) ;
155- const values : any [ ] = [ ] ;
156- for ( const k of keys ) {
157- values . push ( [ k , sortObject ( o [ k ] ) ] ) ;
158- }
159- return values ;
160- }
0 commit comments