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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@qhweb/gm-crypto

v1.0.9

Published

Using npm:

Readme

gm-crypto

Quick Start

Install

Using npm:

$ npm install @qhweb/gm-crypto

Using yarn:

$ yarn add @qhweb/gm-crypto

Basic Usage

SM2

Public Key Cryptographic Algorithm Based on Elliptic Curves.

import {SM2} from '@qhweb/gm-crypto'
//生成公钥和私钥
const { publicKey, privateKey } = SM2.generateKeyPairHexHex()
//加密模式,1 - C1C3C2,0 - C1C2C3,默认为1
const cipherMode = 1
//加密数据
const originalData = 'SM2 椭圆曲线公钥密码算法'
//加密结果
const encryptedData = SM2.encrypt(originalData, publicKey, cipherMode)
//解密结果
const decryptedData = SM2.decrypt(encryptedData, privateKey, cipherMode)

SM3

Cryptographic Hash Algorithm.

import {SM3} from '@qhweb/gm-crypto'

//普通加密
console.log(SM3('abc'))
//密钥填充
console.log(SM3('abc', {key:'1234567890abcdef'}))

SM4

Block Cipher Algorithm.

import {SM4} from '@qhweb/gm-crypto'
//密钥
const key = '0123456789abcdef' // Any string of 16 hexadecimal digits
//待加密数据
const originalData = 'SM4 国标对称加密'

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

let encryptedData, decryptedData

// ECB
encryptedData = SM4.encrypt(originalData, key)
decryptedData = SM4.decrypt(encryptedData, key)

// CBC
const iv = 'fedcba9876543210' // Initialization vector(any string of 16 hexadecimal digits)
encryptedData = SM4.encrypt(originalData, key, {
  iv: iv,
  mode: 'cbc',
})
decryptedData = SM4.decrypt(encryptedData, key, {
  iv: iv,
  mode: 'cbc',
})

API

SM2.generateKeyPairHex()

Generates a new asymmetric key pair.

SM2.encrypt(data, key[, options])

Encrypt data.

| Param | Type | Default | Description| | ---------------------- | --------------------------------- | -------- | ----------------------------------------------------------------------- | | data | string|ArrayBuffer|Buffer | | Plain message| | key | string | | Public key generated by SM2.generateKeyPairHex() | | options | object | | Options | | options.mode | C1C3C2 | C1C2C3 | C1C3C2 | Concatenation mode |

SM2.decrypt(data, key[, options])

Decrypt data.

| Param | Type | Default | Description | | ---------------------- | --------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | data | string|ArrayBuffer|Buffer | | Ciphered data| | key | string | | Private key generated by SM2.generateKeyPairHex() | | options.mode | C1C3C2 | C1C2C3 | C1C3C2 | Concatenation mode|

SM3(data, [options])

Calculates the digest.

| Param | Type | Default | Description | | -------------- | --------------------------------- | -------- | ----------- | | data | string|ArrayBuffer|Buffer | | Data message| | options | object | "utf8" | {key:'0123456789abcdef'} |

SM4.encrypt(data, key[, options])

Encrypt data.

| Param | Type | Default | Description | | ---------------------- | --------------------------------- | -------- | --------------------------------------------------------------------------------- | | data | string|ArrayBuffer|Buffer | | Plain message | | key | string | | Cipher key(any string of 32 hexadecimal digits)| | options | object | {} | Options | | options.mode | ECB | CBC | ECB | Block cipher mode | | options.iv | string | | Initialization vector(any string of 32 hexadecimal digits)|
| options.output | string | array | | If output is provided, a string will be returned, otherwise a [ArrayBuffer] is returned. |

SM4.decrypt(data, key[, options])

Decrypt data.

| Param | Type | Default | Description| | ---------------------- | --------------------------------- | ------- | ----------------------------------------------------------------------------- | | data | string|ArrayBuffer|Buffer | | Ciphered data | | key | string | | Cipher key(any string of 32 hexadecimal digits) | | options | object | | Options | | options.mode | ECB | CBC | ECB | Block cipher mode | | options.iv | string | | Initialization vector(any string of 32 hexadecimal digits) | | options.output | string | | If output is provided, a string will be returned, otherwise a [ArrayBuffer] is returned. |