一、环境配置
在之前的node.js库配置中,我们已经配置好了node和npm,再次检查配置情况
node -v
npm -v
npm install --save miniprogram-sm-crypto
二、进入工作目录/usr/local/bin/node_modules/sm-crypto
vim sm2.js
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
const msgString = "20201307lcy"
const cipherMode = 1; // 1 - C1C3C2,0 - C1C2C3,默认为1
const encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode); // 加密结果
console.log("original data:");
console.log(msgString);
console.log("encrypted data:");
console.log(encryptData);
const decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode); // 解密结果
console.log("decrypted data:");
console.log(decryptData);
三、代码验证
1 sm2加解密
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
const msgString = "20201307lcy"
const cipherMode = 1; // 1 - C1C3C2,0 - C1C2C3,默认为1
const encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode); // 加密结果
console.log("original data:");
console.log(msgString);
console.log("encrypted data:");
console.log(encryptData);
const decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode); // 解密结果
console.log("decrypted data:");
console.log(decryptData);
2 sm2签名验签
- 1 纯签名
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
const msg = "20201307lcy"
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
// 纯签名
let sigValueHex = sm2.doSignature(msg, privateKey); // 签名
console.log(sigValueHex);
let verifyResult = sm2.doVerifySignature(msg, sigValueHex, publicKey); // 验签结果
console.log(verifyResult);
- 2 纯签名 + 生成椭圆曲线点
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
const msg = "20201307lcy"
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
// 纯签名 + 生成椭圆曲线点
let sigValueHex2 = sm2.doSignature(msg, privateKey, {
pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], // 传入事先已生成好的椭圆曲线点,可加快签名速度
}); // 签名
console.log(sigValueHex2);
let verifyResult2 = sm2.doVerifySignature(msg, sigValueHex2, publicKey); // 验签结果
console.log(verifyResult2);
- 3 纯签名 + 生成椭圆曲线点 + der编解码
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
const msg = "20201307lcy"
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
// 纯签名 + 生成椭圆曲线点 + der编解码
let sigValueHex3 = sm2.doSignature(msg, privateKey, {
der: true,
}); // 签名
console.log(sigValueHex3);
let verifyResult3 = sm2.doVerifySignature(msg, sigValueHex3, publicKey, {
der: true,
}); // 验签结果
console.log(verifyResult3);
- 4 纯签名 + 生成椭圆曲线点 + sm3杂凑
const sm2 = require("miniprogram-sm-crypto").sm2;
let keypair = sm2.generateKeyPairHex();
const msg = "20201307lcy"
publicKey = keypair.publicKey; // 公钥
privateKey = keypair.privateKey; // 私钥
// 纯签名 + 生成椭圆曲线点 + sm3杂凑
let sigValueHex4 = sm2.doSignature(msg, privateKey, {
hash: true,
}); // 签名
console.log(sigValueHex4);
let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, publicKey, {
hash: true,
}); // 验签结果
console.log(verifyResult4);
3 sm3加密解密
const sm3 = require("miniprogram-sm-crypto").sm3;
const msg = "20201307lcy"
console.log(msg);
let hashData = sm3(msg); // 杂凑
console.log(hashData);
4 sm4 算法实现
const sm4 = require("miniprogram-sm-crypto").sm4;
const msg = '20201307lcy' // 可以为 utf8 串或字节数组
const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 >填充)
console.log(encryptData);
let decryptData = sm4.decrypt(encryptData, key);
console.log(decryptData);
标签:console,log,keypair,privateKey,crypto,sm2,算法,sm,const
From: https://www.cnblogs.com/yu15141310373/p/16950371.html