@@ -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
@@ -265,9 +265,9 @@ Example: Using `Decipher` objects as streams:
265
265
const crypto = require (' crypto' );
266
266
const decipher = crypto .createDecipher (' aes192' , ' a password' );
267
267
268
- var decrypted = ' ' ;
268
+ let decrypted = ' ' ;
269
269
decipher .on (' readable' , () => {
270
- var data = decipher .read ();
270
+ const data = decipher .read ();
271
271
if (data)
272
272
decrypted += data .toString (' utf8' );
273
273
});
@@ -276,7 +276,7 @@ decipher.on('end', () => {
276
276
// Prints: some clear text data
277
277
});
278
278
279
- var encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
279
+ const encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
280
280
decipher .write (encrypted, ' hex' );
281
281
decipher .end ();
282
282
```
@@ -300,8 +300,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
300
300
const crypto = require (' crypto' );
301
301
const decipher = crypto .createDecipher (' aes192' , ' a password' );
302
302
303
- var encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
304
- var decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
303
+ const encrypted = ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504' ;
304
+ let decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
305
305
decrypted += decipher .final (' utf8' );
306
306
console .log (decrypted);
307
307
// Prints: some clear text data
@@ -392,18 +392,18 @@ const assert = require('assert');
392
392
393
393
// Generate Alice's keys...
394
394
const alice = crypto .createDiffieHellman (2048 );
395
- const alice_key = alice .generateKeys ();
395
+ const aliceKey = alice .generateKeys ();
396
396
397
397
// Generate Bob's keys...
398
398
const bob = crypto .createDiffieHellman (alice .getPrime (), alice .getGenerator ());
399
- const bob_key = bob .generateKeys ();
399
+ const bobKey = bob .generateKeys ();
400
400
401
401
// Exchange and generate the secret...
402
- const alice_secret = alice .computeSecret (bob_key );
403
- const bob_secret = bob .computeSecret (alice_key );
402
+ const aliceSecret = alice .computeSecret (bobKey );
403
+ const bobSecret = bob .computeSecret (aliceKey );
404
404
405
405
// OK
406
- assert .equal ( alice_secret .toString (' hex' ), bob_secret .toString (' hex' ));
406
+ assert .strictEqual ( aliceSecret .toString (' hex' ), bobSecret .toString (' hex' ));
407
407
```
408
408
409
409
### diffieHellman.computeSecret(other_public_key[ , input_encoding] [ , output_encoding ] )
@@ -521,17 +521,17 @@ const assert = require('assert');
521
521
522
522
// Generate Alice's keys...
523
523
const alice = crypto .createECDH (' secp521r1' );
524
- const alice_key = alice .generateKeys ();
524
+ const aliceKey = alice .generateKeys ();
525
525
526
526
// Generate Bob's keys...
527
527
const bob = crypto .createECDH (' secp521r1' );
528
- const bob_key = bob .generateKeys ();
528
+ const bobKey = bob .generateKeys ();
529
529
530
530
// Exchange and generate the secret...
531
- const alice_secret = alice .computeSecret (bob_key );
532
- const bob_secret = bob .computeSecret (alice_key );
531
+ const aliceSecret = alice .computeSecret (bobKey );
532
+ const bobSecret = bob .computeSecret (aliceKey );
533
533
534
- assert (alice_secret, bob_secret );
534
+ assert . strictEqual ( aliceSecret . toString ( ' hex ' ), bobSecret . toString ( ' hex ' ) );
535
535
// OK
536
536
```
537
537
@@ -638,13 +638,14 @@ alice.setPrivateKey(
638
638
);
639
639
640
640
// Bob uses a newly generated cryptographically strong
641
- // pseudorandom key pair bob.generateKeys();
641
+ // pseudorandom key pair
642
+ bob .generateKeys ();
642
643
643
- const alice_secret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
644
- const bob_secret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
644
+ const aliceSecret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
645
+ const bobSecret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
645
646
646
- // alice_secret and bob_secret should be the same shared secret value
647
- console .log (alice_secret === bob_secret );
647
+ // aliceSecret and bobSecret should be the same shared secret value
648
+ console .log (aliceSecret === bobSecret );
648
649
```
649
650
650
651
## Class: Hash
@@ -670,7 +671,7 @@ const crypto = require('crypto');
670
671
const hash = crypto .createHash (' sha256' );
671
672
672
673
hash .on (' readable' , () => {
673
- var data = hash .read ();
674
+ const data = hash .read ();
674
675
if (data)
675
676
console .log (data .toString (' hex' ));
676
677
// Prints:
@@ -753,7 +754,7 @@ const crypto = require('crypto');
753
754
const hmac = crypto .createHmac (' sha256' , ' a secret' );
754
755
755
756
hmac .on (' readable' , () => {
756
- var data = hmac .read ();
757
+ const data = hmac .read ();
757
758
if (data)
758
759
console .log (data .toString (' hex' ));
759
760
// Prints:
@@ -837,8 +838,8 @@ const sign = crypto.createSign('RSA-SHA256');
837
838
sign .write (' some data to sign' );
838
839
sign .end ();
839
840
840
- const private_key = getPrivateKeySomehow ();
841
- console .log (sign .sign (private_key , ' hex' ));
841
+ const privateKey = getPrivateKeySomehow ();
842
+ console .log (sign .sign (privateKey , ' hex' ));
842
843
// Prints: the calculated signature
843
844
```
844
845
@@ -850,8 +851,8 @@ const sign = crypto.createSign('RSA-SHA256');
850
851
851
852
sign .update (' some data to sign' );
852
853
853
- const private_key = getPrivateKeySomehow ();
854
- console .log (sign .sign (private_key , ' hex' ));
854
+ const privateKey = getPrivateKeySomehow ();
855
+ console .log (sign .sign (privateKey , ' hex' ));
855
856
// Prints: the calculated signature
856
857
```
857
858
@@ -868,13 +869,14 @@ const sign = crypto.createSign('sha256');
868
869
869
870
sign .update (' some data to sign' );
870
871
871
- const private_key = ' -----BEGIN EC PRIVATE KEY-----\n ' +
872
- ' MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n ' +
873
- ' AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n ' +
874
- ' pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n ' +
875
- ' -----END EC PRIVATE KEY-----\n ' ;
872
+ const privateKey =
873
+ ` -----BEGIN EC PRIVATE KEY-----
874
+ MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49
875
+ AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2
876
+ pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
877
+ -----END EC PRIVATE KEY-----` ;
876
878
877
- console .log (sign .sign (private_key ).toString (' hex' ));
879
+ console .log (sign .sign (privateKey ).toString (' hex' ));
878
880
```
879
881
880
882
### sign.sign(private_key[ , output_format] )
@@ -937,9 +939,9 @@ const verify = crypto.createVerify('RSA-SHA256');
937
939
verify .write (' some data to sign' );
938
940
verify .end ();
939
941
940
- const public_key = getPublicKeySomehow ();
942
+ const publicKey = getPublicKeySomehow ();
941
943
const signature = getSignatureToVerify ();
942
- console .log (verify .verify (public_key , signature));
944
+ console .log (verify .verify (publicKey , signature));
943
945
// Prints: true or false
944
946
```
945
947
@@ -951,9 +953,9 @@ const verify = crypto.createVerify('RSA-SHA256');
951
953
952
954
verify .update (' some data to sign' );
953
955
954
- const public_key = getPublicKeySomehow ();
956
+ const publicKey = getPublicKeySomehow ();
955
957
const signature = getSignatureToVerify ();
956
- console .log (verify .verify (public_key , signature));
958
+ console .log (verify .verify (publicKey , signature));
957
959
// Prints: true or false
958
960
```
959
961
@@ -1182,7 +1184,7 @@ const hash = crypto.createHash('sha256');
1182
1184
1183
1185
const input = fs .createReadStream (filename);
1184
1186
input .on (' readable' , () => {
1185
- var data = input .read ();
1187
+ const data = input .read ();
1186
1188
if (data)
1187
1189
hash .update (data);
1188
1190
else {
@@ -1216,7 +1218,7 @@ const hmac = crypto.createHmac('sha256', 'a secret');
1216
1218
1217
1219
const input = fs .createReadStream (filename);
1218
1220
input .on (' readable' , () => {
1219
- var data = input .read ();
1221
+ const data = input .read ();
1220
1222
if (data)
1221
1223
hmac .update (data);
1222
1224
else {
@@ -1268,7 +1270,7 @@ Example:
1268
1270
1269
1271
``` js
1270
1272
const curves = crypto .getCurves ();
1271
- console .log (curves); // ['secp256k1 ', 'secp384r1 ', ...]
1273
+ console .log (curves); // ['Oakley-EC2N-3 ', 'Oakley-EC2N-4 ', ...]
1272
1274
```
1273
1275
1274
1276
### crypto.getDiffieHellman(group_name)
@@ -1297,11 +1299,11 @@ const bob = crypto.getDiffieHellman('modp14');
1297
1299
alice .generateKeys ();
1298
1300
bob .generateKeys ();
1299
1301
1300
- const alice_secret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
1301
- const bob_secret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
1302
+ const aliceSecret = alice .computeSecret (bob .getPublicKey (), null , ' hex' );
1303
+ const bobSecret = bob .computeSecret (alice .getPublicKey (), null , ' hex' );
1302
1304
1303
- /* alice_secret and bob_secret should be the same */
1304
- console .log (alice_secret == bob_secret );
1305
+ /* aliceSecret and bobSecret should be the same */
1306
+ console .log (aliceSecret === bobSecret );
1305
1307
```
1306
1308
1307
1309
### crypto.getHashes()
@@ -1316,7 +1318,7 @@ Example:
1316
1318
1317
1319
``` js
1318
1320
const hashes = crypto .getHashes ();
1319
- console .log (hashes); // ['sha ', 'sha1 ', 'sha1WithRSAEncryption ', ...]
1321
+ console .log (hashes); // ['DSA ', 'DSA-SHA ', 'DSA-SHA1 ', ...]
1320
1322
```
1321
1323
1322
1324
### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
@@ -1347,7 +1349,7 @@ Example:
1347
1349
const crypto = require (' crypto' );
1348
1350
crypto .pbkdf2 (' secret' , ' salt' , 100000 , 512 , ' sha512' , (err , key ) => {
1349
1351
if (err) throw err;
1350
- console .log (key .toString (' hex' )); // 'c5e478d ...1469e50 '
1352
+ console .log (key .toString (' hex' )); // '3745e48 ...aa39b34 '
1351
1353
});
1352
1354
```
1353
1355
@@ -1380,7 +1382,7 @@ Example:
1380
1382
``` js
1381
1383
const crypto = require (' crypto' );
1382
1384
const key = crypto .pbkdf2Sync (' secret' , ' salt' , 100000 , 512 , ' sha512' );
1383
- console .log (key .toString (' hex' )); // 'c5e478d ...1469e50 '
1385
+ console .log (key .toString (' hex' )); // '3745e48 ...aa39b34 '
1384
1386
```
1385
1387
1386
1388
An array of supported digest functions can be retrieved using
@@ -1928,6 +1930,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
1928
1930
[ `crypto.createHash()` ] : #crypto_crypto_createhash_algorithm
1929
1931
[ `crypto.createHmac()` ] : #crypto_crypto_createhmac_algorithm_key
1930
1932
[ `crypto.createSign()` ] : #crypto_crypto_createsign_algorithm
1933
+ [ `crypto.createVerify()` ] : #crypto_crypto_createverify_algorithm
1931
1934
[ `crypto.getCurves()` ] : #crypto_crypto_getcurves
1932
1935
[ `crypto.getHashes()` ] : #crypto_crypto_gethashes
1933
1936
[ `crypto.pbkdf2()` ] : #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
0 commit comments