更新记录
1.0.0(2026-04-05)
1.0.0
平台兼容性
uni-app x(4.0)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | √ | √ | √ | × |
xxl-sm-crypto
国密算法 SM2/SM3/SM4 UTS 插件,支持 Android、iOS、HarmonyOS。
平台支持
| 平台 | 底层实现 |
|---|---|
| Android | Bouncy Castle |
| iOS | GmSSL.xcframework |
| HarmonyOS | @kit.CryptoArchitectureKit |
数据格式约定
- SM2 公钥:
04开头,130 位 hex(非压缩公钥) - SM2 私钥:64 位 hex
- SM2 密文:
04 + C1C3C2(hex) - SM2 签名:
der: true-> DER,30开头der: false或不传 ->04 + rs(130 位 hex)
- SM3 / SM3-HMAC:返回 64 位 hex
- SM4 key:32 位 hex(16 字节)
- SM4 CBC iv:建议使用 32 位 hex(跨平台一致)
类型定义
type KeyPairHex = {
privateKey: string
publicKey: string
}
type SM2SignatureOptions = {
der?: boolean
}
type SM4ModeInterface = {
mode?: string
iv?: string
}
引入方式
import { XxlSmCrypto, KeyPairHex, SM2SignatureOptions, SM4ModeInterface } from '@/uni_modules/xxl-sm-crypto'
API
1) XxlSmCrypto.sm2GenerateKeyPairHex()
生成 SM2 密钥对。公钥 hex(04 开头),私钥 hex(64 位)。
- 参数:无
- 返回:
KeyPairHex
const keyPair = XxlSmCrypto.sm2GenerateKeyPairHex() as KeyPairHex
const publicKey = keyPair.publicKey
const privateKey = keyPair.privateKey
2) XxlSmCrypto.sm2Encrypt(msg, publicKey)
SM2 公钥加密。
- 参数:
msg: string明文publicKey: string公钥 hex(04 开头)
- 返回:
string密文 hex(04+C1C3C2),失败返回""
const encrypted = XxlSmCrypto.sm2Encrypt('Hello你好123.!', publicKey)
3) XxlSmCrypto.sm2Decrypt(encryptData, privateKey)
SM2 私钥解密。
- 参数:
encryptData: string密文 hexprivateKey: string私钥 hex(64 位)
- 返回:
string解密后的明文,失败返回""
const decrypted = XxlSmCrypto.sm2Decrypt(encrypted, privateKey)
4) XxlSmCrypto.sm2Signature(msg, privateKey, options?)
SM2 签名。
- 参数:
msg: string待签名内容privateKey: string私钥 hex(64 位)options?: SM2SignatureOptions{ der: true }返回 DER(30 开头){ der: false }返回04+rs
- 返回:
string签名 hex,失败返回""
const sigDer = XxlSmCrypto.sm2Signature('Hello你好123.!', privateKey, { der: true })
const sig04rs = XxlSmCrypto.sm2Signature('Hello你好123.!', privateKey, { der: false })
5) XxlSmCrypto.sm2VerifySignature(msg, signHex, publicKey, options?)
SM2 验签。
- 参数:
msg: string原文signHex: string签名 hexpublicKey: string公钥 hex(04 开头)options?: SM2SignatureOptions- 验 DER:传
{ der: true } - 验
04+rs:传{ der: false }
- 返回:
boolean是否通过
const okDer = XxlSmCrypto.sm2VerifySignature('Hello你好123.!', sigDer, publicKey, { der: true })
const ok04 = XxlSmCrypto.sm2VerifySignature('Hello你好123.!', sig04rs, publicKey, { der: false })
6) XxlSmCrypto.sm3(input)
SM3 哈希。
- 参数:
input: string输入字符串
- 返回:
string64 位 hex,失败返回""
const hash = XxlSmCrypto.sm3('Hello你好123.!')
7) XxlSmCrypto.sm3Hmac(data, key)
SM3-HMAC。
- 参数:
data: string消息字符串key: string密钥字符串
- 返回:
string64 位 hex,失败返回""
const key = '30a86dc9056c44cc05420fec26927021'
const mac = XxlSmCrypto.sm3Hmac('Hello你好123.!', key)
8) XxlSmCrypto.sm4Encrypt(data, key, mode?)
SM4 加密,支持 ECB/CBC。
- 参数:
data: string明文key: string32 位 hex keymode?: SM4ModeInterface- null -> ECB
{ mode: 'cbc', iv: 'ae8cd6b9d7805f959e6dbf78c1b56d2e' }-> CBC
- 返回:
string密文 hex,失败返回""
const key = '0123456789abcdeffedcba9876543210'
// ECB
const sm4Enc = XxlSmCrypto.sm4Encrypt('Hello你好123.!', key, null)
// CBC
const mode : SM4ModeInterface = { mode: 'cbc', iv: 'ae8cd6b9d7805f959e6dbf78c1b56d2e' }
const sm4Enc = XxlSmCrypto.sm4Encrypt('Hello你好123.!', key, mode)
9) XxlSmCrypto.sm4Decrypt(data, key, mode?)
SM4 解密,支持 ECB/CBC。
- 参数:
data: string密文 hexkey: string32 位 hex keymode?: SM4ModeInterface(需与加密时一致)
- 返回:
string明文,失败返回""
// ECB
const sm4Dec = XxlSmCrypto.sm4Decrypt(sm4Enc, key, null)
// CBC
const sm4Dec = XxlSmCrypto.sm4Decrypt(sm4Enc, key, mode)
完整示例
import { KeyPairHex, SM2SignatureOptions, SM4ModeInterface, XxlSmCrypto } from '@/uni_modules/xxl-sm-crypto'
const msg = 'Hello你好123.!'
// SM2
const keyPair = XxlSmCrypto.sm2GenerateKeyPairHex() as KeyPairHex
const encrypted = XxlSmCrypto.sm2Encrypt(msg, keyPair.publicKey)
const decrypted = XxlSmCrypto.sm2Decrypt(encrypted, keyPair.privateKey)
const options : SM2SignatureOptions = { der: true }
const signature = XxlSmCrypto.sm2Signature(msg, keyPair.privateKey, options)
const verified = XxlSmCrypto.sm2VerifySignature(msg, signature, keyPair.publicKey, options)
// SM3
const sm3 = XxlSmCrypto.sm3(msg)
// SM3-HMAC
const key = '0123456789abcdeffedcba9876543210'
const sm3Hmac = XxlSmCrypto.sm3Hmac(msg, key)
// SM4
const sm4Key = '0123456789abcdeffedcba9876543210'
// ECB
const sm4Enc = XxlSmCrypto.sm4Encrypt(msg, sm4Key, null)
const sm4Dec = XxlSmCrypto.sm4Decrypt(sm4Enc, sm4Key, null)
// CBC
const mode : SM4ModeInterface = { mode: 'cbc', iv: 'ae8cd6b9d7805f959e6dbf78c1b56d2e' }
const sm4Enc = XxlSmCrypto.sm4Encrypt(msg, sm4Key, mode)
const sm4Dec = XxlSmCrypto.sm4Decrypt(sm4Enc, sm4Key, mode)
License
MIT

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 596
赞赏 0
下载 11465831
赞赏 1902
赞赏
京公网安备:11010802035340号