crypto.randomFill(buffer[, offset][, size], callback)
buffer{Buffer|TypedArray|DataView} Must be supplied.offset{number} Default:0size{number} Default:buffer.length - offsetcallback{Function}function(err, buf) {}.
This function is similar to [crypto.randomBytes()][] but requires the first
argument to be a [Buffer][] that will be filled. It also
requires that a callback is passed in.
If the callback function is not provided, an error will be thrown.
const buf = Buffer.alloc(10);
crypto.randomFill(buf, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
crypto.randomFill(buf, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
// The above is equivalent to the following:
crypto.randomFill(buf, 5, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
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'),
);
});
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'),
);
});
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'),
);
});
This API uses libuv’s threadpool, which can have surprising and
negative performance implications for some applications; see the
[UV_THREADPOOL_SIZE][] documentation for more information.
The asynchronous version of crypto.randomFill() is carried out in a single
threadpool request. To minimize threadpool task length variation, partition
large randomFill requests when doing so as part of fulfilling a client
request.
Feedback
Was this page helpful?
很高兴听到! 请告诉我们,我们如何才能改善.
很遗憾听到这个消息。 请告诉我们,我们如何才能改善.