# Web Crypto
Web Crypto API (opens new window) 用于在 Web 应用中执行基础的密码学操作(如加密、解密、签名、密钥生成等)。它旨在为开发者提供安全、标准化的密码学功能,替代传统的不安全实现(如自行引入第三方加密库),同时确保操作在浏览器环境中安全可控。
- 哈希 (opens new window):SHA-256、SHA-384 等
- 加密 (opens new window)/解密 (opens new window):AES-CBC、AES-GCM、RSA-OAEP 等
- 数字签名 (opens new window):ECDSA、RSA-PSS 等
- 密钥生成与管理 (opens new window):HMAC、PBKDF2(密钥派生)等
# 一、Hash
计算 SHA-256 哈希
// 将字符串转为 ArrayBuffer async function sha256(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)); return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); } // 使用 sha256("Hello World").then(console.log); // 输出 SHA-256 哈希值直接计算小文件哈希
Web Crypto API 不支持流式哈希。
<input type="file" id="fileInput" /> <script> document.getElementById('fileInput').addEventListener('change', async (e) => { const file = e.target.files[0]; if (!file) return; // 读取文件内容为 ArrayBuffer const arrayBuffer = await file.arrayBuffer(); // 计算哈希 const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); console.log('SHA-256:', hashHex); }); </script>