Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1253650

Browse filesBrowse files
vsemozhetbytItalo A. Casas
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 <luigipinca@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3177d65 commit 1253650
Copy full SHA for 1253650

File tree

Expand file treeCollapse file tree

1 file changed

+53
-50
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+53
-50
lines changed
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+53-50Lines changed: 53 additions & 50 deletions
  • Display the source diff
  • Display the rich diff
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
@@ -269,9 +269,9 @@ Example: Using `Decipher` objects as streams:
269269
const crypto = require('crypto');
270270
const decipher = crypto.createDecipher('aes192', 'a password');
271271

272-
var decrypted = '';
272+
let decrypted = '';
273273
decipher.on('readable', () => {
274-
var data = decipher.read();
274+
const data = decipher.read();
275275
if (data)
276276
decrypted += data.toString('utf8');
277277
});
@@ -280,7 +280,7 @@ decipher.on('end', () => {
280280
// Prints: some clear text data
281281
});
282282

283-
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
283+
const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
284284
decipher.write(encrypted, 'hex');
285285
decipher.end();
286286
```
@@ -304,8 +304,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
304304
const crypto = require('crypto');
305305
const decipher = crypto.createDecipher('aes192', 'a password');
306306

307-
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
308-
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
307+
const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
308+
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
309309
decrypted += decipher.final('utf8');
310310
console.log(decrypted);
311311
// Prints: some clear text data
@@ -402,18 +402,18 @@ const assert = require('assert');
402402

403403
// Generate Alice's keys...
404404
const alice = crypto.createDiffieHellman(2048);
405-
const alice_key = alice.generateKeys();
405+
const aliceKey = alice.generateKeys();
406406

407407
// Generate Bob's keys...
408408
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
409-
const bob_key = bob.generateKeys();
409+
const bobKey = bob.generateKeys();
410410

411411
// 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);
414414

415415
// OK
416-
assert.equal(alice_secret.toString('hex'), bob_secret.toString('hex'));
416+
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
417417
```
418418

419419
### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])
@@ -531,17 +531,17 @@ const assert = require('assert');
531531

532532
// Generate Alice's keys...
533533
const alice = crypto.createECDH('secp521r1');
534-
const alice_key = alice.generateKeys();
534+
const aliceKey = alice.generateKeys();
535535

536536
// Generate Bob's keys...
537537
const bob = crypto.createECDH('secp521r1');
538-
const bob_key = bob.generateKeys();
538+
const bobKey = bob.generateKeys();
539539

540540
// 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);
543543

544-
assert(alice_secret, bob_secret);
544+
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
545545
// OK
546546
```
547547

@@ -648,13 +648,14 @@ alice.setPrivateKey(
648648
);
649649

650650
// Bob uses a newly generated cryptographically strong
651-
// pseudorandom key pair bob.generateKeys();
651+
// pseudorandom key pair
652+
bob.generateKeys();
652653

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

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);
658659
```
659660

660661
## Class: Hash
@@ -680,7 +681,7 @@ const crypto = require('crypto');
680681
const hash = crypto.createHash('sha256');
681682

682683
hash.on('readable', () => {
683-
var data = hash.read();
684+
const data = hash.read();
684685
if (data)
685686
console.log(data.toString('hex'));
686687
// Prints:
@@ -763,7 +764,7 @@ const crypto = require('crypto');
763764
const hmac = crypto.createHmac('sha256', 'a secret');
764765

765766
hmac.on('readable', () => {
766-
var data = hmac.read();
767+
const data = hmac.read();
767768
if (data)
768769
console.log(data.toString('hex'));
769770
// Prints:
@@ -847,8 +848,8 @@ const sign = crypto.createSign('RSA-SHA256');
847848
sign.write('some data to sign');
848849
sign.end();
849850

850-
const private_key = getPrivateKeySomehow();
851-
console.log(sign.sign(private_key, 'hex'));
851+
const privateKey = getPrivateKeySomehow();
852+
console.log(sign.sign(privateKey, 'hex'));
852853
// Prints: the calculated signature
853854
```
854855

@@ -860,8 +861,8 @@ const sign = crypto.createSign('RSA-SHA256');
860861

861862
sign.update('some data to sign');
862863

863-
const private_key = getPrivateKeySomehow();
864-
console.log(sign.sign(private_key, 'hex'));
864+
const privateKey = getPrivateKeySomehow();
865+
console.log(sign.sign(privateKey, 'hex'));
865866
// Prints: the calculated signature
866867
```
867868

@@ -878,13 +879,14 @@ const sign = crypto.createSign('sha256');
878879

879880
sign.update('some data to sign');
880881

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

887-
console.log(sign.sign(private_key).toString('hex'));
889+
console.log(sign.sign(privateKey).toString('hex'));
888890
```
889891

890892
### sign.sign(private_key[, output_format])
@@ -947,9 +949,9 @@ const verify = crypto.createVerify('RSA-SHA256');
947949
verify.write('some data to sign');
948950
verify.end();
949951

950-
const public_key = getPublicKeySomehow();
952+
const publicKey = getPublicKeySomehow();
951953
const signature = getSignatureToVerify();
952-
console.log(verify.verify(public_key, signature));
954+
console.log(verify.verify(publicKey, signature));
953955
// Prints: true or false
954956
```
955957

@@ -961,9 +963,9 @@ const verify = crypto.createVerify('RSA-SHA256');
961963

962964
verify.update('some data to sign');
963965

964-
const public_key = getPublicKeySomehow();
966+
const publicKey = getPublicKeySomehow();
965967
const signature = getSignatureToVerify();
966-
console.log(verify.verify(public_key, signature));
968+
console.log(verify.verify(publicKey, signature));
967969
// Prints: true or false
968970
```
969971

@@ -1192,7 +1194,7 @@ const hash = crypto.createHash('sha256');
11921194

11931195
const input = fs.createReadStream(filename);
11941196
input.on('readable', () => {
1195-
var data = input.read();
1197+
const data = input.read();
11961198
if (data)
11971199
hash.update(data);
11981200
else {
@@ -1226,7 +1228,7 @@ const hmac = crypto.createHmac('sha256', 'a secret');
12261228

12271229
const input = fs.createReadStream(filename);
12281230
input.on('readable', () => {
1229-
var data = input.read();
1231+
const data = input.read();
12301232
if (data)
12311233
hmac.update(data);
12321234
else {
@@ -1278,7 +1280,7 @@ Example:
12781280

12791281
```js
12801282
const curves = crypto.getCurves();
1281-
console.log(curves); // ['secp256k1', 'secp384r1', ...]
1283+
console.log(curves); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
12821284
```
12831285

12841286
### crypto.getDiffieHellman(group_name)
@@ -1307,11 +1309,11 @@ const bob = crypto.getDiffieHellman('modp14');
13071309
alice.generateKeys();
13081310
bob.generateKeys();
13091311

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

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);
13151317
```
13161318

13171319
### crypto.getHashes()
@@ -1326,7 +1328,7 @@ Example:
13261328

13271329
```js
13281330
const hashes = crypto.getHashes();
1329-
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
1331+
console.log(hashes); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
13301332
```
13311333

13321334
### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
@@ -1357,7 +1359,7 @@ Example:
13571359
const crypto = require('crypto');
13581360
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
13591361
if (err) throw err;
1360-
console.log(key.toString('hex')); // 'c5e478d...1469e50'
1362+
console.log(key.toString('hex')); // '3745e48...aa39b34'
13611363
});
13621364
```
13631365

@@ -1390,7 +1392,7 @@ Example:
13901392
```js
13911393
const crypto = require('crypto');
13921394
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'
13941396
```
13951397

13961398
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.
19381940
[`crypto.createHash()`]: #crypto_crypto_createhash_algorithm
19391941
[`crypto.createHmac()`]: #crypto_crypto_createhmac_algorithm_key
19401942
[`crypto.createSign()`]: #crypto_crypto_createsign_algorithm
1943+
[`crypto.createVerify()`]: #crypto_crypto_createverify_algorithm
19411944
[`crypto.getCurves()`]: #crypto_crypto_getcurves
19421945
[`crypto.getHashes()`]: #crypto_crypto_gethashes
19431946
[`crypto.pbkdf2()`]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.