diff options
author | Gerhard Stoebich <deb2001-github@yahoo.de> | 2018-06-26 23:49:47 +0200 |
---|---|---|
committer | Trivikram Kamat <16024985+trivikr@users.noreply.github.com> | 2018-08-05 10:55:07 -0700 |
commit | acc633c76e0ed9b54da96fcabf3106067d9e1a8e (patch) | |
tree | ec48a4c347b65fb267e4ecf479a1205b801ee1b8 /doc/api/crypto.md | |
parent | b07852d1f7a6bd29f32d0bb9b442f18d4ca6528d (diff) | |
download | android-node-v8-acc633c76e0ed9b54da96fcabf3106067d9e1a8e.tar.gz android-node-v8-acc633c76e0ed9b54da96fcabf3106067d9e1a8e.tar.bz2 android-node-v8-acc633c76e0ed9b54da96fcabf3106067d9e1a8e.zip |
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)`.
PR-URL: https://github.com/nodejs/node/pull/21550
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'doc/api/crypto.md')
-rw-r--r-- | doc/api/crypto.md | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 0e038d93e3..f870a5be98 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2035,7 +2035,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()`][]. @@ -2055,13 +2055,16 @@ 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, + a.byteOffset, a.byteLength).toString('hex')); const b = new Float64Array(10); -console.log(crypto.randomFillSync(b).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(crypto.randomFillSync(c).toString('hex')); +console.log(Buffer.from(crypto.randomFillSync(c).buffer, + c.byteOffset, c.byteLength).toString('hex')); ``` ### crypto.randomFill(buffer[, offset][, size], callback) @@ -2109,19 +2112,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(buf.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(buf.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(buf.toString('hex')); + console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) + .toString('hex')); }); ``` |