• Node.js Video Tutorials

NodeJS - crypto.getRandomValues() 方法


NodeJs 的Crypto getRandomValues() 方法用于使用密码学安全的随机值填充指定的类型化数组。这些安全的随机值由该方法生成,然后填充到指定的类型化数组中。

安全的随机数是一个以不受任何因素约束的方式随机生成的数字,它高度不可预测,可用于安全敏感的应用程序,例如生成加密密钥、强密码或数字签名。

类型化数组是类似数组的对象,提供了一种在内存缓冲区中读取和写入原始二进制数据的机制。它们可以是任何类型的类型化数组,例如 Int8Array、Int16Array 或 Int32Array。

以下是使用此方法时需要注意的一些重要事项

  • 提供的类型化数组必须是整数类型。
  • 如果类型化数组的大小大于 65,536 字节,则此方法将抛出错误。

语法

以下是 NodeJs 的Crypto.getRandomValues() 方法的语法:

Crypto.getRandomValues(typedarray)

参数

此方法接受一个名为“typedarray”的参数,如下所述:

  • typedarray − 这是一个需要填充随机值的类型化数组。

返回值

此方法返回传递给它的相同类型化数组,其中填充了随机值。

示例 1

以下是 NodeJs crypto.getRandomValues() 方法的基本示例。

const crypto = require('crypto');

// Creating a Uint8Array with 5 elements
const array = new Uint8Array(5);

// Filling the array with random values
crypto.randomFillSync(array);

console.log(array); // prints the array of 5 random numbers

输出

上述程序产生以下输出:

Uint8Array(5) [ 66, 92, 165, 91, 88 ]

示例 2

以下是 NodeJs crypto getRandomValues() 方法的另一个示例。我们使用此方法将 10 个随机值填充到指定的类型化数组 (Int32Array) 中。

const crypto = require('crypto');

// Creating a Int32Array with 10 elements
const array = new Int32Array(10);

// Filling the array with random values
crypto.randomFillSync(array);

console.log(array); // prints the array of 10 random numbers

输出

上述程序产生以下输出:

Int32Array(10) [
  -1965795337, -1818733850,
   2025232344, -1120136716,
   1293668707, -1440944093,
   1948921771,   444689755,
   -975518890,   136290660
]

示例 3

如果指定的类型化数组大小大于 65,536 字节,则会抛出错误。

在下面的示例中,我们使用 crypto getRandomValues() 方法将随机值填充到指定的类型化数组中。

const { webcrypto } = require('crypto');

function generateLargeRandomValues(byteLength) {
  const chunkSize = 65537; // larger than 65536 bytes
  const numChunks = Math.ceil(byteLength / chunkSize);
  const remainder = byteLength % chunkSize;
  
  // Create a buffer to hold the entire result
  const resultBuffer = new Uint8Array(byteLength);

  // Generate random values for each chunk
  for (let i = 0; i < numChunks; i++) {
    const start = i * chunkSize;
    const end = (i + 1) * chunkSize > byteLength ? byteLength : (i + 1) * chunkSize;
    const currentChunkSize = end - start;
    
    const tempBuffer = new Uint8Array(currentChunkSize);
    webcrypto.getRandomValues(tempBuffer);
    
    resultBuffer.set(tempBuffer, start);
  }

  return resultBuffer;
}

const largeRandomValues = generateLargeRandomValues(100000);
console.log(largeRandomValues);

输出

执行上述程序后,它将显示以下输出:

node:internal/crypto/random:322
    throw lazyDOMException(
    ^

DOMException [QuotaExceededError]: The requested length exceeds 65,536 bytes
    at Crypto.getRandomValues (node:internal/crypto/random:322:11)
    at generateLargeRandomValues (/index.js:18:15)
    at Object. (/index.js:26:27)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
nodejs_crypto.htm
广告
© . All rights reserved.