# gm-sm4

> Chinese GM crypt algorithm in JavaScript

Latest version **0.0.3** (published 2022-04-01) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install gm-sm4
pnpm add gm-sm4
yarn add gm-sm4
bun add gm-sm4
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2022-04-01 |
| First published | 2022-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Endy |
| Maintainers | endy_c |
| Keywords | 国密, ChineseGM, SM2, SM3, SM4 |

## Links

- npm: https://www.npmjs.com/package/gm-sm4
- Repository: https://github.com/Endy-c/gm-crypt
- Homepage: https://github.com/Endy-c/gm-crypt#readme
- Issues: https://github.com/Endy-c/gm-crypt/issues
- npm.io page: https://npm.io/package/gm-sm4

## Dependencies (1)

- [base64-js](https://npm.io/package/base64-js.md) ^1.3.0

## Recent versions

- 0.0.3 (latest) — 2022-04-01
- 0.0.2 — 2022-04-01
- 0.0.1 — 2022-04-01

## README

# gm-sm4-crypto

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 

基于`javascript`和[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)的国密加密算法实现。

Implement of Chinese encrypt algorithm in JavaScript and [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray).

为了兼容浏览器环境和node.je环境，这里我们使用了[TextEncoder](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)和[Base64.js](https://github.com/beatgammit/base64-js)。

Here we use [TextEncoder](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) and [Base64.js](https://github.com/beatgammit/base64-js) for both browser and node.js environment.

由于使用了`TextEncoder`，所以暂时不支持`Edge`。Node.js版本最低为8，建议使用最新的LTS版本。

Because of using `TextEncoder`， this code cannot run in `Edge` browser. `Node.js`‘s version should at least be 8, and the newest LTS version is recommended.

新增特性
 - 与[`openssl_encrypt`](https://www.php.net/manual/en/function.openssl-encrypt.php)和[`openssl_decrypt`](https://www.php.net/manual/en/function.openssl-decrypt.php)保持一致性，当密码长度小于16时，静默填充NUL；当密码长度大于16时，静默截断。
 - 要使用openssl国密算法，openssl库需 >= 1.1.1

## Roadmap

- [x] SM4
- [ ] SM3
- [ ] SM2

## Documentation

### Install

```
npm install gm-sm4
```

### SM4

#### Init

```js
const SM4 = require('gm-sm4').sm4

let sm4Config = {
  // encrypt/decypt main key; cannot be omitted
  key: 'JeF8U9wHFOMfs2Y8',

  // optional; can be 'cbc' or 'ecb'
  mode: 'cbc', // default

  // optional; when use cbc mode, it's necessary
  iv: 'UISwD9fW6cFh9SNS', // default is null

  // optional: this is the cipher data's type; Can be 'base64' or 'text'
  cipherType: 'base64' // default is base64
}

let sm4 = new SM4(sm4Config)
```

#### Encrypt

```js
let plaintext = '中国国密加解密算法'
let ciphertext = sm4.encrypt(plaintext)
// ciphertext's result is 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
```

#### Decrypt

```js
let ciphertext = 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
let plaintext = sm4.decrypt(ciphertext)
// plaintext's result is '中国国密加解密算法'
```


## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/gm-sm4 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
