更新记录
1.0.0(2026-07-19)
首发:自研高性能原生 base64 / 哈希(md5·sha1·sha256·sha512·HMAC) / 加解密(口令派生 + AES-256-GCM) 核心,提供
平台兼容性
uni-app x(5.14)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| √ | √ | 5.0 | 12 | × | × |
nex-crypto —— 高性能加解密 / 编解码
自研高性能原生 base64 / 哈希 / 加解密核心,提供 uni-app 可调的 JS API。支持 App 端(Android / iOS) 与 H5 端 (双端同一核心引擎、同真值,见下「H5 端」);小程序不支持。
同一份 utssdk 插件同时支持 uni-app x(uvue) 与 经典 uni-app(vue3),无需分叉。 两类数据形态:文本进出(base64 / 哈希 / HMAC / 文本加解密返回字符串)与 文件进出(文件哈希返回 hex;文件 base64 / 加解密读写文件、返回
CryptoFileResult)。 随机源走系统级 CSPRNG,无额外系统依赖。
API
| 组 | JS API | 签名 | 说明 |
|---|---|---|---|
| Base64 | base64Encode |
base64Encode(text): string |
UTF-8 文本 → base64 |
base64Decode |
base64Decode(b64): string |
base64 → UTF-8 文本(非法 → 抛错) | |
base64EncodeFile |
base64EncodeFile(src): string |
读文件 → base64 字符串 | |
base64DecodeToFile |
base64DecodeToFile(b64, dest): CryptoFileResult |
base64 → 写文件 | |
| 哈希 | hashText |
hashText(text, algo): string |
文本哈希 → 小写 hex;algo ∈ md5/sha1/sha256/sha512 |
hashFile |
hashFile(src, algo): string |
文件流式哈希 → 小写 hex | |
hmacSha256 |
hmacSha256(text, keyText): string |
HMAC-SHA256 → 小写 hex | |
| 加解密 | encryptText |
encryptText(plaintext, password): string |
→ base64 token |
decryptText |
decryptText(token, password): string |
token → 明文 | |
encryptFile |
encryptFile(src, dest, password): CryptoFileResult |
文件加密 | |
decryptFile |
decryptFile(src, dest, password): CryptoFileResult |
文件解密 |
类型
// 文件型操作返回(字段 camelCase)
type CryptoFileResult = { sizeBytes: number; costMs: number };
- 哈希
algo仅支持md5/sha1/sha256/sha512,其它值抛参数错误。 base64Decode对非法 base64 或非 UTF-8 字节抛解码错误。- 加解密方案:口令 →
PBKDF2-HMAC-SHA256(随机 16 字节盐,迭代 600000 次)→ 32 字节密钥;AES-256-GCM(随机 12 字节 nonce)认证加密。打包布局salt(16) || nonce(12) || ciphertext+tag(encryptText再 base64 成 token)。 - 加密是认证加密(AEAD):口令错误或密文被篡改,解密会抛解密错误(不会返回错误明文)。
- 同一明文 + 同一口令两次加密,token 不同(盐 / nonce 随机),但都能正确解回。
CryptoFileResult.sizeBytes为写入文件字节数;costMs只计「处理段」耗时(不含磁盘 IO)。
用法
import {
base64Encode, base64Decode,
hashText, hmacSha256,
encryptText, decryptText
} from "@/uni_modules/nex-crypto";
// base64
const b64 = base64Encode("hello"); // "aGVsbG8="
const text = base64Decode(b64); // "hello"
// 哈希
const sha = hashText("hello", "sha256"); // 2cf24dba...
const mac = hmacSha256("payload", "secret"); // HMAC-SHA256 hex
// 加解密(口令派生 AES-256-GCM)
const token = encryptText("敏感数据", "my-password");
const plain = decryptText(token, "my-password"); // "敏感数据"
// 文件:hashFile / encryptFile / decryptFile / base64EncodeFile 同理(路径进出)
H5 端
- H5 端与 App 端由同一自研核心驱动,同算法、同真值(无需额外静态资源部署)。
- 文本型 6 API 全部可用(base64Encode/base64Decode/hashText/hmacSha256/encryptText/decryptText);随机源走浏览器
crypto.getRandomValues。 - 文件型 5 API H5 不支持(浏览器无文件系统),调用抛错。
- H5 端引擎初始化是异步的:建议应用启动时(仅 H5 分支)
await ensureReady()一次, 过早调用会抛「尚未就绪」错误。
平台与调试边界
- app-android / app-ios / H5(web);各家小程序不支持。
- Android 本地真机调试原生库需 HBuilderX ≥ 4.26(自定义调试基座),云打包不受此限。
- iOS 端原生库含 Swift,宿主原生工程需开启「支持 Swift」。
- 最终 App 打包由 HBuilderX /
@dcloudioCLI 完成。

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 1178
赞赏 0
下载 12438100
赞赏 1934
赞赏
京公网安备:11010802035340号