Skip to content

Commit 34300aa

Browse files
Flarnatargos
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6622ac7 commit 34300aa

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
@@ -2044,7 +2044,7 @@ changes:
20442044
* `buffer` {Buffer|TypedArray|DataView} Must be supplied.
20452045
* `offset` {number} **Default:** `0`
20462046
* `size` {number} **Default:** `buffer.length - offset`
2047-
* Returns: {Buffer}
2047+
* Returns: {Buffer|TypedArray|DataView} The object passed as `buffer` argument.
20482048

20492049
Synchronous version of [`crypto.randomFill()`][].
20502050

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

20652065
```js
20662066
const a = new Uint32Array(10);
2067-
console.log(crypto.randomFillSync(a).toString('hex'));
2067+
console.log(Buffer.from(crypto.randomFillSync(a).buffer,
2068+
a.byteOffset, a.byteLength).toString('hex'));
20682069

20692070
const b = new Float64Array(10);
2070-
console.log(crypto.randomFillSync(b).toString('hex'));
2071+
console.log(Buffer.from(crypto.randomFillSync(b).buffer,
2072+
b.byteOffset, b.byteLength).toString('hex'));
20712073

20722074
const c = new DataView(new ArrayBuffer(10));
2073-
console.log(crypto.randomFillSync(c).toString('hex'));
2075+
console.log(Buffer.from(crypto.randomFillSync(c).buffer,
2076+
c.byteOffset, c.byteLength).toString('hex'));
20742077
```
20752078

20762079
### crypto.randomFill(buffer[, offset][, size], callback)
@@ -2118,19 +2121,22 @@ Any `TypedArray` or `DataView` instance may be passed as `buffer`.
21182121
const a = new Uint32Array(10);
21192122
crypto.randomFill(a, (err, buf) => {
21202123
if (err) throw err;
2121-
console.log(buf.toString('hex'));
2124+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2125+
.toString('hex'));
21222126
});
21232127

21242128
const b = new Float64Array(10);
21252129
crypto.randomFill(b, (err, buf) => {
21262130
if (err) throw err;
2127-
console.log(buf.toString('hex'));
2131+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2132+
.toString('hex'));
21282133
});
21292134

21302135
const c = new DataView(new ArrayBuffer(10));
21312136
crypto.randomFill(c, (err, buf) => {
21322137
if (err) throw err;
2133-
console.log(buf.toString('hex'));
2138+
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2139+
.toString('hex'));
21342140
});
21352141
```
21362142

0 commit comments

Comments
 (0)