npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crypto-pack

v0.0.1

Published

浏览器环境和 Node.js 环境通用的加解密工具集

Downloads

1

Readme

crypto-pack

浏览器环境和 Node.js 环境通用的加解密工具集

Node.js 在 v15 版本引入了同 W3C 一致的 Web Cryptography API,在浏览器端和 Node 环境可以使用相同的函数进行加解密。

本仓库在 WebCryptoAPI 的基础上封装 AES 和 RSA 工具集,进一步简化加解密模块的使用。

Installation

HTML script 标签引入:

<script src="https://unpkg.com/crypto-pack/lib/browser.js"></script>

<script>
  const { cryptoPack } = window;
  const { RSA } = cryptoPack;
</script>

npm 引入:

npm install crypto-pack --save

ES6 module / TypeScript:

// 浏览器
import { RSA } from "crypto-pack/lib/browser";

// Node.js
import { RSA } from "crypto-pack/lib/node";

Node.js:

// Node.js
const { RSA } = require("crypto-pack/lib/node");

通用函数集

AES

import { AES } from "crypto-pack/lib/browser";
// or
import { AES } from "crypto-pack/lib/node";

生成AES密钥,返回 base64 字符串

await AES.generateKey(l ?: number)

AES加解密

// AES 加密,返回 base64 字符串
await AES.encrypt(key: string, iv: string, msg: string)

// AES 解密
await AES.decrypt(key: string, iv: string, t: string)

RSA

import { RSA } from "crypto-pack/lib/browser";
// or
import { RSA } from "crypto-pack/lib/node";

生成密钥对

await RSA.generateKeyPair()

// 对于只需要在内存生成一次密钥对的场景,可以使用工具集提供的闭包函数
const getRsaKeyPair = await RSA.getKeyPairClosure();

const { publicKey, privateKey } = await getRsaKeyPair();

RSA 公钥加密

await RSA.publicEncrypt(msg: string, key: string)

RSA 私钥解密

await RSA.privateDecrypt(eMsg: string, key: string)

AES + RSA

import { encryptWithAesRsaPb, decryptWithAesRsaPv } from "crypto-pack/lib/browser";
// or
import { encryptWithAesRsaPb, decryptWithAesRsaPv } from "crypto-pack/lib/node";

非对称加密通常只公开只能加密的公钥,解密的私钥不对外公开,所以安全性会更高。缺点是对加密的内容长度有限制,并且相比于对称加密性能开销更大。

在双端通信过程中,更好的做法是,生成随机对称加密密钥,使用生成的对称密钥加密主内容,再使用对端的非对称公钥加密对称密钥。

这样发给对端的是公钥加密后的对称密钥和对称密钥加密的主内容。对端收到内容后反向解密,先使用私钥解密对称密钥,再使用对称密钥解密主内容。

对于这个常见的通信场景,封装了如下函数:

RSA Public Key + AES 加密

await encryptWithAesRsaPb(data: string, publicKey: string)

RSA Private Key + AES 解密

// 解密函数添加了防重放功能,timeoutSecond 参数如果大于 0,防重放功能生效,单位为秒
await decryptWithAesRsaPv(
  encryptAesData: string,
  encryptAesKey: string,
  privateKey: string,
  // 防重放
  timeoutSecond = -1
)

Node.js 独有的函数

生成 RSA 密钥对并保存到本地

import { RSA_GenerateKeyPairToFile } from "crypto-pack/lib/node";

await RSA_GenerateKeyPairToFile()