Skip to content

Commit 7e73eea

Browse files
vsemozhetbytMylesBorins
authored andcommitted
doc: update examples in api/crypto.md
* var -> const / let in crypto.md * fix error in crypto.md code example * equal -> strictEqual, == -> === in crypto.md * update estimated outputs in crypto.md * snake_case -> camelCase in crypto.md examples * concatenation -> multiline template in crypto * add missing line break in crypto code example * add missing link reference in crypto.md PR-URL: #10909 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b5a2014 commit 7e73eea

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

doc/api/crypto.md

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ It is possible for Node.js to be built without including support for the
2626
error being thrown.
2727

2828
```js
29-
var crypto;
29+
let crypto;
3030
try {
3131
crypto = require('crypto');
3232
} catch (err) {
@@ -132,9 +132,9 @@ Example: Using `Cipher` objects as streams:
132132
const crypto = require('crypto');
133133
const cipher = crypto.createCipher('aes192', 'a password');
134134

135-
var encrypted = '';
135+
let encrypted = '';
136136
cipher.on('readable', () => {
137-
var data = cipher.read();
137+
const data = cipher.read();
138138
if (data)
139139
encrypted += data.toString('hex');
140140
});
@@ -166,7 +166,7 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
166166
const crypto = require('crypto');
167167
const cipher = crypto.createCipher('aes192', 'a password');
168168

169-
var encrypted = cipher.update('some clear text data', 'utf8', 'hex');
169+
let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
170170
encrypted += cipher.final('hex');
171171
console.log(encrypted);
172172
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
@@ -265,9 +265,9 @@ Example: Using `Decipher` objects as streams:
265265
const crypto = require('crypto');
266266
const decipher = crypto.createDecipher('aes192', 'a password');
267267

268-
var decrypted = '';
268+
let decrypted = '';
269269
decipher.on('readable', () => {
270-
var data = decipher.read();
270+
const data = decipher.read();
271271
if (data)
272272
decrypted += data.toString('utf8');
273273
});
@@ -276,7 +276,7 @@ decipher.on('end', () => {
276276
// Prints: some clear text data
277277
});
278278

279-
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
279+
const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
280280
decipher.write(encrypted, 'hex');
281281
decipher.end();
282282
```
@@ -300,8 +300,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
300300
const crypto = require('crypto');
301301
const decipher = crypto.createDecipher('aes192', 'a password');
302302

303-
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
304-
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
303+
const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
304+
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
305305
decrypted += decipher.final('utf8');
306306
console.log(decrypted);
307307
// Prints: some clear text data
@@ -392,18 +392,18 @@ const assert = require('assert');
392392

393393
// Generate Alice's keys...
394394
const alice = crypto.createDiffieHellman(2048);
395-
const alice_key = alice.generateKeys();
395+
const aliceKey = alice.generateKeys();
396396

397397
// Generate Bob's keys...
398398
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
399-
const bob_key = bob.generateKeys();
399+
const bobKey = bob.generateKeys();
400400

401401
// 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);
404404

405405
// OK
406-
assert.equal(alice_secret.toString('hex'), bob_secret.toString('hex'));
406+
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
407407
```
408408

409409
### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])
@@ -521,17 +521,17 @@ const assert = require('assert');
521521

522522
// Generate Alice's keys...
523523
const alice = crypto.createECDH('secp521r1');
524-
const alice_key = alice.generateKeys();
524+
const aliceKey = alice.generateKeys();
525525

526526
// Generate Bob's keys...
527527
const bob = crypto.createECDH('secp521r1');
528-
const bob_key = bob.generateKeys();
528+
const bobKey = bob.generateKeys();
529529

530530
// 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);
533533

534-
assert(alice_secret, bob_secret);
534+
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
535535
// OK
536536
```
537537

@@ -638,13 +638,14 @@ alice.setPrivateKey(
638638
);
639639

640640
// Bob uses a newly generated cryptographically strong
641-
// pseudorandom key pair bob.generateKeys();
641+
// pseudorandom key pair
642+
bob.generateKeys();
642643

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');
645646

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);
648649
```
649650

650651
## Class: Hash
@@ -670,7 +671,7 @@ const crypto = require('crypto');
670671
const hash = crypto.createHash('sha256');
671672

672673
hash.on('readable', () => {
673-
var data = hash.read();
674+
const data = hash.read();
674675
if (data)
675676
console.log(data.toString('hex'));
676677
// Prints:
@@ -753,7 +754,7 @@ const crypto = require('crypto');
753754
const hmac = crypto.createHmac('sha256', 'a secret');
754755

755756
hmac.on('readable', () => {
756-
var data = hmac.read();
757+
const data = hmac.read();
757758
if (data)
758759
console.log(data.toString('hex'));
759760
// Prints:
@@ -837,8 +838,8 @@ const sign = crypto.createSign('RSA-SHA256');
837838
sign.write('some data to sign');
838839
sign.end();
839840

840-
const private_key = getPrivateKeySomehow();
841-
console.log(sign.sign(private_key, 'hex'));
841+
const privateKey = getPrivateKeySomehow();
842+
console.log(sign.sign(privateKey, 'hex'));
842843
// Prints: the calculated signature
843844
```
844845

@@ -850,8 +851,8 @@ const sign = crypto.createSign('RSA-SHA256');
850851

851852
sign.update('some data to sign');
852853

853-
const private_key = getPrivateKeySomehow();
854-
console.log(sign.sign(private_key, 'hex'));
854+
const privateKey = getPrivateKeySomehow();
855+
console.log(sign.sign(privateKey, 'hex'));
855856
// Prints: the calculated signature
856857
```
857858

@@ -868,13 +869,14 @@ const sign = crypto.createSign('sha256');
868869

869870
sign.update('some data to sign');
870871

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-----`;
876878

877-
console.log(sign.sign(private_key).toString('hex'));
879+
console.log(sign.sign(privateKey).toString('hex'));
878880
```
879881

880882
### sign.sign(private_key[, output_format])
@@ -937,9 +939,9 @@ const verify = crypto.createVerify('RSA-SHA256');
937939
verify.write('some data to sign');
938940
verify.end();
939941

940-
const public_key = getPublicKeySomehow();
942+
const publicKey = getPublicKeySomehow();
941943
const signature = getSignatureToVerify();
942-
console.log(verify.verify(public_key, signature));
944+
console.log(verify.verify(publicKey, signature));
943945
// Prints: true or false
944946
```
945947

@@ -951,9 +953,9 @@ const verify = crypto.createVerify('RSA-SHA256');
951953

952954
verify.update('some data to sign');
953955

954-
const public_key = getPublicKeySomehow();
956+
const publicKey = getPublicKeySomehow();
955957
const signature = getSignatureToVerify();
956-
console.log(verify.verify(public_key, signature));
958+
console.log(verify.verify(publicKey, signature));
957959
// Prints: true or false
958960
```
959961

@@ -1182,7 +1184,7 @@ const hash = crypto.createHash('sha256');
11821184

11831185
const input = fs.createReadStream(filename);
11841186
input.on('readable', () => {
1185-
var data = input.read();
1187+
const data = input.read();
11861188
if (data)
11871189
hash.update(data);
11881190
else {
@@ -1216,7 +1218,7 @@ const hmac = crypto.createHmac('sha256', 'a secret');
12161218

12171219
const input = fs.createReadStream(filename);
12181220
input.on('readable', () => {
1219-
var data = input.read();
1221+
const data = input.read();
12201222
if (data)
12211223
hmac.update(data);
12221224
else {
@@ -1268,7 +1270,7 @@ Example:
12681270

12691271
```js
12701272
const curves = crypto.getCurves();
1271-
console.log(curves); // ['secp256k1', 'secp384r1', ...]
1273+
console.log(curves); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
12721274
```
12731275

12741276
### crypto.getDiffieHellman(group_name)
@@ -1297,11 +1299,11 @@ const bob = crypto.getDiffieHellman('modp14');
12971299
alice.generateKeys();
12981300
bob.generateKeys();
12991301

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');
13021304

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);
13051307
```
13061308

13071309
### crypto.getHashes()
@@ -1316,7 +1318,7 @@ Example:
13161318

13171319
```js
13181320
const hashes = crypto.getHashes();
1319-
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
1321+
console.log(hashes); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
13201322
```
13211323

13221324
### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
@@ -1347,7 +1349,7 @@ Example:
13471349
const crypto = require('crypto');
13481350
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
13491351
if (err) throw err;
1350-
console.log(key.toString('hex')); // 'c5e478d...1469e50'
1352+
console.log(key.toString('hex')); // '3745e48...aa39b34'
13511353
});
13521354
```
13531355

@@ -1380,7 +1382,7 @@ Example:
13801382
```js
13811383
const crypto = require('crypto');
13821384
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'
13841386
```
13851387

13861388
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.
19281930
[`crypto.createHash()`]: #crypto_crypto_createhash_algorithm
19291931
[`crypto.createHmac()`]: #crypto_crypto_createhmac_algorithm_key
19301932
[`crypto.createSign()`]: #crypto_crypto_createsign_algorithm
1933+
[`crypto.createVerify()`]: #crypto_crypto_createverify_algorithm
19311934
[`crypto.getCurves()`]: #crypto_crypto_getcurves
19321935
[`crypto.getHashes()`]: #crypto_crypto_gethashes
19331936
[`crypto.pbkdf2()`]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback

0 commit comments

Comments
 (0)