Crypto
TypeScript Crypto API 与 .NET CryptoUtil 的公开方法及算法名称大小写保持一致。AES-GCM、密码 AES 载荷、PBKDF2 密码哈希和 PEM 密钥支持两端互操作。
ts
import { AESDecryptWithPassword, AESEncryptWithPassword } from "@fast-china/utils";
const payload = await AESEncryptWithPassword("受保护内容", "correct horse battery staple");
const plaintext = await AESDecryptWithPassword(payload, "correct horse battery staple");
plaintext;
const jsonPayload = await AESEncryptWithPassword('{"id":1}', "correct horse battery staple");
const result = (await AESDecryptWithPassword(jsonPayload, "correct horse battery staple")).parseJson<{ id: number }>();Base64 与 Crypto 的文本解码/解密入口返回原始字符串类型 DecodedText,可以直接作为 string 使用;只有显式调用 .parseJson<T = any>() 才尝试解析 JSON,解析失败时直接返回原始字符串。首次文本解码会按需安装不可枚举的 String.prototype.parseJson,若同名方法已被其他实现占用则明确抛错。泛型不会验证不可信 JSON 的实际结构,也不保证运行时结果一定是对象。
密码存储使用 HashPasswordPBKDF2SHA256 和 VerifyPasswordPBKDF2SHA256。MD5、SHA-1、AES-CBC 与 AES-ECB 不提供密码存储或认证加密保证。完整方法列表和安全边界见 API 文档。
兼容哈希与 AES API 使用的 CryptoJS 已内联到 ESM 和 CDN 产物,消费项目无需安装或解析 crypto-js 子路径;未使用 Crypto API 的应用仍可通过 Tree Shaking 移除独立的加密模块。
