sm-crypto
国密算法sm2、sm3和sm4的js实现。
安装
npm install --save sm-crypto
sm2
获取密钥对
const sm2 = require('sm-crypto').sm2
let keypair = sm2.generateKeyPairHex()
publicKey = keypair.publicKey
privateKey = keypair.privateKey
const compressedPublicKey = sm2.compressPublicKeyHex(publicKey)
sm2.comparePublicKeyHex(publicKey, compressedPublicKey)
let keypair2 = sm2.generateKeyPairHex('123123123123123')
let keypair3 = sm2.generateKeyPairHex(256, SecureRandom)
let verifyResult = sm2.verifyPublicKey(publicKey)
verifyResult = sm2.verifyPublicKey(compressedPublicKey)
加密解密
const sm2 = require('sm-crypto').sm2
const cipherMode = 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:密文会在解密时自动补充 04
,如遇到其他工具补充的 04
需手动去除再传入。
签名验签
ps:理论上来说,只做纯签名是最快的。
const sm2 = require('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)
let sigValueHex3 = sm2.doSignature(msg, privateKey, {
der: true,
})
let verifyResult3 = sm2.doVerifySignature(msg, sigValueHex3, publicKey, {
der: true,
})
let sigValueHex4 = sm2.doSignature(msg, privateKey, {
hash: true,
})
let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, publicKey, {
hash: true,
})
let sigValueHex5 = sm2.doSignature(msg, privateKey, {
hash: true,
publicKey,
})
let verifyResult5 = sm2.doVerifySignature(msg, sigValueHex5, publicKey, {
hash: true,
publicKey,
})
let sigValueHex6 = sm2.doSignature(msgString, privateKey, {
hash: true,
publicKey,
userId: 'testUserId',
})
let verifyResult6 = sm2.doVerifySignature(msgString, sigValueHex6, publicKey, {
hash: true,
userId: 'testUserId',
})
获取椭圆曲线点
const sm2 = require('sm-crypto').sm2
let point = sm2.getPoint()
根据私钥获取公钥
const sm2 = require('sm-crypto).sm2
let publicKey = sm2.getPublicKeyFromPrivateKey(privateKey)
sm3
const sm3 = require('sm-crypto').sm3
let hashData = sm3('abc')
hashData = sm3('abc', {
key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9',
})
sm4
加密
const sm4 = require('sm-crypto').sm4
const msg = 'hello world! 我是 juneandgreen.'
const key = '0123456789abcdeffedcba9876543210'
let encryptData = sm4.encrypt(msg, key)
let encryptData = sm4.encrypt(msg, key, {padding: 'none'})
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'})
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'})
解密
const sm4 = require('sm-crypto').sm4
const encryptData = '0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c'
const key = '0123456789abcdeffedcba9876543210'
let decryptData = sm4.decrypt(encryptData, key)
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'})
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'})
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'})
其他实现
协议
MIT