Skip to content

Commit acc633c

Browse files
Flarnatrivikr
authored andcommitted
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: #21550 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b07852d commit acc633c

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

doc/api/crypto.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ changes:
20352035
* `buffer` {Buffer|TypedArray|DataView} Must be supplied.
20362036
* `offset` {number} **Default:** `0`
20372037
* `size` {number} **Default:** `buffer.length - offset`
2038-
* Returns: {Buffer}
2038+
* Returns: {Buffer|TypedArray|DataView} The object passed as `buffer` argument.
20392039

20402040
Synchronous version of [`crypto.randomFill()`][].
20412041

@@ -2055,13 +2055,16 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`.
20552055

20562056
```js
20572057
const a = new Uint32Array(10);
2058-
console.log(crypto.randomFillSync(a).toString('hex'));
2058+
console.log(Buffer.from(crypto.randomFillSync(a).buffer,
2059+
a.byteOffset, a.byteLength).toString('hex'));
20592060

20602061
const b = new Float64Array(10);
2061-
console.log(crypto.randomFillSync(b).toString('hex'));
2062+
console.log(Buffer.from(crypto.randomFillSync(b).buffer,
2063+
b.byteOffset, b.byteLength).toString('hex'));
20622064

20632065
const c = new DataView(new ArrayBuffer(10));
2064-
console.log(crypto.randomFillSync(c).toString('hex'));
2066+
console.log(Buffer.from(crypto.randomFillSync(c).buffer,
2067+
c.byteOffset, c.byteLength).toString('hex'));
20652068
```
20662069

20672070
### crypto.randomFill(buffer[, offset][, size], callback)
@@ -2109,19 +2112,22 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`.
21092112
const a = new Uint32Array(10);
21102113
crypto.randomFill(a, (err, buf) => {
21112114
if (err) throw err;
2112-
console.log(buf.toString('hex'));
2115+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2116+
.toString('hex'));
21132117
});
21142118

21152119
const b = new Float64Array(10);
21162120
crypto.randomFill(b, (err, buf) => {
21172121
if (err) throw err;
2118-
console.log(buf.toString('hex'));
2122+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2123+
.toString('hex'));
21192124
});
21202125

21212126
const c = new DataView(new ArrayBuffer(10));
21222127
crypto.randomFill(c, (err, buf) => {
21232128
if (err) throw err;
2124-
console.log(buf.toString('hex'));
2129+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2130+
.toString('hex'));
21252131
});
21262132
```
21272133

0 commit comments

Comments
 (0)