@@ -26,7 +26,7 @@ It is possible for Node.js to be built without including support for the
26
26
error being thrown.
27
27
28
28
``` js
29
- var crypto;
29
+ let crypto;
30
30
try {
31
31
crypto = require (' crypto' );
32
32
} catch (err) {
@@ -132,9 +132,9 @@ Example: Using `Cipher` objects as streams:
132
132
const crypto = require (' crypto' );
133
133
const cipher = crypto .createCipher (' aes192' , ' a password' );
134
134
135
- var encrypted = ' ' ;
135
+ let encrypted = ' ' ;
136
136
cipher .on (' readable' , () => {
137
- var data = cipher .read ();
137
+ const data = cipher .read ();
138
138
if (data)
139
139
encrypted += data .toString (' hex' );
140
140
});
@@ -166,7 +166,7 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
166
166
const crypto = require (' crypto' );
167
167
const cipher = crypto .createCipher (' aes192' , ' a password' );
168
168
169
- var encrypted = cipher .update (' some clear text data' , ' utf8' , ' hex' );
169
+ let encrypted = cipher .update (' some clear text data' , ' utf8' , ' hex' );
170
170
encrypted += cipher .final (' hex' );
171
171
console .log (encrypted);
172
172
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
@@ -269,9 +269,9 @@ Example: Using `Decipher` objects as streams:
269
269
const crypto = require (' crypto' );
270
270
const decipher = crypto .createDecipher (' aes192' , ' a password' );
271
271
272
- var decrypted = ' ' ;
272
+ let decrypted = ' ' ;
273
273
decipher .on (' readable' , () => {
274
- var data = decipher .read ();
274
+ const data = decipher .read ();
275
275
if (data)
276
276
decrypted += data .toString (' utf8' );
277
277
});
@@ -280,7 +280,7 @@ decipher.on('end', () => {
280
280
// Prints: some clear text data
281
281
});
282
282
283
- var encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
283
+ const encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
284
284
decipher .write (encrypted, ' hex' );
285
285
decipher .end ();
286
286
```
@@ -304,8 +304,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
304
304
const crypto = require (' crypto' );
305
305
const decipher = crypto .createDecipher (' aes192' , ' a password' );
306
306
307
- var encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
308
- var decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
307
+ const encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
308
+ let decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
309
309
decrypted += decipher .final (' utf8' );
310
310
console .log (decrypted);
311
311
// Prints: some clear text data
@@ -402,18 +402,18 @@ const assert = require('assert');
402
402
403
403
// Generate Alice's keys...
404
404
const alice = crypto .createDiffieHellman (2048 );
405
- const alice_key = alice .generateKeys ();
405
+ const aliceKey = alice .generateKeys ();
406
406
407
407
// Generate Bob's keys...
408
408
const bob = crypto .createDiffieHellman (alice .getPrime (), alice .getGenerator ());
409
- const bob_key = bob .generateKeys ();
409
+ const bobKey = bob .generateKeys ();
410
410
411
411
// Exchange and generate the secret...
412
- const alice_secret = alice .computeSecret (bob_key );
413
- const bob_secret = bob .computeSecret (alice_key );
412
+ const aliceSecret = alice .computeSecret (bobKey );
413
+ const bobSecret = bob .computeSecret (aliceKey );
414
414
415
415
// OK
416
- assert .equal ( alice_secret .toString (' hex' ), bob_secret .toString (' hex' ));
416
+ assert .strictEqual ( aliceSecret .toString (' hex' ), bobSecret .toString (' hex' ));
417
417
```
418
418
419
419
### diffieHellman.computeSecret(other_public_key[ , input_encoding] [ , output_encoding ] )
@@ -531,17 +531,17 @@ const assert = require('assert');
531
531
532
532
// Generate Alice's keys...
533
533
const alice = crypto .createECDH (' secp521r1' );
534
- const alice_key = alice .generateKeys ();
534
+ const aliceKey = alice .generateKeys ();
535
535
536
536
// Generate Bob's keys...
537
537
const bob = crypto .createECDH (' secp521r1' );
538
- const bob_key = bob .generateKeys ();
538
+ const bobKey = bob .generateKeys ();
539
539
540
540
// Exchange and generate the secret...
541
- const alice_secret = alice .computeSecret (bob_key );
542
- const bob_secret = bob .computeSecret (alice_key );
541
+ const aliceSecret = alice .computeSecret (bobKey );
542
+ const bobSecret = bob .computeSecret (aliceKey );
543
543
544
- assert (alice_secret, bob_secret );
544
+ assert . strictEqual ( aliceSecret . toString ( ' hex ' ), bobSecret . toString ( ' hex ' ) );
545
545
// OK
546
546
```
547
547
@@ -648,13 +648,14 @@ alice.setPrivateKey(
648
648
);
649
649
650
650
// Bob uses a newly generated cryptographically strong
651
- // pseudorandom key pair bob.generateKeys();
651
+ // pseudorandom key pair
652
+ bob .generateKeys ();
652
653
653
- const alice_secret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
654
- const bob_secret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
654
+ const aliceSecret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
655
+ const bobSecret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
655
656
656
- // alice_secret and bob_secret should be the same shared secret value
657
- console .log (alice_secret === bob_secret );
657
+ // aliceSecret and bobSecret should be the same shared secret value
658
+ console .log (aliceSecret === bobSecret );
658
659
```
659
660
660
661
## Class: Hash
@@ -680,7 +681,7 @@ const crypto = require('crypto');
680
681
const hash = crypto .createHash (' sha256' );
681
682
682
683
hash .on (' readable' , () => {
683
- var data = hash .read ();
684
+ const data = hash .read ();
684
685
if (data)
685
686
console .log (data .toString (' hex' ));
686
687
// Prints:
@@ -763,7 +764,7 @@ const crypto = require('crypto');
763
764
const hmac = crypto .createHmac (' sha256' , ' a secret' );
764
765
765
766
hmac .on (' readable' , () => {
766
- var data = hmac .read ();
767
+ const data = hmac .read ();
767
768
if (data)
768
769
console .log (data .toString (' hex' ));
769
770
// Prints:
@@ -847,8 +848,8 @@ const sign = crypto.createSign('RSA-SHA256');
847
848
sign .write (' some data to sign' );
848
849
sign .end ();
849
850
850
- const private_key = getPrivateKeySomehow ();
851
- console .log (sign .sign (private_key , ' hex' ));
851
+ const privateKey = getPrivateKeySomehow ();
852
+ console .log (sign .sign (privateKey , ' hex' ));
852
853
// Prints: the calculated signature
853
854
```
854
855
@@ -860,8 +861,8 @@ const sign = crypto.createSign('RSA-SHA256');
860
861
861
862
sign .update (' some data to sign' );
862
863
863
- const private_key = getPrivateKeySomehow ();
864
- console .log (sign .sign (private_key , ' hex' ));
864
+ const privateKey = getPrivateKeySomehow ();
865
+ console .log (sign .sign (privateKey , ' hex' ));
865
866
// Prints: the calculated signature
866
867
```
867
868
@@ -878,13 +879,14 @@ const sign = crypto.createSign('sha256');
878
879
879
880
sign .update (' some data to sign' );
880
881
881
- const private_key = ' -----BEGIN EC PRIVATE KEY-----\n ' +
882
- ' MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n ' +
883
- ' AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n ' +
884
- ' pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n ' +
885
- ' -----END EC PRIVATE KEY-----\n ' ;
882
+ const privateKey =
883
+ ` -----BEGIN EC PRIVATE KEY-----
884
+ MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49
885
+ AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2
886
+ pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
887
+ -----END EC PRIVATE KEY-----` ;
886
888
887
- console .log (sign .sign (private_key ).toString (' hex' ));
889
+ console .log (sign .sign (privateKey ).toString (' hex' ));
888
890
```
889
891
890
892
### sign.sign(private_key[ , output_format] )
@@ -947,9 +949,9 @@ const verify = crypto.createVerify('RSA-SHA256');
947
949
verify .write (' some data to sign' );
948
950
verify .end ();
949
951
950
- const public_key = getPublicKeySomehow ();
952
+ const publicKey = getPublicKeySomehow ();
951
953
const signature = getSignatureToVerify ();
952
- console .log (verify .verify (public_key , signature));
954
+ console .log (verify .verify (publicKey , signature));
953
955
// Prints: true or false
954
956
```
955
957
@@ -961,9 +963,9 @@ const verify = crypto.createVerify('RSA-SHA256');
961
963
962
964
verify .update (' some data to sign' );
963
965
964
- const public_key = getPublicKeySomehow ();
966
+ const publicKey = getPublicKeySomehow ();
965
967
const signature = getSignatureToVerify ();
966
- console .log (verify .verify (public_key , signature));
968
+ console .log (verify .verify (publicKey , signature));
967
969
// Prints: true or false
968
970
```
969
971
@@ -1192,7 +1194,7 @@ const hash = crypto.createHash('sha256');
1192
1194
1193
1195
const input = fs .createReadStream (filename);
1194
1196
input .on (' readable' , () => {
1195
- var data = input .read ();
1197
+ const data = input .read ();
1196
1198
if (data)
1197
1199
hash .update (data);
1198
1200
else {
@@ -1226,7 +1228,7 @@ const hmac = crypto.createHmac('sha256', 'a secret');
1226
1228
1227
1229
const input = fs .createReadStream (filename);
1228
1230
input .on (' readable' , () => {
1229
- var data = input .read ();
1231
+ const data = input .read ();
1230
1232
if (data)
1231
1233
hmac .update (data);
1232
1234
else {
@@ -1278,7 +1280,7 @@ Example:
1278
1280
1279
1281
``` js
1280
1282
const curves = crypto .getCurves ();
1281
- console .log (curves); // ['secp256k1 ', 'secp384r1 ', ...]
1283
+ console .log (curves); // ['Oakley-EC2N-3 ', 'Oakley-EC2N-4 ', ...]
1282
1284
```
1283
1285
1284
1286
### crypto.getDiffieHellman(group_name)
@@ -1307,11 +1309,11 @@ const bob = crypto.getDiffieHellman('modp14');
1307
1309
alice .generateKeys ();
1308
1310
bob .generateKeys ();
1309
1311
1310
- const alice_secret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
1311
- const bob_secret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
1312
+ const aliceSecret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
1313
+ const bobSecret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
1312
1314
1313
- /* alice_secret and bob_secret should be the same */
1314
- console .log (alice_secret == bob_secret );
1315
+ /* aliceSecret and bobSecret should be the same */
1316
+ console .log (aliceSecret === bobSecret );
1315
1317
```
1316
1318
1317
1319
### crypto.getHashes()
@@ -1326,7 +1328,7 @@ Example:
1326
1328
1327
1329
``` js
1328
1330
const hashes = crypto .getHashes ();
1329
- console .log (hashes); // ['sha ', 'sha1 ', 'sha1WithRSAEncryption ', ...]
1331
+ console .log (hashes); // ['DSA ', 'DSA-SHA ', 'DSA-SHA1 ', ...]
1330
1332
```
1331
1333
1332
1334
### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
@@ -1357,7 +1359,7 @@ Example:
1357
1359
const crypto = require (' crypto' );
1358
1360
crypto .pbkdf2 (' secret' , ' salt' , 100000 , 512 , ' sha512' , (err , key ) => {
1359
1361
if (err) throw err;
1360
- console .log (key .toString (' hex' )); // 'c5e478d ...1469e50 '
1362
+ console .log (key .toString (' hex' )); // '3745e48 ...aa39b34 '
1361
1363
});
1362
1364
```
1363
1365
@@ -1390,7 +1392,7 @@ Example:
1390
1392
``` js
1391
1393
const crypto = require (' crypto' );
1392
1394
const key = crypto .pbkdf2Sync (' secret' , ' salt' , 100000 , 512 , ' sha512' );
1393
- console .log (key .toString (' hex' )); // 'c5e478d ...1469e50 '
1395
+ console .log (key .toString (' hex' )); // '3745e48 ...aa39b34 '
1394
1396
```
1395
1397
1396
1398
An array of supported digest functions can be retrieved using
@@ -1938,6 +1940,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
1938
1940
[ `crypto.createHash()` ] : #crypto_crypto_createhash_algorithm
1939
1941
[ `crypto.createHmac()` ] : #crypto_crypto_createhmac_algorithm_key
1940
1942
[ `crypto.createSign()` ] : #crypto_crypto_createsign_algorithm
1943
+ [ `crypto.createVerify()` ] : #crypto_crypto_createverify_algorithm
1941
1944
[ `crypto.getCurves()` ] : #crypto_crypto_getcurves
1942
1945
[ `crypto.getHashes()` ] : #crypto_crypto_gethashes
1943
1946
[ `crypto.pbkdf2()` ] : #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
0 commit comments