npm package: @qhweb/gm-crypto
Description: Using npm:
Latest version: 1.0.9

# gm-crypto

## Quick Start

### Install

Using npm:

```bash
$ npm install @qhweb/gm-crypto
```

Using yarn:

```bash
$ yarn add @qhweb/gm-crypto
```

### Basic Usage

#### SM2

> Public Key Cryptographic Algorithm Based on Elliptic Curves.

```js
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.

```js
import {SM3} from '@qhweb/gm-crypto'

//普通加密
console.log(SM3('abc'))
//密钥填充
console.log(SM3('abc', {key:'1234567890abcdef'}))
```

#### SM4

> Block Cipher Algorithm.

```js
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](#api)

  - [.generateKeyPairHex()](#sm2generateKeyPairHex) ⇒ `object`
  - [.encrypt(data, key[, options]](#sm2encryptdata-key-options) ⇒ `string` | `ArrayBuffer`
  - [.decrypt(data, key[, options])](#sm2decryptdata-key-options) ⇒ `string` | `ArrayBuffer`

- [SM3](#api)

  - [(data[, options])](#sm3digestdata-inputencoding-outputencoding) ⇒ `string` | `ArrayBuffer`

- [SM4](#api)
  - [.encrypt(data, key[, options])](#sm4encryptdata-key-options) ⇒ `string` | `ArrayBuffer`
  - [.decrypt(data, key[, options])](#sm4decryptdata-key-options) ⇒ `string` | `ArrayBuffer`

### 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()](#sm2generateKeyPairHex)                                                                                                                             |
| 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()](#sm2generateKeyPairHex)                                                                                                                            |
| 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. |