crypto.randomFillSync(buffer[, offset][, size])
-
buffer<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 必须提供。所提供的buffer的尺寸不得大于2**31 - 1。¥
buffer<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> Must be supplied. The size of the providedbuffermust 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. Thesizemust not be larger than2**31 - 1. -
返回:<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 对象作为
buffer参数传入。¥Returns: <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> The object passed as
bufferargument.
crypto.randomFill() 的同步版本。
¥Synchronous version of crypto.randomFill().
import { Buffer } from 'node:buffer';
const { randomFillSync } = await import('node:crypto');
const buf = Buffer.alloc(10);
console.log(randomFillSync(buf).toString('hex'));
randomFillSync(buf, 5);
console.log(buf.toString('hex'));
// The above is equivalent to the following:
randomFillSync(buf, 5, 5);
console.log(buf.toString('hex'));const { randomFillSync } = require('node:crypto');
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(10);
console.log(randomFillSync(buf).toString('hex'));
randomFillSync(buf, 5);
console.log(buf.toString('hex'));
// The above is equivalent to the following:
randomFillSync(buf, 5, 5);
console.log(buf.toString('hex'));任何 ArrayBuffer、TypedArray 或 DataView 实例都可以作为 buffer 传入。
¥Any ArrayBuffer, TypedArray or DataView instance may be passed as
buffer.
import { Buffer } from 'node:buffer';
const { randomFillSync } = await import('node:crypto');
const a = new Uint32Array(10);
console.log(Buffer.from(randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));
const b = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));
const c = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(c)).toString('hex'));const { randomFillSync } = require('node:crypto');
const { Buffer } = require('node:buffer');
const a = new Uint32Array(10);
console.log(Buffer.from(randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));
const b = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));
const c = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(c)).toString('hex'));