@@ -14,12 +14,17 @@ const crypto = require('crypto');
14
14
assert . throws ( ( ) => h . update ( 'hello' ) , / ^ T y p e E r r o r : H m a c U p d a t e f a i l $ / ) ;
15
15
}
16
16
17
- // Test HMAC
18
- const h1 = crypto . createHmac ( 'sha1' , 'Node' )
19
- . update ( 'some data' )
20
- . update ( 'to hmac' )
21
- . digest ( 'hex' ) ;
22
- assert . strictEqual ( h1 , '19fd6e1ba73d9ed2224dd5094a71babe85d9a892' , 'test HMAC' ) ;
17
+ {
18
+ // Test HMAC
19
+ const actual = crypto . createHmac ( 'sha1' , 'Node' )
20
+ . update ( 'some data' )
21
+ . update ( 'to hmac' )
22
+ . digest ( 'hex' ) ;
23
+ const expected = '19fd6e1ba73d9ed2224dd5094a71babe85d9a892' ;
24
+ assert . strictEqual ( actual ,
25
+ expected ,
26
+ `Test HMAC: ${ actual } must be ${ expected } ` ) ;
27
+ }
23
28
24
29
// Test HMAC (Wikipedia Test Cases)
25
30
const wikipedia = [
@@ -70,12 +75,15 @@ for (let i = 0, l = wikipedia.length; i < l; i++) {
70
75
// FIPS does not support MD5.
71
76
if ( common . hasFipsCrypto && hash === 'md5' )
72
77
continue ;
73
- const result = crypto . createHmac ( hash , wikipedia [ i ] [ 'key' ] )
78
+ const expected = wikipedia [ i ] [ 'hmac' ] [ hash ] ;
79
+ const actual = crypto . createHmac ( hash , wikipedia [ i ] [ 'key' ] )
74
80
. update ( wikipedia [ i ] [ 'data' ] )
75
81
. digest ( 'hex' ) ;
76
- assert . strictEqual ( wikipedia [ i ] [ 'hmac' ] [ hash ] ,
77
- result ,
78
- `Test HMAC-${ hash } : Test case ${ i + 1 } wikipedia` ) ;
82
+ assert . strictEqual (
83
+ actual ,
84
+ expected ,
85
+ `Test HMAC-${ hash } wikipedia case ${ i + 1 } : ${ actual } must be ${ expected } `
86
+ ) ;
79
87
}
80
88
}
81
89
@@ -232,17 +240,20 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
232
240
const str = crypto . createHmac ( hash , rfc4231 [ i ] . key ) ;
233
241
str . end ( rfc4231 [ i ] . data ) ;
234
242
let strRes = str . read ( ) . toString ( 'hex' ) ;
235
- let result = crypto . createHmac ( hash , rfc4231 [ i ] [ 'key' ] )
243
+ let actual = crypto . createHmac ( hash , rfc4231 [ i ] [ 'key' ] )
236
244
. update ( rfc4231 [ i ] [ 'data' ] )
237
245
. digest ( 'hex' ) ;
238
246
if ( rfc4231 [ i ] [ 'truncate' ] ) {
239
- result = result . substr ( 0 , 32 ) ; // first 128 bits == 32 hex chars
247
+ actual = actual . substr ( 0 , 32 ) ; // first 128 bits == 32 hex chars
240
248
strRes = strRes . substr ( 0 , 32 ) ;
241
249
}
242
- assert . strictEqual ( rfc4231 [ i ] [ 'hmac' ] [ hash ] ,
243
- result ,
244
- `Test HMAC-${ hash } : Test case ${ i + 1 } rfc 4231` ) ;
245
- assert . strictEqual ( strRes , result , 'Should get same result from stream' ) ;
250
+ const expected = rfc4231 [ i ] [ 'hmac' ] [ hash ] ;
251
+ assert . strictEqual (
252
+ actual ,
253
+ expected ,
254
+ `Test HMAC-${ hash } rfc 4231 case ${ i + 1 } : ${ actual } must be ${ expected } `
255
+ ) ;
256
+ assert . strictEqual ( actual , strRes , 'Should get same result from stream' ) ;
246
257
}
247
258
}
248
259
@@ -357,22 +368,26 @@ const rfc2202_sha1 = [
357
368
358
369
if ( ! common . hasFipsCrypto ) {
359
370
for ( let i = 0 , l = rfc2202_md5 . length ; i < l ; i ++ ) {
371
+ const actual = crypto . createHmac ( 'md5' , rfc2202_md5 [ i ] [ 'key' ] )
372
+ . update ( rfc2202_md5 [ i ] [ 'data' ] )
373
+ . digest ( 'hex' ) ;
374
+ const expected = rfc2202_md5 [ i ] [ 'hmac' ] ;
360
375
assert . strictEqual (
361
- rfc2202_md5 [ i ] [ 'hmac' ] ,
362
- crypto . createHmac ( 'md5' , rfc2202_md5 [ i ] [ 'key' ] )
363
- . update ( rfc2202_md5 [ i ] [ 'data' ] )
364
- . digest ( 'hex' ) ,
365
- `Test HMAC-MD5 : Test case ${ i + 1 } rfc 2202`
376
+ actual ,
377
+ expected ,
378
+ `Test HMAC-MD5 rfc 2202 case ${ i + 1 } : ${ actual } must be ${ expected } `
366
379
) ;
367
380
}
368
381
}
369
382
for ( let i = 0 , l = rfc2202_sha1 . length ; i < l ; i ++ ) {
383
+ const actual = crypto . createHmac ( 'sha1' , rfc2202_sha1 [ i ] [ 'key' ] )
384
+ . update ( rfc2202_sha1 [ i ] [ 'data' ] )
385
+ . digest ( 'hex' ) ;
386
+ const expected = rfc2202_sha1 [ i ] [ 'hmac' ] ;
370
387
assert . strictEqual (
371
- rfc2202_sha1 [ i ] [ 'hmac' ] ,
372
- crypto . createHmac ( 'sha1' , rfc2202_sha1 [ i ] [ 'key' ] )
373
- . update ( rfc2202_sha1 [ i ] [ 'data' ] )
374
- . digest ( 'hex' ) ,
375
- `Test HMAC-SHA1 : Test case ${ i + 1 } rfc 2202`
388
+ actual ,
389
+ expected ,
390
+ `Test HMAC-SHA1 rfc 2202 case ${ i + 1 } : ${ actual } must be ${ expected } `
376
391
) ;
377
392
}
378
393
0 commit comments