From df0068deab2ed435c1b4a2b962cac8b0928924bd Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich Date: Tue, 26 Jun 2018 23:49:47 +0200 Subject: [PATCH 1/3] doc: correct crypto.randomFill() and randomFillSync() Correct return type of `crypto.randomFillSync()` which is of same type as passed as `buffer` argument. Correct samples for `randomFill()` and `randomFillSync()` using a `TypeArray` or `DataView` as these types don't support `.toString(encoding)`. --- doc/api/crypto.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index ab33ff3a578d13..283d8b57adc443 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2041,7 +2041,7 @@ changes: * `buffer` {Buffer|TypedArray|DataView} Must be supplied. * `offset` {number} **Default:** `0` * `size` {number} **Default:** `buffer.length - offset` -* Returns: {Buffer} +* Returns: {Buffer|TypedArray|DataView} The object passed as `buffer` argument. Synchronous version of [`crypto.randomFill()`][]. @@ -2061,13 +2061,13 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. ```js const a = new Uint32Array(10); -console.log(crypto.randomFillSync(a).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(a).buffer).toString('hex')); const b = new Float64Array(10); -console.log(crypto.randomFillSync(b).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(b).buffer).toString('hex')); const c = new DataView(new ArrayBuffer(10)); -console.log(crypto.randomFillSync(c).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(c).buffer).toString('hex')); ``` ### crypto.randomFill(buffer[, offset][, size], callback) @@ -2115,19 +2115,19 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. const a = new Uint32Array(10); crypto.randomFill(a, (err, buf) => { if (err) throw err; - console.log(buf.toString('hex')); + console.log(Buffer.from(buf.buffer).toString('hex')); }); const b = new Float64Array(10); crypto.randomFill(b, (err, buf) => { if (err) throw err; - console.log(buf.toString('hex')); + console.log(Buffer.from(buf.buffer).toString('hex')); }); const c = new DataView(new ArrayBuffer(10)); crypto.randomFill(c, (err, buf) => { if (err) throw err; - console.log(buf.toString('hex')); + console.log(Buffer.from(buf.buffer).toString('hex')); }); ``` From 43052941cba0c706a343b390a94be7f746757a17 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich Date: Wed, 27 Jun 2018 15:52:22 +0200 Subject: [PATCH 2/3] use .byteOffset and .byteLength to construct Buffer --- doc/api/crypto.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 283d8b57adc443..e11165d1cc1f07 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2061,13 +2061,13 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. ```js const a = new Uint32Array(10); -console.log(Buffer.from(crypto.randomFillSync(a).buffer).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(a).buffer, a.byteOffset, a.byteLength).toString('hex')); const b = new Float64Array(10); -console.log(Buffer.from(crypto.randomFillSync(b).buffer).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(b).buffer, b.byteOffset, b.byteLength).toString('hex')); const c = new DataView(new ArrayBuffer(10)); -console.log(Buffer.from(crypto.randomFillSync(c).buffer).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(c).buffer, c.byteOffset, c.byteLength).toString('hex')); ``` ### crypto.randomFill(buffer[, offset][, size], callback) @@ -2115,19 +2115,19 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. const a = new Uint32Array(10); crypto.randomFill(a, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); }); const b = new Float64Array(10); crypto.randomFill(b, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); }); const c = new DataView(new ArrayBuffer(10)); crypto.randomFill(c, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); }); ``` From 6395f984b1c71b5fc82e79dad3b474193394a694 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich Date: Thu, 12 Jul 2018 22:41:50 +0200 Subject: [PATCH 3/3] fix lint --- doc/api/crypto.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index e11165d1cc1f07..6bea9387400cab 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2061,13 +2061,16 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. ```js const a = new Uint32Array(10); -console.log(Buffer.from(crypto.randomFillSync(a).buffer, a.byteOffset, a.byteLength).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(a).buffer, + a.byteOffset, a.byteLength).toString('hex')); const b = new Float64Array(10); -console.log(Buffer.from(crypto.randomFillSync(b).buffer, b.byteOffset, b.byteLength).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(b).buffer, + b.byteOffset, b.byteLength).toString('hex')); const c = new DataView(new ArrayBuffer(10)); -console.log(Buffer.from(crypto.randomFillSync(c).buffer, c.byteOffset, c.byteLength).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(c).buffer, + c.byteOffset, c.byteLength).toString('hex')); ``` ### crypto.randomFill(buffer[, offset][, size], callback) @@ -2115,19 +2118,22 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`. const a = new Uint32Array(10); crypto.randomFill(a, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) + .toString('hex')); }); const b = new Float64Array(10); crypto.randomFill(b, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) + .toString('hex')); }); const c = new DataView(new ArrayBuffer(10)); crypto.randomFill(c, (err, buf) => { if (err) throw err; - console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) + .toString('hex')); }); ```