crypto.randomFill(buffer[, offset][, size], callback)
-
buffer
<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 必须提供。所提供的buffer
的尺寸不得大于2**31 - 1
。¥
buffer
<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> Must be supplied. The size of the providedbuffer
must not be larger than2**31 - 1
. -
offset
<number> 默认值:0
¥
offset
<number> Default:0
-
size
<number> 默认值:buffer.length - offset
。size
不得大于2**31 - 1
。¥
size
<number> Default:buffer.length - offset
. Thesize
must not be larger than2**31 - 1
. -
callback
<Function>function(err, buf) {}
。
此函数类似于 crypto.randomBytes()
,但要求第一个参数是将被填充的 Buffer
。它还要求传入回调。
¥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.
如果未提供 callback
函数,则会抛出错误。
¥If the callback
function is not provided, an error will be thrown.
import { Buffer } from 'node:buffer';
const { randomFill } = await import('node:crypto');
const buf = Buffer.alloc(10);
randomFill(buf, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
randomFill(buf, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
// The above is equivalent to the following:
randomFill(buf, 5, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
const { randomFill } = require('node:crypto');
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(10);
randomFill(buf, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
randomFill(buf, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
// The above is equivalent to the following:
randomFill(buf, 5, 5, (err, buf) => {
if (err) throw err;
console.log(buf.toString('hex'));
});
任何 ArrayBuffer
、TypedArray
或 DataView
实例都可以作为 buffer
传入。
¥Any ArrayBuffer
, TypedArray
, or DataView
instance may be passed as
buffer
.
虽然这包括 Float32Array
和 Float64Array
的实例,但不应使用此函数生成随机浮点数。结果可能包含 +Infinity
、-Infinity
和 NaN
,即使数组只包含有限数字,它们也不是从均匀随机分布中抽取的,并且没有有意义的下限或上限。
¥While this includes instances of Float32Array
and Float64Array
, this
function should not be used to generate random floating-point numbers. The
result may contain +Infinity
, -Infinity
, and NaN
, and even if the array
contains finite numbers only, they are not drawn from a uniform random
distribution and have no meaningful lower or upper bounds.
import { Buffer } from 'node:buffer';
const { randomFill } = await import('node:crypto');
const a = new Uint32Array(10);
randomFill(a, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});
const b = new DataView(new ArrayBuffer(10));
randomFill(b, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});
const c = new ArrayBuffer(10);
randomFill(c, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf).toString('hex'));
});
const { randomFill } = require('node:crypto');
const { Buffer } = require('node:buffer');
const a = new Uint32Array(10);
randomFill(a, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});
const b = new DataView(new ArrayBuffer(10));
randomFill(b, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});
const c = new ArrayBuffer(10);
randomFill(c, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf).toString('hex'));
});
该 API 使用 libuv 的线程池,这对某些应用可能具有令人惊讶的负面性能影响;有关详细信息,请参阅 UV_THREADPOOL_SIZE
文档。
¥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.
crypto.randomFill()
的异步版本是在单个线程池请求中执行的。为了最大限度地减少线程池任务长度变化,在执行客户端请求时将大型 randomFill
请求分区。
¥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.