3
3
// to use buffers by default.
4
4
5
5
6
- var common = require ( '../common' ) ;
7
- var assert = require ( 'assert' ) ;
8
- var constants = require ( 'constants' ) ;
6
+ const common = require ( '../common' ) ;
9
7
10
8
if ( ! common . hasCrypto ) {
11
9
common . skip ( 'missing crypto' ) ;
12
10
return ;
13
11
}
14
- var crypto = require ( 'crypto' ) ;
15
- var tls = require ( 'tls' ) ;
16
12
17
- crypto . DEFAULT_ENCODING = 'binary' ;
13
+ const assert = require ( 'assert' ) ;
14
+ const constants = require ( 'constants' ) ;
15
+ const crypto = require ( 'crypto' ) ;
16
+ const fs = require ( 'fs' ) ;
17
+ const path = require ( 'path' ) ;
18
+ const tls = require ( 'tls' ) ;
18
19
19
- var fs = require ( 'fs' ) ;
20
- var path = require ( 'path' ) ;
20
+ crypto . DEFAULT_ENCODING = 'binary' ;
21
21
22
22
// Test Certificates
23
- var certPem = fs . readFileSync ( common . fixturesDir + '/test_cert.pem' , 'ascii' ) ;
24
- var certPfx = fs . readFileSync ( common . fixturesDir + '/test_cert.pfx' ) ;
25
- var keyPem = fs . readFileSync ( common . fixturesDir + '/test_key.pem' , 'ascii' ) ;
26
- var rsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_pubkey.pem' ,
27
- 'ascii' ) ;
28
- var rsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_privkey.pem' ,
29
- 'ascii' ) ;
23
+ const certPem = fs . readFileSync ( common . fixturesDir + '/test_cert.pem' , 'ascii' ) ;
24
+ const certPfx = fs . readFileSync ( common . fixturesDir + '/test_cert.pfx' ) ;
25
+ const keyPem = fs . readFileSync ( common . fixturesDir + '/test_key.pem' , 'ascii' ) ;
26
+ const rsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_pubkey.pem' ,
27
+ 'ascii' ) ;
28
+ const rsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_privkey.pem' ,
29
+ 'ascii' ) ;
30
30
31
31
// PFX tests
32
32
assert . doesNotThrow ( function ( ) {
@@ -35,22 +35,22 @@ assert.doesNotThrow(function() {
35
35
36
36
assert . throws ( function ( ) {
37
37
tls . createSecureContext ( { pfx : certPfx } ) ;
38
- } , ' mac verify failure' ) ;
38
+ } , / ^ E r r o r : m a c v e r i f y f a i l u r e $ / ) ;
39
39
40
40
assert . throws ( function ( ) {
41
41
tls . createSecureContext ( { pfx : certPfx , passphrase : 'test' } ) ;
42
- } , ' mac verify failure' ) ;
42
+ } , / ^ E r r o r : m a c v e r i f y f a i l u r e $ / ) ;
43
43
44
44
assert . throws ( function ( ) {
45
45
tls . createSecureContext ( { pfx : 'sample' , passphrase : 'test' } ) ;
46
- } , ' not enough data' ) ;
46
+ } , / ^ E r r o r : n o t e n o u g h d a t a $ / ) ;
47
47
48
48
// Test HMAC
49
49
const hmacHash = crypto . createHmac ( 'sha1' , 'Node' )
50
50
. update ( 'some data' )
51
51
. update ( 'to hmac' )
52
52
. digest ( 'hex' ) ;
53
- assert . equal ( hmacHash , '19fd6e1ba73d9ed2224dd5094a71babe85d9a892' , 'test HMAC ') ;
53
+ assert . strictEqual ( hmacHash , '19fd6e1ba73d9ed2224dd5094a71babe85d9a892' ) ;
54
54
55
55
// Test HMAC-SHA-* (rfc 4231 Test Cases)
56
56
var rfc4231 = [
@@ -207,9 +207,11 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
207
207
if ( rfc4231 [ i ] [ 'truncate' ] ) {
208
208
result = result . substr ( 0 , 32 ) ; // first 128 bits == 32 hex chars
209
209
}
210
- assert . equal ( rfc4231 [ i ] [ 'hmac' ] [ hash ] ,
211
- result ,
212
- 'Test HMAC-' + hash + ': Test case ' + ( i + 1 ) + ' rfc 4231' ) ;
210
+ assert . strictEqual (
211
+ rfc4231 [ i ] [ 'hmac' ] [ hash ] ,
212
+ result ,
213
+ `Test HMAC-${ hash } : Test case ${ i + 1 } rfc 4231`
214
+ ) ;
213
215
}
214
216
}
215
217
@@ -328,15 +330,15 @@ for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
328
330
crypto . createHmac ( 'md5' , rfc2202_md5 [ i ] [ 'key' ] )
329
331
. update ( rfc2202_md5 [ i ] [ 'data' ] )
330
332
. digest ( 'hex' ) ,
331
- ' Test HMAC-MD5 : Test case ' + ( i + 1 ) + ' rfc 2202' ) ;
333
+ ` Test HMAC-MD5 : Test case ${ i + 1 } rfc 2202` ) ;
332
334
}
333
335
}
334
336
for ( let i = 0 , l = rfc2202_sha1 . length ; i < l ; i ++ ) {
335
337
assert . equal ( rfc2202_sha1 [ i ] [ 'hmac' ] ,
336
338
crypto . createHmac ( 'sha1' , rfc2202_sha1 [ i ] [ 'key' ] )
337
339
. update ( rfc2202_sha1 [ i ] [ 'data' ] )
338
340
. digest ( 'hex' ) ,
339
- ' Test HMAC-SHA1 : Test case ' + ( i + 1 ) + ' rfc 2202' ) ;
341
+ ` Test HMAC-SHA1 : Test case ${ i + 1 } rfc 2202` ) ;
340
342
}
341
343
342
344
// Test hashing
@@ -347,24 +349,27 @@ var a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
347
349
348
350
if ( ! common . hasFipsCrypto ) {
349
351
var a0 = crypto . createHash ( 'md5' ) . update ( 'Test123' ) . digest ( 'binary' ) ;
350
- assert . equal (
352
+ assert . strictEqual (
351
353
a0 ,
352
354
'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca\u00bd\u008c' ,
353
355
'Test MD5 as binary'
354
356
) ;
355
357
}
356
358
357
- assert . equal ( a1 , '8308651804facb7b9af8ffc53a33a22d6a1c8ac2' , 'Test SHA1' ) ;
359
+ assert . strictEqual ( a1 , '8308651804facb7b9af8ffc53a33a22d6a1c8ac2' , 'Test SHA1' ) ;
358
360
359
- assert . equal ( a2 , '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=' ,
360
- 'Test SHA256 as base64' ) ;
361
+ assert . strictEqual ( a2 , '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=' ,
362
+ 'Test SHA256 as base64' ) ;
361
363
362
- assert . equal ( a3 , '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
363
- '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
364
- '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
365
- '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
366
- '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'' ,
367
- 'Test SHA512 as assumed binary' ) ;
364
+ assert . strictEqual (
365
+ a3 ,
366
+ '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
367
+ '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
368
+ '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
369
+ '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
370
+ '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'' ,
371
+ 'Test SHA512 as assumed binary'
372
+ ) ;
368
373
369
374
assert . deepEqual ( a4 ,
370
375
new Buffer ( '8308651804facb7b9af8ffc53a33a22d6a1c8ac2' , 'hex' ) ,
@@ -373,7 +378,7 @@ assert.deepEqual(a4,
373
378
// Test multiple updates to same hash
374
379
var h1 = crypto . createHash ( 'sha1' ) . update ( 'Test123' ) . digest ( 'hex' ) ;
375
380
var h2 = crypto . createHash ( 'sha1' ) . update ( 'Test' ) . update ( '123' ) . digest ( 'hex' ) ;
376
- assert . equal ( h1 , h2 , 'multipled updates' ) ;
381
+ assert . strictEqual ( h1 , h2 , 'multipled updates' ) ;
377
382
378
383
// Test hashing for binary files
379
384
var fn = path . join ( common . fixturesDir , 'sample.png' ) ;
@@ -382,16 +387,18 @@ var fileStream = fs.createReadStream(fn);
382
387
fileStream . on ( 'data' , function ( data ) {
383
388
sha1Hash . update ( data ) ;
384
389
} ) ;
385
- fileStream . on ( 'close' , function ( ) {
386
- assert . equal ( sha1Hash . digest ( 'hex' ) ,
387
- '22723e553129a336ad96e10f6aecdf0f45e4149e' ,
388
- 'Test SHA1 of sample.png' ) ;
389
- } ) ;
390
+ fileStream . on ( 'close' , common . mustCall ( function ( ) {
391
+ assert . strictEqual (
392
+ sha1Hash . digest ( 'hex' ) ,
393
+ '22723e553129a336ad96e10f6aecdf0f45e4149e' ,
394
+ 'Test SHA1 of sample.png'
395
+ ) ;
396
+ } ) ) ;
390
397
391
398
// Issue #2227: unknown digest method should throw an error.
392
399
assert . throws ( function ( ) {
393
400
crypto . createHash ( 'xyzzy' ) ;
394
- } ) ;
401
+ } , / ^ E r r o r : D i g e s t m e t h o d n o t s u p p o r t e d $ / ) ;
395
402
396
403
// Test signing and verifying
397
404
var s1 = crypto . createSign ( 'RSA-SHA1' )
@@ -437,7 +444,7 @@ function testCipher1(key) {
437
444
var txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
438
445
txt += decipher . final ( 'utf8' ) ;
439
446
440
- assert . equal ( txt , plaintext , 'encryption and decryption' ) ;
447
+ assert . strictEqual ( txt , plaintext , 'encryption and decryption' ) ;
441
448
}
442
449
443
450
@@ -459,7 +466,7 @@ function testCipher2(key) {
459
466
var txt = decipher . update ( ciph , 'base64' , 'utf8' ) ;
460
467
txt += decipher . final ( 'utf8' ) ;
461
468
462
- assert . equal ( txt , plaintext , 'encryption and decryption with Base64' ) ;
469
+ assert . strictEqual ( txt , plaintext , 'encryption and decryption with Base64' ) ;
463
470
}
464
471
465
472
@@ -477,7 +484,8 @@ function testCipher3(key, iv) {
477
484
var txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
478
485
txt += decipher . final ( 'utf8' ) ;
479
486
480
- assert . equal ( txt , plaintext , 'encryption and decryption with key and iv' ) ;
487
+ assert . strictEqual ( txt , plaintext ,
488
+ 'encryption and decryption with key and iv' ) ;
481
489
}
482
490
483
491
@@ -495,7 +503,8 @@ function testCipher4(key, iv) {
495
503
var txt = decipher . update ( ciph , 'buffer' , 'utf8' ) ;
496
504
txt += decipher . final ( 'utf8' ) ;
497
505
498
- assert . equal ( txt , plaintext , 'encryption and decryption with key and iv' ) ;
506
+ assert . strictEqual ( txt , plaintext ,
507
+ 'encryption and decryption with key and iv' ) ;
499
508
}
500
509
501
510
if ( ! common . hasFipsCrypto ) {
@@ -517,7 +526,7 @@ testCipher4(new Buffer('0123456789abcd0123456789'), new Buffer('12345678'));
517
526
// update() should only take buffers / strings
518
527
assert . throws ( function ( ) {
519
528
crypto . createHash ( 'sha1' ) . update ( { foo : 'bar' } ) ;
520
- } , / b u f f e r / ) ;
529
+ } , / ^ T y p e E r r o r : N o t a s t r i n g o r b u f f e r $ / ) ;
521
530
522
531
523
532
// Test Diffie-Hellman with two parties sharing a secret,
@@ -530,7 +539,7 @@ var key2 = dh2.generateKeys('hex');
530
539
var secret1 = dh1 . computeSecret ( key2 , 'hex' , 'base64' ) ;
531
540
var secret2 = dh2 . computeSecret ( key1 , 'binary' , 'buffer' ) ;
532
541
533
- assert . equal ( secret1 , secret2 . toString ( 'base64' ) ) ;
542
+ assert . strictEqual ( secret1 , secret2 . toString ( 'base64' ) ) ;
534
543
535
544
// Create "another dh1" using generated keys from dh1,
536
545
// and compute secret again
@@ -539,37 +548,39 @@ var privkey1 = dh1.getPrivateKey();
539
548
dh3 . setPublicKey ( key1 ) ;
540
549
dh3 . setPrivateKey ( privkey1 ) ;
541
550
542
- assert . equal ( dh1 . getPrime ( ) , dh3 . getPrime ( ) ) ;
543
- assert . equal ( dh1 . getGenerator ( ) , dh3 . getGenerator ( ) ) ;
544
- assert . equal ( dh1 . getPublicKey ( ) , dh3 . getPublicKey ( ) ) ;
545
- assert . equal ( dh1 . getPrivateKey ( ) , dh3 . getPrivateKey ( ) ) ;
551
+ assert . strictEqual ( dh1 . getPrime ( ) , dh3 . getPrime ( ) ) ;
552
+ assert . strictEqual ( dh1 . getGenerator ( ) , dh3 . getGenerator ( ) ) ;
553
+ assert . strictEqual ( dh1 . getPublicKey ( ) , dh3 . getPublicKey ( ) ) ;
554
+ assert . strictEqual ( dh1 . getPrivateKey ( ) , dh3 . getPrivateKey ( ) ) ;
546
555
547
556
var secret3 = dh3 . computeSecret ( key2 , 'hex' , 'base64' ) ;
548
557
549
- assert . equal ( secret1 , secret3 ) ;
558
+ assert . strictEqual ( secret1 , secret3 ) ;
550
559
551
560
// https://github.com/joyent/node/issues/2338
552
561
var p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
553
562
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' +
554
563
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
555
564
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF' ;
556
565
var d = crypto . createDiffieHellman ( p , 'hex' ) ;
557
- assert . equal ( d . verifyError , constants . DH_NOT_SUITABLE_GENERATOR ) ;
566
+ assert . strictEqual ( d . verifyError , constants . DH_NOT_SUITABLE_GENERATOR ) ;
558
567
559
568
// Test RSA key signing/verification
560
569
var rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
561
570
var rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
562
- assert . ok ( rsaSign ) ;
563
- assert . ok ( rsaVerify ) ;
571
+ assert . ok ( rsaSign instanceof crypto . Sign ) ;
572
+ assert . ok ( rsaVerify instanceof crypto . Verify ) ;
564
573
565
574
rsaSign . update ( rsaPubPem ) ;
566
575
var rsaSignature = rsaSign . sign ( rsaKeyPem , 'hex' ) ;
567
- assert . equal ( rsaSignature ,
568
- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
569
- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
570
- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
571
- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
572
- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
576
+ assert . strictEqual (
577
+ rsaSignature ,
578
+ '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
579
+ '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
580
+ 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
581
+ '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
582
+ '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'
583
+ ) ;
573
584
574
585
rsaVerify . update ( rsaPubPem ) ;
575
586
assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
@@ -636,12 +647,13 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
636
647
// Test PBKDF2 with RFC 6070 test vectors (except #4)
637
648
//
638
649
function testPBKDF2 ( password , salt , iterations , keylen , expected ) {
639
- var actual = crypto . pbkdf2Sync ( password , salt , iterations , keylen ) ;
640
- assert . equal ( actual , expected ) ;
650
+ const actual = crypto . pbkdf2Sync ( password , salt , iterations , keylen ) ;
651
+ assert . strictEqual ( actual , expected ) ;
641
652
642
- crypto . pbkdf2 ( password , salt , iterations , keylen , function ( err , actual ) {
643
- assert . equal ( actual , expected ) ;
653
+ const cb = common . mustCall ( ( err , actual ) => {
654
+ assert . strictEqual ( actual , expected ) ;
644
655
} ) ;
656
+ crypto . pbkdf2 ( password , salt , iterations , keylen , cb ) ;
645
657
}
646
658
647
659
0 commit comments