- 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 使用 Where 条件
- Node.js - MySQL 使用 Order By 排序
- 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.subtle 属性
NodeJs 的 Crypto.subtle 是 crypto 接口的一个实例且只读属性。它用于检索可用于执行低级加密操作的 Subtle Crypto。
SubtleCrypto 是 NodeJs 中的一个接口,它提供了一些低级加密函数。
加密操作是用于保护信息的常用基本过程,通过将其转换为无法识别形式来保护信息,使未经授权的人无法访问。
这些操作包括数据加密、数据完整性、身份验证、信息认证和不可否认性。
语法
以下是 NodeJs Crypto.subtle 属性的语法:
Crypto.subtle
参数
- 由于它是一个属性,因此不接受任何参数。
返回值
此属性返回 SubtleCrypto 接口。
示例 1
以下是 NodeJs Crypto.subtle 属性的基本示例。
const crypto = require('crypto').webcrypto;
const message = 'Hello, world!';
async function createHash(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
createHash(message).then(hash => console.log('SHA-256 hash of ', message, 'is: ', hash));
输出
以上程序产生以下输出:
SHA-256 hash of Hello, world! is: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
示例 2
以下是 NodeJs Crypto.subtle 属性的另一个示例。我们使用此属性检索 CryptoSubtle 以执行低级加密操作,例如数据加密和解密。
const { webcrypto } = require('crypto');
async function generateKey() {
return webcrypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']);
}
async function encryptData(data, key) {
const iv = webcrypto.getRandomValues(new Uint8Array(12));
const encryptedData = await webcrypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, new TextEncoder().encode(data));
return { iv, encryptedData: new Uint8Array(encryptedData) };
}
async function decryptData(encryptedData, iv, key) {
const decryptedDataBuffer = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedData);
return new TextDecoder().decode(decryptedDataBuffer);
}
(async () => {
const key = await generateKey();
const data = 'Tutorialspoint!';
const { iv, encryptedData } = await encryptData(data, key);
console.log('Encrypted Data:', encryptedData);
const decryptedData = await decryptData(encryptedData, iv, key);
console.log('Decrypted Data:', decryptedData);
})();
输出
执行以上程序后,将显示以下输出:
Encrypted Data: Uint8Array(31) [ 223, 193, 14, 42, 142, 251, 166, 92, 192, 167, 135, 65, 143, 66, 41, 20, 51, 227, 13, 10, 151, 81, 221, 140, 41, 183, 157, 1, 181, 252, 251 ] Decrypted Data: Tutorialspoint!
nodejs_crypto.htm
广告
