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

request-crypto-sm2

v1.1.14

Published

request crypto by sm2

Downloads

17

Readme

安装

npm install request-crypto-sm2 -S

使用

引入配置

// 引入配置
import { publicKey, privateKey, strKey, cipherMode } from '@/config'
import RequestCryptoSM2 from 'request-crypto-sm2'

初始化

// 初始化
const rcs2 = new RequestCryptoSM2(
  publicKey,
  privateKey,
  strKey,
  cipherMode,
  false // 调试,是否打印参数 默认fasle
)

加解密

// 加密
// 入参: obj对象 eg: { name: 'foo', age: 12 }
// 出参: params ajax的参数; sign 签名;
const { params, sign } = rcs2.encryptResult(data)

// 解密
// 入参: string
// 出参: string类型的json; 需要和后端约定返回数据为json类型的加密字符串;
const result = rcs2.decryptResult(data)
return result && JSON.parse(result) // 需要序列化

加密规则

params 对提交的参数进行SM2加密

sm2.doEncrypt(JSON.stringify({
  data // ajax提交的参数
  timestemp // 毫秒时间戳
  notice // 16位随机字符串 a-z0-9
}), privateKey, cipherMode)

sign

1、自然排序KEY, 按顺序取出value,用(-)分割。 会过滤掉null, '', undefined。

const data = {
  car: '鲁A12345',
  dog: true,
  cat: false,
  isNull: null,
  isEmpty: '',
  isUndefined: undefined,
  person: {
    name: '张三',
    age: 32,
    child: [{
      name: '张武',
      age: 12
    }, {
      name: '张燕',
      age: 6
    }]
  }
}
const a = _sortJsonByKeys(data)
console.log(a) // 鲁A12345-false-true-{"name":"张三","age":32,"child":[{"name":"张武","age":12},{"name":"张燕","age":6}]}

2、在末尾拼接上自定义的strKey用横线隔开

const strKey = '-auth'
// 即上一步的字符串变成
// 鲁A12345-false-true-{"name":"张三","age":32,"child":[{"name":"张武","age":12},{"name":"张燕","age":6}]}-auth

3、对此字符串进行MD5加密,即得到sign签名