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

@muguilin/gm-crypto

v1.0.4

Published

基于 gm-crypto 密码行业标准化委员会(SM4)和 big-integer 大整数处理 的前后端API数据传输加解密处理模块!

Downloads

34

Readme

@muguilin/gm-crypto

MIT License

基于 gm-crypto 密码行业标准化委员会(SM4)和 big-integer 大整数处理 的前后端API数据传输 加解密处理模块!

🏡下载安装:

# 使用npm命令下载安装
$ npm i @muguilin/gm-crypto

# 使用yarn命令下载安装
$ yarn add @muguilin/gm-crypto

🚀使用实例:

  • 基于SM4算法简单的数据加密与解密

    import { useMS4 } from '@muguilin/gm-crypto';
      
    // 加解密的密钥(32位十六进制数字的任意字符串)【注:前后端要一致,一般由后端提供】!
    const key = '0123456789abcdeffedcba9876543210';
      
    // 实例化SM4
    const { encrypt, decrypt } = useMS4(key);
      
    // 加密
    const enText = encrypt("要加密的数据");
    console.log("加密后的数据:", enText);
      
    // 解密
    const deText = decrypt(enText); // enText要解密的数据
    console.log("解密后的数据:", deText);
      
  • 基于DH(g、p、server_number) 动态生成key的数据加密与解密

    DH-SM4

    import { SMT } from '@muguilin/gm-crypto';
      
    // g(底数)用于生加成加解密参数key的计算,一般由后端提供(后端[服务端]如 PHP 使用dh+sm4方式进行加解密的话,还需要在PHP环境中配置GMP扩展)!
    const g = "1038272227580712591710520727608303818673911356163915961405717611171367511230688128700450068148956934708940258418872350885653640473176181192540522651506533255838991048827665976480700225624160024731079722170677370103248727024197708983088571412106375460189255864958701180711121568396017340770855322080575567550949325637777197678655333725734029203250985055060177328699461276182647203930121935504198892010036307848034646028067498289207605424628656754571390515444839264715026166394370979911525156696104494751352224616674298363558921419967181358293514909040869097373019600391108502923597645594040124139849905333261652846776468847455483680198441111660808929037682589885319184876688894899925187095339339054250753607810487010089536185759207920565152930466661764879677531227388969330095782856332361506071119133144295802630500264321699986300934321940810255469010859954588899621259906630420193397923160825361087745167481790971187813341612";
      
    // p(模数)用于生加成加解密参数key的计算,一般由后端提供!
    const p = "5809605995369958062791915965639201402176612226902900533702900882779736177890990861472094774477339581147373410185646378328043729800750470098210924487866935059164371588168047540943981644516632755067501626434556398193186628990071248660819361205119793693985433297036118232914410171876807536457391277857011849897410207519105333355801121109356897459426271845471397952675959440793493071628394122780510124618488232602464649876850458861245784240929258426287699705312584509625419513463605155428017165714465363094021609290561084025893662561222573202082865797821865270991145082200656978177192827024538990239969175546190770645685893438011714430426409338676314743571154537142031573004276428701433036381801705308659830751190352946025482059931306571004727362479688415574702596946457770284148435989129632853918392117997472632693078113129886487399347796982772784615865232621289656944284216824611318709764535152507354116344703769998514148343807";
      
    // server_number用于生加成加解密参数key的计算,一般由后端提供!
    const server_number = "533002428294336616873774952106421502149652599458945478609579456633825175435591572213461924245684506823740277481160893127743825132048523201730626138802577877838910882706326333379360061600687327466758774476542895981134200289204422404617548945391801177150646411253838195041563877632692881701550741428542152830253153473327351279494487904039611209303291596783449362822656199480718368618032509368200940760413110348306689538262319224500875234864333495532671661427579382966483603022011800839509503834308172324313333943072078504221645391025883372716069801252155477584045884503119483337385532431165994014594769775689105920235973259687204048804435021223022934701279317419628464758137141877321487918330568586022286653201801159779446145360351977841762477378726595709936168629569734722619448959387741990885057768232228982299736696596473381071538379147381077944582515375238879189105298215519272231254953533071137209214928296924637340551477";
            
    // 加密
    const { key = '', result = '', client_number = '' } = SMT.encrypt("要加密的文本数据", g, p, server_number);
    console.log("加密后的数据:", result);
      
    // 解密
    const { result = '' } = SMT.decrypt("要解密的数据", key); // result 要解密的数据
    console.log("解密后的数据:", result);
      
  • 基于DH(g、p、server_number) 动态生成key的数据解密

    import { SMT } from '@muguilin/gm-crypto';
      
    // g(底数)用于生加成加解密参数key的计算,一般由后端提供(后端[服务端]如 PHP 使用dh+sm4方式进行加解密的话,还需要在PHP环境中配置GMP扩展)!
    const g = "1038272227580712591710520727608303818673911356163915961405717611171367511230688128700450068148956934708940258418872350885653640473176181192540522651506533255838991048827665976480700225624160024731079722170677370103248727024197708983088571412106375460189255864958701180711121568396017340770855322080575567550949325637777197678655333725734029203250985055060177328699461276182647203930121935504198892010036307848034646028067498289207605424628656754571390515444839264715026166394370979911525156696104494751352224616674298363558921419967181358293514909040869097373019600391108502923597645594040124139849905333261652846776468847455483680198441111660808929037682589885319184876688894899925187095339339054250753607810487010089536185759207920565152930466661764879677531227388969330095782856332361506071119133144295802630500264321699986300934321940810255469010859954588899621259906630420193397923160825361087745167481790971187813341612";
      
    // p(模数)用于生加成加解密参数key的计算,一般由后端提供!
    const p = "5809605995369958062791915965639201402176612226902900533702900882779736177890990861472094774477339581147373410185646378328043729800750470098210924487866935059164371588168047540943981644516632755067501626434556398193186628990071248660819361205119793693985433297036118232914410171876807536457391277857011849897410207519105333355801121109356897459426271845471397952675959440793493071628394122780510124618488232602464649876850458861245784240929258426287699705312584509625419513463605155428017165714465363094021609290561084025893662561222573202082865797821865270991145082200656978177192827024538990239969175546190770645685893438011714430426409338676314743571154537142031573004276428701433036381801705308659830751190352946025482059931306571004727362479688415574702596946457770284148435989129632853918392117997472632693078113129886487399347796982772784615865232621289656944284216824611318709764535152507354116344703769998514148343807";
      
    // server_number用于生加成加解密参数key的计算,一般由后端提供!
    const server_number = "533002428294336616873774952106421502149652599458945478609579456633825175435591572213461924245684506823740277481160893127743825132048523201730626138802577877838910882706326333379360061600687327466758774476542895981134200289204422404617548945391801177150646411253838195041563877632692881701550741428542152830253153473327351279494487904039611209303291596783449362822656199480718368618032509368200940760413110348306689538262319224500875234864333495532671661427579382966483603022011800839509503834308172324313333943072078504221645391025883372716069801252155477584045884503119483337385532431165994014594769775689105920235973259687204048804435021223022934701279317419628464758137141877321487918330568586022286653201801159779446145360351977841762477378726595709936168629569734722619448959387741990885057768232228982299736696596473381071538379147381077944582515375238879189105298215519272231254953533071137209214928296924637340551477";
      
    // 加密
    const { key = '', bigKey = '', client_number = '' } = SMT.getDH(g, p, server_number);
    console.log("加密后的数据:", result);
      
    // 解密
    const { result = '' } = SMT.decrypt("要解密的数据", key);
    console.log("解密后的数据:", result);
  • UTF8字节 - 字符串 相互转换

    import { Utf8 } from '@muguilin/gm-crypto';
      
    const utf8 = Utf8.encode("要转换的文本数据");
    console.log("转换后的utf8字节数据:", utf8);
      
    const str = Utf8.decode(utf8);
    console.log("utf8字节数据 转 字符串:", str);
  • 十六进制 - 字符串 相互转换

    import { Hex } from '@muguilin/gm-crypto';
      
    const hex = Hex.encode("要转换的文本数据");
    console.log("转换后的16进制数据:", hex);
      
    const str = Hex.decode(hex);
    console.log("16进制 转 字符串:", str);
  • Base64 - 字符串 相互转换

    import { Base64 } from '@muguilin/gm-crypto';
      
    const b64 = Base64.encode("要转换的文本数据");
    console.log("转换后的Base64数据:", b64);
      
    const str = Base64.decode(b64);
    console.log("Base64 转 字符串:", str);
  • 获取Web浏览器地址栏URL中?后面的参数

    import { getQuery } from '@muguilin/gm-crypto';
      
    const uid = getQuery("user_id", globalThis.location.search); // globalThis.location.search 非必传参数
    console.log("从Web浏览器URL中获取到的用户ID:", uid);

📢关于big-integer模块:保留了bigInt的原有用法:

BigInteger.js 是 Javascript 的任意长度整数库,它允许对无限大小的整数,去进行算术运算,尽管在有内存和时间限制的情况下。

Npm地址:big-integer

import { bigInt } from '@muguilin/gm-crypto';

let zero = bigInt();

let ninetyThree = bigInt(93);

let largeNumber = bigInt("75643564363473453456342378564387956906736546456235345");

let googol = bigInt("1e100");

let bigNumber = bigInt(largeNumber);

let maximumByte = bigInt("FF", 16);

let fiftyFiveGoogol = bigInt("<55>0", googol);

📢关于gm-crypto模块:保留了SM2、SM3、SM4的原有用法:

密码行业标准化委员会,GM/T系列(sm2、sm3、sm4)加密算法的纯JavaScript实现,与Node.js和浏览器兼容,支持类型声明文件。

Npm地址:gm-crypto

  • SM2 基于椭圆曲线的公钥密码算法。
import { SM2, SM3, SM4 } from '@muguilin/gm-crypto';

const { publicKey, privateKey } = SM2.generateKeyPair()
const originalData = 'SM2 椭圆曲线公钥密码算法'

const encryptedData = SM2.encrypt(originalData, publicKey, {
  inputEncoding: 'utf8',
  outputEncoding: 'base64'
})

const decryptedData = SM2.decrypt(encryptedData, privateKey, {
  inputEncoding: 'base64',
  outputEncoding: 'utf8'
})
  • SM3 加密哈希算法。
import { SM2, SM3, SM4 } from '@muguilin/gm-crypto';

console.log(SM3.digest('abc'))
console.log(SM3.digest('YWJj', 'base64'))
console.log(SM3.digest('616263', 'hex', 'base64'))
  • SM4 分组密码算法。
import { SM2, SM3, SM4 } from '@muguilin/gm-crypto';

const key = '0123456789abcdeffedcba9876543210' // Any string of 32 hexadecimal digits
const originalData = 'SM4 国标对称加密'

/**
 * Block cipher modes:
 * - ECB: electronic codebook
 * - CBC: cipher block chaining
 */

let encryptedData, decryptedData

// ECB
encryptedData = SM4.encrypt(originalData, key, {
  inputEncoding: 'utf8',
  outputEncoding: 'base64'
})
decryptedData = SM4.decrypt(encryptedData, key, {
  inputEncoding: 'base64',
  outputEncoding: 'utf8'
})

// CBC
const iv = '0123456789abcdeffedcba9876543210' // Initialization vector(any string of 32 hexadecimal digits)
encryptedData = SM4.encrypt(originalData, key, {
  iv: iv,
  mode: SM4.constants.CBC,
  inputEncoding: 'utf8',
  outputEncoding: 'hex'
})
decryptedData = SM4.decrypt(encryptedData, key, {
  iv: iv,
  mode: SM4.constants.CBC,
  inputEncoding: 'hex',
  outputEncoding: 'utf8'
})