summaryrefslogtreecommitdiff
path: root/doc/api/crypto.md
diff options
context:
space:
mode:
authorMatteo Collina <hello@matteocollina.com>2019-01-07 15:37:34 +0100
committerMatteo Collina <hello@matteocollina.com>2019-01-10 11:19:38 +0100
commit2d2f82c4139f43dc3a657903aa23a641779d6ed0 (patch)
tree77fa223ca5163476fe9ca71895b06743c60cb257 /doc/api/crypto.md
parent842a35fbac5c29e8bb095fe3716375232ee0d0ce (diff)
downloadandroid-node-v8-2d2f82c4139f43dc3a657903aa23a641779d6ed0.tar.gz
android-node-v8-2d2f82c4139f43dc3a657903aa23a641779d6ed0.tar.bz2
android-node-v8-2d2f82c4139f43dc3a657903aa23a641779d6ed0.zip
doc: make sure that calls to .read() are looped
The 'readable' event assumes that calls to readable.read() happens within that event handler until readable.read() returns null. Fixes: https://github.com/nodejs/node/issues/20503 PR-URL: https://github.com/nodejs/node/pull/25375 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'doc/api/crypto.md')
-rw-r--r--doc/api/crypto.md21
1 files changed, 15 insertions, 6 deletions
diff --git a/doc/api/crypto.md b/doc/api/crypto.md
index 2605393878..54bbf7794e 100644
--- a/doc/api/crypto.md
+++ b/doc/api/crypto.md
@@ -200,9 +200,10 @@ const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = '';
cipher.on('readable', () => {
- const data = cipher.read();
- if (data)
- encrypted += data.toString('hex');
+ let chunk;
+ while (null !== (chunk = cipher.read())) {
+ encrypted += chunk.toString('hex');
+ }
});
cipher.on('end', () => {
console.log(encrypted);
@@ -383,9 +384,9 @@ const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
decipher.on('readable', () => {
- const data = decipher.read();
- if (data)
- decrypted += data.toString('utf8');
+ while (null !== (chunk = decipher.read())) {
+ decrypted += chunk.toString('utf8');
+ }
});
decipher.on('end', () => {
console.log(decrypted);
@@ -941,6 +942,8 @@ const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.on('readable', () => {
+ // Only one element is going to be produced by the
+ // hash stream.
const data = hash.read();
if (data) {
console.log(data.toString('hex'));
@@ -1033,6 +1036,8 @@ const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
hmac.on('readable', () => {
+ // Only one element is going to be produced by the
+ // hash stream.
const data = hmac.read();
if (data) {
console.log(data.toString('hex'));
@@ -1762,6 +1767,8 @@ const hash = crypto.createHash('sha256');
const input = fs.createReadStream(filename);
input.on('readable', () => {
+ // Only one element is going to be produced by the
+ // hash stream.
const data = input.read();
if (data)
hash.update(data);
@@ -1807,6 +1814,8 @@ const hmac = crypto.createHmac('sha256', 'a secret');
const input = fs.createReadStream(filename);
input.on('readable', () => {
+ // Only one element is going to be produced by the
+ // hash stream.
const data = input.read();
if (data)
hmac.update(data);