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