Skip to content

Commit

Permalink
doc: Examples work when data exceeds buffer size
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
garrows authored and Myles Borins committed Feb 22, 2016
1 parent e3e2042 commit 3f876b1
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions doc/api/crypto.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ Example: Using `Cipher` objects as streams:
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');

var encrypted = '';
cipher.on('readable', () => {
var data = cipher.read();
if (data)
console.log(data.toString('hex'));
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
encrypted += data.toString('hex');
});
cipher.on('end', () => {
console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
});

cipher.write('clear text data');
cipher.write('some clear text data');
cipher.end();
```

Expand All @@ -132,9 +136,10 @@ Example: Using the `cipher.update()` and `cipher.final()` methods:
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');

cipher.update('clear text data');
console.log(cipher.final('hex'));
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
var encrypted = cipher.update('some clear text data', 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
```

### cipher.final([output_encoding])
Expand Down Expand Up @@ -212,14 +217,19 @@ Example: Using `Decipher` objects as streams:
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');

var decrypted = '';
decipher.on('readable', () => {
var data = decipher.read();
if (data)
console.log(data.toString());
// Prints: clear text data
decrypted += data.toString('utf8');
});
decipher.on('end', () => {
console.log(decrypted);
// Prints: some clear text data
});

decipher.write('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
decipher.write(encrypted, 'hex');
decipher.end();
```

Expand All @@ -242,9 +252,11 @@ Example: Using the `decipher.update()` and `decipher.final()` methods:
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');

decipher.update('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
console.log(decipher.final('utf8'));
// Prints: clear text data
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);
// Prints: some clear text data
```

### decipher.final([output_encoding])
Expand Down

0 comments on commit 3f876b1

Please sign in to comment.