Skip to content

Commit

Permalink
doc: correct crypto.randomFill() and randomFillSync()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Flarna authored and targos committed Aug 6, 2018
1 parent 6622ac7 commit 34300aa
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,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()`][].

Expand All @@ -2064,13 +2064,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)
Expand Down Expand Up @@ -2118,19 +2121,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'));
});
```

Expand Down

0 comments on commit 34300aa

Please sign in to comment.