@qhweb/gm-crypto
v1.0.9
Published
Using npm:
Readme
gm-crypto
Quick Start
Install
Using npm:
$ npm install @qhweb/gm-cryptoUsing yarn:
$ yarn add @qhweb/gm-cryptoBasic 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
- .generateKeyPairHex() ⇒
object - .encrypt(data, key[, options] ⇒
string|ArrayBuffer - .decrypt(data, key[, options]) ⇒
string|ArrayBuffer
- .generateKeyPairHex() ⇒
- (data[, options]) ⇒
string|ArrayBuffer
- (data[, options]) ⇒
- .encrypt(data, key[, options]) ⇒
string|ArrayBuffer - .decrypt(data, key[, options]) ⇒
string|ArrayBuffer
- .encrypt(data, key[, options]) ⇒
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. |
