- 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
广告
