• 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.