# sm-crypto
小程序 js 库。国密算法 sm2、sm3 和 sm4 的实现。
> 使用此组件需要依赖小程序基础库 2.2.1 以上版本,同时依赖开发者工具的 npm 构建。具体详情可查阅[官方 npm 文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)。
## 安装
```bash
npm install --save miniprogram-sm-crypto
```
## sm2
### 获取密钥对
```js
const sm2 = require('miniprogram-sm-crypto').sm2
let keypair = sm2.generateKeyPairHex()
publicKey = keypair.publicKey // 公钥
privateKey = keypair.privateKey // 私钥
// 默认生成公钥 130 位太长,可以压缩公钥到 66 位
const compressedPublicKey = sm2.compressPublicKeyHex(publicKey) // compressedPublicKey 和 publicKey 等价
sm2.comparePublicKeyHex(publicKey, compressedPublicKey) // 判断公钥是否等价
// 自定义随机数,参数会直接透传给 jsbn 库的 BigInteger 构造器
// 注意:开发者使用自定义随机数,需要自行确保传入的随机数符合密码学安全
let keypair2 = sm2.generateKeyPairHex('123123123123123')
let keypair3 = sm2.generateKeyPairHex(256, SecureRandom)
let verifyResult = sm2.verifyPublicKey(publicKey) // 验证公钥
verifyResult = sm2.verifyPublicKey(compressedPublicKey) // 验证公钥
```
### 加密解密
```js
const sm2 = require('miniprogram-sm-crypto').sm2
const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果
encryptData = sm2.doEncrypt(msgArray, publicKey, cipherMode) // 加密结果,输入数组
decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode, {output: 'array'}) // 解密结果,输出数组
```
### 签名验签
> ps:理论上来说,只做纯签名是最快的。
```js
const sm2 = require('miniprogram-sm-crypto').sm2
// 纯签名 + 生成椭圆曲线点
let sigValueHex = sm2.doSignature(msg, privateKey) // 签名
let verifyResult = sm2.doVerifySignature(msg, sigValueHex, publicKey) // 验签结果
// 纯签名
let sigValueHex2 = sm2.doSignature(msg, privateKey, {
pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], // 传入事先已生成好的椭圆曲线点,可加快签名速度
}) // 签名
let verifyResult2 = sm2.doVerifySignature(msg, sigValueHex2, publicKey) // 验签结果
// 纯签名 + 生成椭圆曲线点 + der编解码
let sigValueHex3 = sm2.doSignature(msg, privateKey, {
der: true,
}) // 签名
let verifyResult3 = sm2.doVerifySignature(msg, sigValueHex3, publicKey, {
der: true,
}) // 验签结果
// 纯签名 + 生成椭圆曲线点 + sm3杂凑
let sigValueHex4 = sm2.doSignature(msg, privateKey, {
hash: true,
}) // 签名
let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, publicKey, {
hash: true,
}) // 验签结果
// 纯签名 + 生成椭圆曲线点 + sm3杂凑(不做公钥推导)
let sigValueHex5 = sm2.doSignature(msg, privateKey, {
hash: true,
publicKey, // 传入公钥的话,可以去掉sm3杂凑中推导公钥的过程,速度会比纯签名 + 生成椭圆曲线点 + sm3杂凑快
})
let verifyResult5 = sm2.doVerifySignature(msg, sigValueHex5, publicKey, {
hash: true,
publicKey,
})
// 纯签名 + 生成椭圆曲线点 + sm3杂凑 + 不做公钥推 + 添加 userId(长度小于 8192)
// 默认 userId 值为 1234567812345678
let sigValueHex6 = sm2.doSignature(msgString, privateKey, {
hash: true,
publicKey,
userId: 'testUserId',
})
let verifyResult6 = sm2.doVerifySignature(msgString, sigValueHex6, publicKey, {
hash: true,
userId: 'testUserId',
})
```
### 获取椭圆曲线点
```js
const sm2 = require('miniprogram-sm-crypto').sm2
let point = sm2.getPoint() // 获取一个椭圆曲线点,可在sm2签名时传入
```
## sm3
```js
const sm3 = require('miniprogram-sm-crypto').sm3
let hashData = sm3('abc') // 杂凑
// hmac
hashData = sm3('abc', {
key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组
})
```
## sm4
### 加密
```js
const sm4 = require('miniprogram-sm-crypto').sm4
const msg = 'hello world! 我是 juneandgreen.' // 可以为 utf8 串或字节数组
const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
```
### 解密
```js
const sm4 = require('miniprogram-sm-crypto').sm4
const encryptData = '0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c' // 可以为 16 进制串或字节数组
const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
```
## 协议
MIT
没有合适的资源?快使用搜索试试~ 我知道了~
基于单片机的微信小程序智能教室控制系统设计与实现
共1926个文件
js:412个
ts:386个
wxss:318个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 59 浏览量
2024-05-16
14:50:05
上传
评论 1
收藏 2.57MB ZIP 举报
温馨提示
【作品名称】:基于单片机的微信小程序智能教室控制系统设计与实现 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 功能实现 基于微信小程序实现教室灯光控制、亮度调节、亮度显示、温度显示、温控风扇控制、wifi或蓝牙数据采集、微信小程序显示等。 硬件部分 单片机基于Arduino和ESP8266 软件部分 微信小程序 主要分为登录功能和控制功能,控制功能包括自动控制和手动控制。 注册管理系统 登录管理系统后端基于node.js,数据库基于MySQL。
资源推荐
资源详情
资源评论
收起资源包目录
基于单片机的微信小程序智能教室控制系统设计与实现 (1926个子文件)
.babelrc 202B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
example.html 222B
Arduino.ino 8KB
Esp8266.ino 3KB
icon.jpg 97KB
mqtt.min.js 187KB
index.js 50KB
index.js 50KB
index.js 43KB
index.js 41KB
index.js 13KB
index.js 13KB
index.js 12KB
index.js 12KB
index.js 11KB
index.js 11KB
index.js 11KB
index.js 11KB
index.js 10KB
index.js 10KB
index.js 9KB
index.js 9KB
index.js 9KB
index.js 8KB
index.js 8KB
index.js 8KB
ec.js 8KB
index.js 8KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 7KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 6KB
index.js 5KB
index.js 5KB
index.js 5KB
index.js 5KB
index.js 5KB
index.js 5KB
index.js 5KB
transition.js 5KB
transition.js 5KB
index.js 5KB
index.js 5KB
utils.js 5KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
app.js 4KB
transition.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
utils.js 4KB
utils.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
sm3.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
server.js 4KB
index.js 4KB
index.js 4KB
asn1.js 4KB
index.js 4KB
index.js 3KB
index.js 3KB
index.js 3KB
transition.js 3KB
index.js 3KB
index.js 3KB
me.js 3KB
共 1926 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- 2301_804832112024-05-25发现一个超赞的资源,赶紧学习起来,大家一起进步,支持!
MarcoPage
- 粉丝: 3112
- 资源: 3467
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功