- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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
廣告
