Node.js CryptoManager: master key in macOS Keychain, AES-256-GCM encryption
npm install @easynet/macos-data-encryption在 macOS 上把主密钥存进 Keychain,用 AES-256-GCM 加解密字符串,仅你的应用可读。Node ≥ 18,Keychain 需 macOS。
``bash`
npm install @easynet/macos-data-encryption
`ts
import { CryptoManager } from "@easynet/macos-data-encryption";
const manager = new CryptoManager({
service: "com.easynet.yourapp", // 建议用 bundle id
account: "master-key",
appBinPath: process.execPath, // 非 .app 包时必填
teamId: process.env.APPLE_TEAM_ID, // 可选,同 team 仅授权一次;不填则尝试从签名解析
});
await manager.init();
const encrypted = manager.encrypt("敏感内容");
const decrypted = manager.decrypt(encrypted);
`
- init():从 Keychain 读主密钥,没有则生成并写入。
- encrypt(plainText) / decrypt(cipherText):加解密。
- CryptoManager.isEncrypted(value):判断是否为加密串。
- 可选:allowPlaintextFallback、masterKey(测试用)。示例见 examples/。
`bash`
npx macos-data-encryption init # 确保 Keychain 里有 key
npx macos-data-encryption encrypt "原文" # 加密
npx macos-data-encryption decrypt "enc:v1:..." # 解密
支持 --service、--account、--team-id,详见 --help。
---
测试: npm test` · 协议: MIT