Skip to content

Commit 5a860d9

Browse files
garrowsrvagg
authored andcommitted
doc: Examples work when data exceeds buffer size
PR-URL: #4811 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Shigeki Ohtsu <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 71ba14d commit 5a860d9

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

doc/api/crypto.markdown

+24-12
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,18 @@ Example: Using `Cipher` objects as streams:
102102
const crypto = require('crypto');
103103
const cipher = crypto.createCipher('aes192', 'a password');
104104

105+
var encrypted = '';
105106
cipher.on('readable', () => {
106107
var data = cipher.read();
107108
if (data)
108-
console.log(data.toString('hex'));
109-
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
109+
encrypted += data.toString('hex');
110+
});
111+
cipher.on('end', () => {
112+
console.log(encrypted);
113+
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
110114
});
111115

112-
cipher.write('clear text data');
116+
cipher.write('some clear text data');
113117
cipher.end();
114118
```
115119

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

135-
cipher.update('clear text data');
136-
console.log(cipher.final('hex'));
137-
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
139+
var encrypted = cipher.update('some clear text data', 'utf8', 'hex');
140+
encrypted += cipher.final('hex');
141+
console.log(encrypted);
142+
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
138143
```
139144

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

220+
var decrypted = '';
215221
decipher.on('readable', () => {
216222
var data = decipher.read();
217223
if (data)
218-
console.log(data.toString());
219-
// Prints: clear text data
224+
decrypted += data.toString('utf8');
225+
});
226+
decipher.on('end', () => {
227+
console.log(decrypted);
228+
// Prints: some clear text data
220229
});
221230

222-
decipher.write('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
231+
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
232+
decipher.write(encrypted, 'hex');
223233
decipher.end();
224234
```
225235

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

245-
decipher.update('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
246-
console.log(decipher.final('utf8'));
247-
// Prints: clear text data
255+
var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
256+
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
257+
decrypted += decipher.final('utf8');
258+
console.log(decrypted);
259+
// Prints: some clear text data
248260
```
249261

250262
### decipher.final([output_encoding])

0 commit comments

Comments
 (0)