Node.js 一键加密,安全无忧:easy-cipher-mate 全面上手指南

开发
easy-cipher-mate 是一款集简单、高效、安全于一体的加密工具。无论是日常开发中的敏感数据加密,还是命令行工具的快速使用,它都能满足你的需求。赶快试试吧!

在 Web 开发中,加密总是让人头疼。JavaScript 本身没有内置强大的加密工具,而现有的库要么复杂难用,要么功能有限。更别提那些需要快速加密文件或文本的场景,根本没有一款好用的命令行工具!😫

今天给大家推荐一款超实用的 npm 包——easy-cipher-mate。它不仅提供了简单易用的 API,还附带强大的 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. 加密敏感数据

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. 解密数据

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-8asciiutf16lebase64 等。以下是如何加密中文和日语文本的示例:

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

四、原理揭秘:如何实现高效加密?

easy-cipher-mate 的核心是基于 Web Crypto API 的加密算法实现。以下是 AES-GCM 的加密流程:

  1. 密钥生成:通过 PBKDF2 从密码和盐值生成加密密钥。
  2. 加密:使用 AES-GCM 算法对数据进行加密。
  3. 解密:使用相同的密钥和算法对密文进行解密。
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"]
  );
}

五、总结

easy-cipher-mate 是一款集简单、高效、安全于一体的加密工具。无论是日常开发中的敏感数据加密,还是命令行工具的快速使用,它都能满足你的需求。赶快试试吧!🚀

npm install easy-cipher-mate


责任编辑:武晓燕 来源: Piper蛋窝
相关推荐

2021-02-01 15:42:45

Node.jsSQL应用程序

2011-11-10 08:55:00

Node.js

2015-03-10 10:59:18

Node.js开发指南基础介绍

2021-01-26 08:37:18

MobXVueReact

2015-06-30 08:41:55

Node.js指南

2019-03-29 16:40:02

Node.js多线程前端

2011-10-31 14:57:37

2014-06-24 09:41:56

Android Stu教程

2024-01-29 00:36:50

Backstage设施工具

2013-06-19 11:26:39

2024-04-12 08:13:24

2021-04-23 10:38:52

Spring BootSpringMVC源码

2021-08-25 06:33:52

Node.jsVscode调试工具

2013-12-04 13:27:56

Android SDK项目

2013-12-04 14:44:41

Android SDK用户交互

2013-12-26 15:40:33

Android SDK项目

2014-01-22 10:00:10

Android SDKAndroid开发

2016-06-20 10:20:22

Docker云计算

2021-11-26 09:40:37

EclipseIDEA开发

2014-08-01 09:57:52

Node.jsNode.js插件
点赞
收藏

51CTO技术栈公众号