更新记录

1.0.2(2026-01-02) 下载此版本

  • Fix some bugs
  • Add AES and RSA support

平台兼容性

uni-app(3.6.15)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟

uni-app x(3.6.15)

Chrome Safari Android iOS 鸿蒙 微信小程序

rl-crypto

rl-crypto 是一个功能丰富的 UniApp / JS SDK 加密插件,最初移植自 Google Dart Crypto 包,并扩展了 AES、RSA 和 Base64 能力。

它旨在为 UniApp 开发者提供一站式的加密解决方案,兼容所有平台。

✨ 特性

  • 纯 JavaScript 实现:无原生依赖,兼容 UniApp 所有平台 (App-VUE, App-NVUE, H5, 小程序)。
  • 扩展加密支持:集成 CryptoJS (AES) 和 JSEncrypt (RSA)。
  • 支持算法
    • Hash: MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256
    • HMAC: 支持上述所有哈希算法
    • Symmetric: AES (支持 CBC, ECB 等模式)
    • Asymmetric: RSA (公钥加密/私钥解密, 签名/验签)
    • Encoding: Base64, Hex, Utf8

📦 安装

在插件市场导入 rl-crypto 到您的项目中。

🚀 快速上手

1. 基础哈希计算 (Hash)

import { md5, sha256 } from '@/uni_modules/rl-crypto/js_sdk';

const text = 'Hello World';
const bytes = new TextEncoder().encode(text); // 转换为 Uint8Array

// SHA-256
console.log('SHA-256:', sha256.convert(bytes).toString()); 

// MD5
console.log('MD5:', md5.convert(bytes).toString()); 

2. Base64 编码/解码

import { Base64, Utf8 } from '@/uni_modules/rl-crypto/js_sdk';

const text = 'Hello Base64';

// 编码 (String -> Base64)
const wordArray = Utf8.parse(text);
const encoded = Base64.stringify(wordArray);
console.log('Base64:', encoded); 
// Output: SGVsbG8gQmFzZTY0

// 解码 (Base64 -> String)
const parsed = Base64.parse(encoded);
const decoded = Utf8.stringify(parsed);
console.log('Decoded:', decoded);

3. AES 加密/解密

基于 CryptoJS 实现。

import { AES } from '@/uni_modules/rl-crypto/js_sdk';

const message = 'Secret Message';
const key = 'my-secret-key';

// 加密
// 注意:AES 导出的是 CryptoJS 对象,所以使用 AES.AES
const encrypted = AES.AES.encrypt(message, key).toString();
console.log('Encrypted:', encrypted);

// 解密
const decryptedBytes = AES.AES.decrypt(encrypted, key);
const decrypted = decryptedBytes.toString(AES.enc.Utf8);
console.log('Decrypted:', decrypted);

4. RSA 加密/解密

基于 JSEncrypt 实现。

import { RSA } from '@/uni_modules/rl-crypto/js_sdk';

const encryptor = new RSA();

// 设置公钥
const publicKey = `-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----`;
encryptor.setPublicKey(publicKey);

// 加密
const encrypted = encryptor.encrypt('Hello RSA');
console.log('RSA Encrypted:', encrypted);

// 解密 (需要私钥)
encryptor.setPrivateKey(`-----BEGIN RSA PRIVATE KEY-----...-----END RSA PRIVATE KEY-----`);
const decrypted = encryptor.decrypt(encrypted);
console.log('RSA Decrypted:', decrypted);

5. HMAC (密钥哈希)

import { Hmac, sha256 } from '@/uni_modules/cr-crypto/js_sdk';

const key = new TextEncoder().encode('secret-key');
const data = new TextEncoder().encode('message');

const hmac = new Hmac(sha256, key);
const digest = hmac.convert(data);

console.log('HMAC-SHA256:', digest.toString());

📄 许可证

MIT

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2024 rl-crypto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

暂无用户评论。