Browse Source

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: https://github.com/nodejs/node/pull/10909
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Vse Mozhet Byt 8 years ago
committed by James M Snell
parent
commit
3e9c8ef53b
  1. 103
      doc/api/crypto.md

103
doc/api/crypto.md

@ -26,7 +26,7 @@ It is possible for Node.js to be built without including support for the
error being thrown. error being thrown.
```js ```js
var crypto; let crypto;
try { try {
crypto = require('crypto'); crypto = require('crypto');
} catch (err) { } catch (err) {
@ -132,9 +132,9 @@ Example: Using `Cipher` objects as streams:
const crypto = require('crypto'); const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password'); const cipher = crypto.createCipher('aes192', 'a password');
var encrypted = ''; let encrypted = '';
cipher.on('readable', () => { cipher.on('readable', () => {
var data = cipher.read(); const data = cipher.read();
if (data) if (data)
encrypted += data.toString('hex'); encrypted += data.toString('hex');
}); });
@ -166,7 +166,7 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
const crypto = require('crypto'); const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password'); const cipher = crypto.createCipher('aes192', 'a password');
var encrypted = cipher.update('some clear text data', 'utf8', 'hex'); let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
encrypted += cipher.final('hex'); encrypted += cipher.final('hex');
console.log(encrypted); console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504 // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
@ -269,9 +269,9 @@ Example: Using `Decipher` objects as streams:
const crypto = require('crypto'); const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password'); const decipher = crypto.createDecipher('aes192', 'a password');
var decrypted = ''; let decrypted = '';
decipher.on('readable', () => { decipher.on('readable', () => {
var data = decipher.read(); const data = decipher.read();
if (data) if (data)
decrypted += data.toString('utf8'); decrypted += data.toString('utf8');
}); });
@ -280,7 +280,7 @@ decipher.on('end', () => {
// Prints: some clear text data // Prints: some clear text data
}); });
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504'; const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
decipher.write(encrypted, 'hex'); decipher.write(encrypted, 'hex');
decipher.end(); decipher.end();
``` ```
@ -304,8 +304,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
const crypto = require('crypto'); const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password'); const decipher = crypto.createDecipher('aes192', 'a password');
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504'; const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
var decrypted = decipher.update(encrypted, 'hex', 'utf8'); let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8'); decrypted += decipher.final('utf8');
console.log(decrypted); console.log(decrypted);
// Prints: some clear text data // Prints: some clear text data
@ -402,18 +402,18 @@ const assert = require('assert');
// Generate Alice's keys... // Generate Alice's keys...
const alice = crypto.createDiffieHellman(2048); const alice = crypto.createDiffieHellman(2048);
const alice_key = alice.generateKeys(); const aliceKey = alice.generateKeys();
// Generate Bob's keys... // Generate Bob's keys...
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator()); const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bob_key = bob.generateKeys(); const bobKey = bob.generateKeys();
// Exchange and generate the secret... // Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key); const aliceSecret = alice.computeSecret(bobKey);
const bob_secret = bob.computeSecret(alice_key); const bobSecret = bob.computeSecret(aliceKey);
// OK // OK
assert.equal(alice_secret.toString('hex'), bob_secret.toString('hex')); assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
``` ```
### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding]) ### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])
@ -531,17 +531,17 @@ const assert = require('assert');
// Generate Alice's keys... // Generate Alice's keys...
const alice = crypto.createECDH('secp521r1'); const alice = crypto.createECDH('secp521r1');
const alice_key = alice.generateKeys(); const aliceKey = alice.generateKeys();
// Generate Bob's keys... // Generate Bob's keys...
const bob = crypto.createECDH('secp521r1'); const bob = crypto.createECDH('secp521r1');
const bob_key = bob.generateKeys(); const bobKey = bob.generateKeys();
// Exchange and generate the secret... // Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key); const aliceSecret = alice.computeSecret(bobKey);
const bob_secret = bob.computeSecret(alice_key); const bobSecret = bob.computeSecret(aliceKey);
assert(alice_secret, bob_secret); assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
// OK // OK
``` ```
@ -648,13 +648,14 @@ alice.setPrivateKey(
); );
// Bob uses a newly generated cryptographically strong // Bob uses a newly generated cryptographically strong
// pseudorandom key pair bob.generateKeys(); // pseudorandom key pair
bob.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
// alice_secret and bob_secret should be the same shared secret value // aliceSecret and bobSecret should be the same shared secret value
console.log(alice_secret === bob_secret); console.log(aliceSecret === bobSecret);
``` ```
## Class: Hash ## Class: Hash
@ -680,7 +681,7 @@ const crypto = require('crypto');
const hash = crypto.createHash('sha256'); const hash = crypto.createHash('sha256');
hash.on('readable', () => { hash.on('readable', () => {
var data = hash.read(); const data = hash.read();
if (data) if (data)
console.log(data.toString('hex')); console.log(data.toString('hex'));
// Prints: // Prints:
@ -763,7 +764,7 @@ const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret'); const hmac = crypto.createHmac('sha256', 'a secret');
hmac.on('readable', () => { hmac.on('readable', () => {
var data = hmac.read(); const data = hmac.read();
if (data) if (data)
console.log(data.toString('hex')); console.log(data.toString('hex'));
// Prints: // Prints:
@ -847,8 +848,8 @@ const sign = crypto.createSign('RSA-SHA256');
sign.write('some data to sign'); sign.write('some data to sign');
sign.end(); sign.end();
const private_key = getPrivateKeySomehow(); const privateKey = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); console.log(sign.sign(privateKey, 'hex'));
// Prints: the calculated signature // Prints: the calculated signature
``` ```
@ -860,8 +861,8 @@ const sign = crypto.createSign('RSA-SHA256');
sign.update('some data to sign'); sign.update('some data to sign');
const private_key = getPrivateKeySomehow(); const privateKey = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); console.log(sign.sign(privateKey, 'hex'));
// Prints: the calculated signature // Prints: the calculated signature
``` ```
@ -878,13 +879,14 @@ const sign = crypto.createSign('sha256');
sign.update('some data to sign'); sign.update('some data to sign');
const private_key = '-----BEGIN EC PRIVATE KEY-----\n' + const privateKey =
'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' + `-----BEGIN EC PRIVATE KEY-----
'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' + MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49
'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' + AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2
'-----END EC PRIVATE KEY-----\n'; pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
-----END EC PRIVATE KEY-----`;
console.log(sign.sign(private_key).toString('hex')); console.log(sign.sign(privateKey).toString('hex'));
``` ```
### sign.sign(private_key[, output_format]) ### sign.sign(private_key[, output_format])
@ -947,9 +949,9 @@ const verify = crypto.createVerify('RSA-SHA256');
verify.write('some data to sign'); verify.write('some data to sign');
verify.end(); verify.end();
const public_key = getPublicKeySomehow(); const publicKey = getPublicKeySomehow();
const signature = getSignatureToVerify(); const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); console.log(verify.verify(publicKey, signature));
// Prints: true or false // Prints: true or false
``` ```
@ -961,9 +963,9 @@ const verify = crypto.createVerify('RSA-SHA256');
verify.update('some data to sign'); verify.update('some data to sign');
const public_key = getPublicKeySomehow(); const publicKey = getPublicKeySomehow();
const signature = getSignatureToVerify(); const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); console.log(verify.verify(publicKey, signature));
// Prints: true or false // Prints: true or false
``` ```
@ -1192,7 +1194,7 @@ const hash = crypto.createHash('sha256');
const input = fs.createReadStream(filename); const input = fs.createReadStream(filename);
input.on('readable', () => { input.on('readable', () => {
var data = input.read(); const data = input.read();
if (data) if (data)
hash.update(data); hash.update(data);
else { else {
@ -1226,7 +1228,7 @@ const hmac = crypto.createHmac('sha256', 'a secret');
const input = fs.createReadStream(filename); const input = fs.createReadStream(filename);
input.on('readable', () => { input.on('readable', () => {
var data = input.read(); const data = input.read();
if (data) if (data)
hmac.update(data); hmac.update(data);
else { else {
@ -1278,7 +1280,7 @@ Example:
```js ```js
const curves = crypto.getCurves(); const curves = crypto.getCurves();
console.log(curves); // ['secp256k1', 'secp384r1', ...] console.log(curves); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
``` ```
### crypto.getDiffieHellman(group_name) ### crypto.getDiffieHellman(group_name)
@ -1307,11 +1309,11 @@ const bob = crypto.getDiffieHellman('modp14');
alice.generateKeys(); alice.generateKeys();
bob.generateKeys(); bob.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
/* alice_secret and bob_secret should be the same */ /* aliceSecret and bobSecret should be the same */
console.log(alice_secret == bob_secret); console.log(aliceSecret === bobSecret);
``` ```
### crypto.getHashes() ### crypto.getHashes()
@ -1326,7 +1328,7 @@ Example:
```js ```js
const hashes = crypto.getHashes(); const hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...] console.log(hashes); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
``` ```
### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback) ### crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
@ -1357,7 +1359,7 @@ Example:
const crypto = require('crypto'); const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => { crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
if (err) throw err; if (err) throw err;
console.log(key.toString('hex')); // 'c5e478d...1469e50' console.log(key.toString('hex')); // '3745e48...aa39b34'
}); });
``` ```
@ -1390,7 +1392,7 @@ Example:
```js ```js
const crypto = require('crypto'); const crypto = require('crypto');
const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512'); const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
console.log(key.toString('hex')); // 'c5e478d...1469e50' console.log(key.toString('hex')); // '3745e48...aa39b34'
``` ```
An array of supported digest functions can be retrieved using 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.
[`crypto.createHash()`]: #crypto_crypto_createhash_algorithm [`crypto.createHash()`]: #crypto_crypto_createhash_algorithm
[`crypto.createHmac()`]: #crypto_crypto_createhmac_algorithm_key [`crypto.createHmac()`]: #crypto_crypto_createhmac_algorithm_key
[`crypto.createSign()`]: #crypto_crypto_createsign_algorithm [`crypto.createSign()`]: #crypto_crypto_createsign_algorithm
[`crypto.createVerify()`]: #crypto_crypto_createverify_algorithm
[`crypto.getCurves()`]: #crypto_crypto_getcurves [`crypto.getCurves()`]: #crypto_crypto_getcurves
[`crypto.getHashes()`]: #crypto_crypto_gethashes [`crypto.getHashes()`]: #crypto_crypto_gethashes
[`crypto.pbkdf2()`]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback [`crypto.pbkdf2()`]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback

Loading…
Cancel
Save