{"id":13647305,"url":"https://github.com/wechat-miniprogram/sm-crypto","last_synced_at":"2025-05-16T13:03:37.608Z","repository":{"id":38430545,"uuid":"144463333","full_name":"wechat-miniprogram/sm-crypto","owner":"wechat-miniprogram","description":"miniprogram sm crypto library","archived":false,"fork":false,"pushed_at":"2023-10-16T07:19:23.000Z","size":558,"stargazers_count":451,"open_issues_count":11,"forks_count":88,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-12T06:18:54.941Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wechat-miniprogram.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-08-12T12:04:57.000Z","updated_at":"2025-04-08T05:27:48.000Z","dependencies_parsed_at":"2023-02-16T19:00:49.367Z","dependency_job_id":"be3b2b1e-0f67-48fa-88e1-df018f2cdf19","html_url":"https://github.com/wechat-miniprogram/sm-crypto","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fsm-crypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fsm-crypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fsm-crypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fsm-crypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wechat-miniprogram","download_url":"https://codeload.github.com/wechat-miniprogram/sm-crypto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525139,"owners_count":21118620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T01:03:28.892Z","updated_at":"2025-04-12T06:19:02.251Z","avatar_url":"https://github.com/wechat-miniprogram.png","language":"JavaScript","readme":"# sm-crypto\n\n小程序 js 库。国密算法 sm2、sm3 和 sm4 的实现。\n\n\u003e 使用此组件需要依赖小程序基础库 2.2.1 以上版本，同时依赖开发者工具的 npm 构建。具体详情可查阅[官方 npm 文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)。\n\n## 安装\n\n```bash\nnpm install --save miniprogram-sm-crypto\n```\n\n## sm2\n\n### 获取密钥对\n\n```js\nconst sm2 = require('miniprogram-sm-crypto').sm2\n\nlet keypair = sm2.generateKeyPairHex()\n\npublicKey = keypair.publicKey // 公钥\nprivateKey = keypair.privateKey // 私钥\n\n// 默认生成公钥 130 位太长，可以压缩公钥到 66 位\nconst compressedPublicKey = sm2.compressPublicKeyHex(publicKey) // compressedPublicKey 和 publicKey 等价\nsm2.comparePublicKeyHex(publicKey, compressedPublicKey) // 判断公钥是否等价\n\n// 自定义随机数，参数会直接透传给 jsbn 库的 BigInteger 构造器\n// 注意：开发者使用自定义随机数，需要自行确保传入的随机数符合密码学安全\nlet keypair2 = sm2.generateKeyPairHex('123123123123123')\nlet keypair3 = sm2.generateKeyPairHex(256, SecureRandom)\n\nlet verifyResult = sm2.verifyPublicKey(publicKey) // 验证公钥\nverifyResult = sm2.verifyPublicKey(compressedPublicKey) // 验证公钥\n```\n\n### 加密解密\n\n```js\nconst sm2 = require('miniprogram-sm-crypto').sm2\nconst cipherMode = 1 // 1 - C1C3C2，0 - C1C2C3，默认为1\n\nlet encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果\nlet decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果\n\nencryptData = sm2.doEncrypt(msgArray, publicKey, cipherMode) // 加密结果，输入数组\ndecryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode, {output: 'array'}) // 解密结果，输出数组\n```\n\n\u003e ps：密文会在解密时自动补充 `04`，如遇到其他工具补充的 `04` 需手动去除再传入。\n\n### 签名验签\n\n\u003e ps：理论上来说，只做纯签名是最快的。\n\n```js\nconst sm2 = require('miniprogram-sm-crypto').sm2\n\n// 纯签名 + 生成椭圆曲线点\nlet sigValueHex = sm2.doSignature(msg, privateKey) // 签名\nlet verifyResult = sm2.doVerifySignature(msg, sigValueHex, publicKey) // 验签结果\n\n// 纯签名\nlet sigValueHex2 = sm2.doSignature(msg, privateKey, {\n    pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], // 传入事先已生成好的椭圆曲线点，可加快签名速度\n}) // 签名\nlet verifyResult2 = sm2.doVerifySignature(msg, sigValueHex2, publicKey) // 验签结果\n\n// 纯签名 + 生成椭圆曲线点 + der编解码\nlet sigValueHex3 = sm2.doSignature(msg, privateKey, {\n    der: true,\n}) // 签名\nlet verifyResult3 = sm2.doVerifySignature(msg, sigValueHex3, publicKey, {\n    der: true,\n}) // 验签结果\n\n// 纯签名 + 生成椭圆曲线点 + sm3杂凑\nlet sigValueHex4 = sm2.doSignature(msg, privateKey, {\n    hash: true,\n}) // 签名\nlet verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, publicKey, {\n    hash: true,\n}) // 验签结果\n\n// 纯签名 + 生成椭圆曲线点 + sm3杂凑（不做公钥推导）\nlet sigValueHex5 = sm2.doSignature(msg, privateKey, {\n    hash: true,\n    publicKey, // 传入公钥的话，可以去掉sm3杂凑中推导公钥的过程，速度会比纯签名 + 生成椭圆曲线点 + sm3杂凑快\n})\nlet verifyResult5 = sm2.doVerifySignature(msg, sigValueHex5, publicKey, {\n    hash: true,\n    publicKey,\n})\n\n// 纯签名 + 生成椭圆曲线点 + sm3杂凑 + 不做公钥推 + 添加 userId（长度小于 8192）\n// 默认 userId 值为 1234567812345678\nlet sigValueHex6 = sm2.doSignature(msgString, privateKey, {\n    hash: true,\n    publicKey,\n    userId: 'testUserId',\n})\nlet verifyResult6 = sm2.doVerifySignature(msgString, sigValueHex6, publicKey, {\n    hash: true,\n    userId: 'testUserId',\n})\n```\n\n### 获取椭圆曲线点\n\n```js\nconst sm2 = require('miniprogram-sm-crypto').sm2\n\nlet point = sm2.getPoint() // 获取一个椭圆曲线点，可在sm2签名时传入\n```\n\n### 根据私钥获取公钥\n\n```js\nconst sm2 = require('sm-crypto).sm2\n\nlet publicKey = sm2.getPublicKeyFromPrivateKey(privateKey)\n```\n\n## sm3\n\n```js\nconst sm3 = require('miniprogram-sm-crypto').sm3\n\nlet hashData = sm3('abc') // 杂凑\n\n// hmac\nhashData = sm3('abc', {\n    key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组\n})\n```\n\n## sm4\n\n### 加密\n\n```js\nconst sm4 = require('miniprogram-sm-crypto').sm4\nconst msg = 'hello world! 我是 juneandgreen.' // 可以为 utf8 串或字节数组\nconst key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组，要求为 128 比特\n\nlet encryptData = sm4.encrypt(msg, key) // 加密，默认输出 16 进制字符串，默认使用 pkcs#7 填充（传 pkcs#5 也会走 pkcs#7 填充）\nlet encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密，不使用 padding\nlet encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密，不使用 padding，输出为字节数组\nlet encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密，cbc 模式\n```\n\n### 解密\n\n```js\nconst sm4 = require('miniprogram-sm-crypto').sm4\nconst encryptData = '0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c' // 可以为 16 进制串或字节数组\nconst key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组，要求为 128 比特\n\nlet decryptData = sm4.decrypt(encryptData, key) // 解密，默认输出 utf8 字符串，默认使用 pkcs#7 填充（传 pkcs#5 也会走 pkcs#7 填充）\nlet decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密，不使用 padding\nlet decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密，不使用 padding，输出为字节数组\nlet decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密，cbc 模式\n```\n\n## 协议\n\nMIT\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fsm-crypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwechat-miniprogram%2Fsm-crypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fsm-crypto/lists"}