Node.js 一鍵加密,安全無憂:easy-cipher-mate 全面上手指南
在 Web 開發(fā)中,加密總是讓人頭疼。JavaScript 本身沒有內(nèi)置強(qiáng)大的加密工具,而現(xiàn)有的庫要么復(fù)雜難用,要么功能有限。更別提那些需要快速加密文件或文本的場景,根本沒有一款好用的命令行工具!??
今天給大家推薦一款超實用的 npm 包——easy-cipher-mate。它不僅提供了簡單易用的 API,還附帶強(qiáng)大的 CLI 工具,無論是文件加密、文本加密,還是多語言支持,都能輕松搞定!??
一、快速安裝與上手
1. 安裝
只需一行命令,即可安裝 easy-cipher-mate:
npm install easy-cipher-mate
或者使用 yarn:
yarn add easy-cipher-mate
2. CLI 工具的使用
安裝完成后,你可以直接使用 CLI 工具加密文件或文本文件。以下是幾個常見場景:
(1)加密文件
easy-cipher-mate encrypt-file -i input.txt -o output.txt -p yourpassword
(2)解密文件
easy-cipher-mate decrypt-file -i encrypted.txt -o decrypted.txt -p yourpassword
(3)逐行加密文本文件
easy-cipher-mate encrypt-text-file -f input.txt -p yourpassword
(4)逐行解密文本文件
easy-cipher-mate decrypt-text-file -f encrypted.txt -p yourpassword
二、API 使用指南
easy-cipher-mate 提供了簡潔的 API,支持 AES-GCM 和 ChaCha20-Poly1305 兩種加密算法。以下是實際場景中的使用示例:
1. 加密敏感數(shù)據(jù)
import { AESGCMEncryption, AESGCMEncryptionConfigFromJSON } from 'easy-cipher-mate';
const encryption = new AESGCMEncryption();
const config = new AESGCMEncryptionConfigFromJSON({
password: 'yourpassword',
textEncoding: 'utf-8'
});
const data = 'Sensitive information';
const encrypted = await encryption.encryptText(data, config);
console.log('Encrypted:', encrypted.data);
2. 解密數(shù)據(jù)
const decrypted = await encryption.decryptText(encrypted.data, config);
console.log('Decrypted:', decrypted);
3. 文件加密與解密
import { readFileSync } from 'fs';
const fileBuffer = readFileSync('document.pdf');
const encryptedFile = await encryption.encryptFile(fileBuffer.buffer, config);
// 保存加密后的文件
const decryptedFile = await encryption.decryptFile(encryptedFile.data, config);
三、實際場景:多語言文本加密
easy-cipher-mate 支持多種文本編碼,包括 utf-8
、ascii
、utf16le
、base64
等。以下是如何加密中文和日語文本的示例:
1. 加密中文文本
const chineseText = '你好,世界';
const encryptedChinese = await encryption.encryptText(chineseText, config);
const decryptedChinese = await encryption.decryptText(encryptedChinese.data, config);
console.log(decryptedChinese === chineseText); // 輸出:true
2. 加密日語文本
const japaneseText = 'こんにちは世界';
const encryptedJapanese = await encryption.encryptText(japaneseText, config, 'utf16le');
const decryptedJapanese = await encryption.decryptText(encryptedJapanese.data, config, 'utf16le');
console.log(decryptedJapanese === japaneseText); // 輸出:true
四、原理揭秘:如何實現(xiàn)高效加密?
easy-cipher-mate 的核心是基于 Web Crypto API 的加密算法實現(xiàn)。以下是 AES-GCM 的加密流程:
- 密鑰生成:通過 PBKDF2 從密碼和鹽值生成加密密鑰。
- 加密:使用 AES-GCM 算法對數(shù)據(jù)進(jìn)行加密。
- 解密:使用相同的密鑰和算法對密文進(jìn)行解密。
async function deriveKey(password: string, salt: Uint8Array): Promise<CryptoKey> {
const encoder = new TextEncoder();
const keyMaterial = await crypto.subtle.importKey(
"raw",
encoder.encode(password),
"PBKDF2",
false,
["deriveKey"]
);
return crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt,
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
}
五、總結(jié)
easy-cipher-mate 是一款集簡單、高效、安全于一體的加密工具。無論是日常開發(fā)中的敏感數(shù)據(jù)加密,還是命令行工具的快速使用,它都能滿足你的需求。趕快試試吧!??
npm install easy-cipher-mate