npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yc-sm-crypto

v1.0.4

Published

SM2加密和解密 以及验签,SM4加密 可以结合Hutools使用

Downloads

45

Readme

yc-sm-crypto

国密算法sm2、sm3和sm4的js实现。

安装

npm install --save yc-sm-crypto

sm2 配合hutools

const Http = require("http");
const {arrayToUtf8} = require("../sm2/utils");
const sm2 = require('../index').sm2

const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3
const msgString = '测试国密SM2'

const myPublicKey = '0416aebc0d9a00f5d82b9aa764425437caf9046ef149475c4add632c9372ef7fbf338e1049f00c71b5e59918e46da09b2a3c6a4deea64316187267d5b533ee2ea9';
const myPrivateKey = '00c41eb1e2f917d8e6e2845b456fd8a15568f1d4a3016b297cfc786e164225e29c';
const myuserid = '78CFCC02D41F489FA3A0D04A81C67450'; //已经转成16进制 userid
const yourPublicKey = '045bd655aa66b20bbb9875c6196c6c552e39a1d17690672f0789064097dd12b5c67f12b62ae75a85abb51eba669bc685165e4230b204a1fb12e9b0a145f04b0b3d';

//构建原始报文
let reqData = {
    "xmsfzmd518": '0d7bfc5a0a32e1ab66bad5832cd8a2b8',
    "xm": "鬼剑士",
    "sfz": "110229194902154016",
};
let reqJson = JSON.stringify(reqData);
let reqDataEncrypt = sm2.Encrypt(reqJson, yourPublicKey, 1);
let reqSign = sm2.Sign(reqDataEncrypt, myPrivateKey, {
    userId: myuserid,
    der: true,// asn.1 der 编码
    hash: true, //sm3杂凑
    userIdType: "Hex",//指定userid的值已经是16进制数据 如果userid 为  xuzhqiang0001 则不需要指定useridType。
    dataType: "Hex",//指定加密的字符串的值已经是16进制 如果是明文则不需要指定dataType。
});
let postdata = {
    data: reqDataEncrypt,
    sign: reqSign,
    code: myuserid
}
let data = JSON.stringify(postdata);
let responseData={};
//请求java hutool的sm2加密解密
const req = Http.request({
    host: 'localhost',
    port: 30030,
    path: '/auth/qrcode',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    },
}, (res) => {
    let resData="";
    res.on('data',response => {

       resData+=response.toString('utf8');


    })
    res.on('end',()=>{

        responseData=JSON.parse(resData);
       //验证返回的签名
        let verifySign = sm2.VerifySign(responseData.data.data, responseData.data.sign, yourPublicKey, {
            userId: myuserid,
            der: true,// asn.1 der 编码
            hash: true, //sm3杂凑
            dataType:"Hex",
            userIdType:"Hex" //指定userid的值已经是16进制数据 如果userid 为  xuzhqiang0001 则不需要指定useridType。
        })
        console.log(verifySign)
    })
});
req.write(data);
req.end();


ApiFox扩展函数