evit-gm-crypt
v1.0.1
Published
Chinese GM crypt algorithm in JavaScript
Downloads
12
Readme
evit-gm-crypt
基于javascript
和TypedArray
的国密加密算法实现。
Implement of Chinese encrypt algorithm in JavaScript and TypedArray.
为了兼容浏览器环境和node.je环境,这里我们使用了TextEncoder和Base64.js。
Here we use TextEncoder and 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
和openssl_decrypt
保持一致性,当密码长度小于16时,静默填充NUL;当密码长度大于16时,静默截断。 - 要使用openssl国密算法,openssl库需 >= 1.1.1
Roadmap
- [x] SM4
- [x] SM3
- [ ] SM2
Documentation
Install
npm install evit-gm-crypt
SM4
Init
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
let plaintext = '中国国密加解密算法'
let ciphertext = sm4.encrypt(plaintext)
// ciphertext's result is 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
Decrypt
let ciphertext = 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
let plaintext = sm4.decrypt(ciphertext)
// plaintext's result is '中国国密加解密算法'