Browse Source

doc: Examples work when data exceeds buffer size

PR-URL: https://github.com/nodejs/node/pull/4811
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
process-exit-stdio-flushing
Glen Arrowsmith 9 years ago
committed by James M Snell
parent
commit
26073dd1c1
  1. 36
      doc/api/crypto.markdown

36
doc/api/crypto.markdown

@ -102,14 +102,18 @@ 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 = '';
cipher.on('readable', () => { cipher.on('readable', () => {
var data = cipher.read(); var data = cipher.read();
if (data) if (data)
console.log(data.toString('hex')); encrypted += data.toString('hex');
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d });
cipher.on('end', () => {
console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
}); });
cipher.write('clear text data'); cipher.write('some clear text data');
cipher.end(); cipher.end();
``` ```
@ -132,9 +136,10 @@ 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');
cipher.update('clear text data'); var encrypted = cipher.update('some clear text data', 'utf8', 'hex');
console.log(cipher.final('hex')); encrypted += cipher.final('hex');
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
``` ```
### cipher.final([output_encoding]) ### cipher.final([output_encoding])
@ -212,14 +217,19 @@ 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 = '';
decipher.on('readable', () => { decipher.on('readable', () => {
var data = decipher.read(); var data = decipher.read();
if (data) if (data)
console.log(data.toString()); decrypted += data.toString('utf8');
// Prints: clear text data });
decipher.on('end', () => {
console.log(decrypted);
// Prints: some clear text data
}); });
decipher.write('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex'); var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
decipher.write(encrypted, 'hex');
decipher.end(); decipher.end();
``` ```
@ -242,9 +252,11 @@ 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');
decipher.update('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex'); var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
console.log(decipher.final('utf8')); var decrypted = decipher.update(encrypted, 'hex', 'utf8');
// Prints: clear text data decrypted += decipher.final('utf8');
console.log(decrypted);
// Prints: some clear text data
``` ```
### decipher.final([output_encoding]) ### decipher.final([output_encoding])

Loading…
Cancel
Save