1.0.14 • Published 1 month ago

@sparta-utils/crypto-util v1.0.14

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

@sparta-utils/crypto-util

🚀 一个统一的加密解密工具库,支持常见加密算法(AES、RSA、SHA、MD5、HMAC、Base64、JWT)以及国密算法(SM2、SM3、SM4)。

  • ✅ AES
  • ✅ RSA
  • ✅ SM2 / SM3 / SM4 (国密)
  • ✅ SHA256 / SHA512
  • ✅ HMAC
  • ✅ MD5
  • ✅ Base64 encode/decode
  • ✅ JWT (sign/verify/decode)

📦 安装

npm install @sparta-utils/crypto-util

或者使用 yarn:

yarn add @sparta-utils/crypto-util

🔰 快速使用

import { CryptoUtil } from '@sparta-utils/crypto-util';

// ✅ AES 加密解密
import { CryptoUtil } from '@sparta-utils/crypto-util'

const key = '1234567890abcdef'
const iv = 'abcdef1234567890' // 可选,如果使用 ECB 模式可以不传

const encrypted = CryptoUtil.aes.encrypt('Hello AES', {
  key,
  iv,
  mode: 'cbc'
})
console.log('加密后:', encrypted)

const decrypted = CryptoUtil.aes.decrypt(encrypted, {
  key,
  iv,
  mode: 'cbc'
})

console.log('解密后:', decrypted)

// ✅ RSA 密钥生成、加密解密、签名验签
const { publicKey: rsaPub, privateKey: rsaPriv } = CryptoUtil.rsa.generateKeyPair();
const rsaEncrypted = CryptoUtil.rsa.encrypt('Hello RSA', rsaPub);
const rsaDecrypted = CryptoUtil.rsa.decrypt(rsaEncrypted, rsaPriv);
const rsaSignature = CryptoUtil.rsa.sign('RSA Message', rsaPriv);
const rsaVerified = CryptoUtil.rsa.verify('RSA Message', rsaSignature, rsaPub);
console.log('RSA验签:', rsaVerified);

// ✅ SM2 密钥生成、加密解密、签名验签
const { publicKey: rsaPub, privateKey: rsaPriv } = CryptoUtil.rsa.generateKeyPair()
console.log('RSA公钥:', rsaPub)
console.log('RSA私钥:', rsaPriv)
const rsaEncrypted = CryptoUtil.rsa.encrypt('Hello RSA', rsaPub)
console.log('RSA加密:', rsaEncrypted)
const rsaDecrypted = CryptoUtil.rsa.decrypt(rsaEncrypted, rsaPriv)
console.log('RSA解密:', rsaDecrypted)
const rsaSignature = CryptoUtil.rsa.sign('RSA Message', rsaPriv)
console.log('RSA签名:', rsaSignature)
const rsaVerified = CryptoUtil.rsa.verify('RSA Message', rsaSignature, rsaPub)
console.log('RSA验签结果:', rsaVerified)

// ✅ SM3 摘要
const sm3Hash = CryptoUtil.sm3('Hello SM3')
console.log('SM3摘要:', sm3Hash)

// ✅ SM4 加密解密
const sm4Key = '0123456789abcdef'; // 16字节密钥
const sm4Encrypted = CryptoUtil.sm4.encrypt('Hello SM4', sm4Key);
const sm4Decrypted = CryptoUtil.sm4.decrypt(sm4Encrypted, sm4Key);
console.log('SM4解密:', sm4Decrypted);

// ✅ SHA256 / SHA512
const sha256Hash = CryptoUtil.sha.sha256('Hello SHA256');
const sha512Hash = CryptoUtil.sha.sha512('Hello SHA512');
console.log('SHA256:', sha256Hash);
console.log('SHA512:', sha512Hash);

// ✅ HMAC-SHA
const hmac256 = CryptoUtil.hmac.hmac('sha256', 'hmacKey', 'HMAC Message')
const hmac512 = CryptoUtil.hmac.hmac('sha512', 'hmacKey', 'HMAC Message')

console.log('HMAC-SHA256:', hmac256)
console.log('HMAC-SHA512:', hmac512)

// ✅ MD5
const md5Hash = CryptoUtil.md5('Hello MD5');
console.log('MD5:', md5Hash);

// ✅ Base64
const base64Encoded = CryptoUtil.base64.encode('Hello Base64')
console.log('Base64编码:', base64Encoded)
const base64Decoded = CryptoUtil.base64.decode(base64Encoded)
console.log('Base64解码:', base64Decoded)

// ✅ JWT 签发与验证 浏览器不可使用 / 当前方法尚不完善
const jwtToken = CryptoUtil.jwt.sign({ uid: 1001 }, 'jwtSecretKey');
const jwtPayload = CryptoUtil.jwt.verify(jwtToken, 'jwtSecretKey');
console.log('JWT验证:', jwtPayload);

📚 支持功能

模块功能方法示例
AES对称加密encrypt, decrypt
RSA非对称加密/签名generateKeyPair, encrypt, decrypt, sign, verify
SM2国密加密/签名generateKeyPair, encrypt, decrypt, sign, verify
SM3国密摘要算法sm3(message)
SM4国密对称加密encrypt, decrypt
SHA摘要(sha256、sha512)sha256, sha512
HMAC带密钥摘要(HMAC-SHA)hmacSHA256, hmacSHA512
MD5MD5 哈希md5(message)
Base64编码与解码encode, decode
JWTToken 生成与校验sign, verify, decode

🧪 加密方法详解

🔐 AES (对称加密)

方法说明参数说明
encrypt加密字符串(text: string, key: string) => string
decrypt解密字符串(cipherText: string, key: string) => string

🔑 RSA (非对称加密)

方法说明参数说明
generateKeyPair生成密钥对() => { publicKey: string, privateKey: string }
encrypt公钥加密(text: string, publicKey: string) => string
decrypt私钥解密(cipherText: string, privateKey: string) => string
sign私钥签名(text: string, privateKey: string) => string
verify公钥验证签名(text: string, signature: string, publicKey: string) => boolean

🧧 SM2 (国密非对称加密)

方法说明参数说明
generateKeyPair生成密钥对() => { publicKey: string, privateKey: string }
encrypt公钥加密(text: string, publicKey: string) => string
decrypt私钥解密(cipherText: string, privateKey: string) => string
sign签名(text: string, privateKey: string) => string
verify验签(text: string, signature: string, publicKey: string) => boolean

🧮 SM3 (国密摘要)

方法说明参数说明
sm3生成摘要(text: string) => string

🔒 SM4 (对称加密)

方法说明参数说明
encrypt加密(text: string, key: string) => string
decrypt解密(cipherText: string, key: string) => string

📌 SHA 摘要算法

方法说明参数说明
sha256SHA256 哈希(text: string) => string
sha512SHA512 哈希(text: string) => string

🔐 HMAC 加密(带密钥的哈希)

方法说明参数说明
hmacSHA256SHA256 HMAC(text: string, key: string) => string
hmacSHA512SHA512 HMAC(text: string, key: string) => string

🧬 MD5

方法说明参数说明
md5MD5 哈希(text: string) => string

🔤 Base64

方法说明参数说明
encodeBase64 编码(text: string) => string
decodeBase64 解码(base64: string) => string

🔑 JWT (JSON Web Token)

方法说明参数说明
sign生成 token(payload: object, secret: string, options?: SignOptions) => string
verify验证 token 有效性(token: string, secret: string) => object \| string
decode解码 token(token: string) => null \| { [key: string]: any }

📄 License MIT License © 2025-present