feat:node-modules
This commit is contained in:
21
node_modules/gm-crypto/LICENSE
generated
vendored
Normal file
21
node_modules/gm-crypto/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 FE Group
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
195
node_modules/gm-crypto/README.md
generated
vendored
Normal file
195
node_modules/gm-crypto/README.md
generated
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
# gm-crypto
|
||||
|
||||
[](https://github.com/byte-fe/gm-crypto/actions/workflows/codecov.yml)
|
||||
[](https://codecov.io/gh/byte-fe/gm-crypto)
|
||||
[](http://commitizen.github.io/cz-cli/)
|
||||
[](https://github.com/prettier/prettier)
|
||||
[](pulls)
|
||||
|
||||

|
||||
|
||||
A pure JavaScript implementation of GM/T series(sm2,sm3,sm4) cryptographic algorithms compatible with Node.js and browsers, with type declaration files support.
|
||||
|
||||
- [GM/T0003-2012《SM2 public key cryptographic algorithm based on elliptic curves》](http://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002386.shtml)
|
||||
- [GM/T0004-2012《SM3 cryptographic hash algorithm》](https://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002389.shtml)
|
||||
- [GM/T0002-2012《SM4 block cipher algorithm》(also aliased as SMS4)](http://www.sca.gov.cn/sca/c100061/201611/1002423/files/330480f731f64e1ea75138211ea0dc27.pdf)
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```bash
|
||||
$ npm install gm-crypto
|
||||
```
|
||||
|
||||
Using yarn:
|
||||
|
||||
```bash
|
||||
$ yarn add gm-crypto
|
||||
```
|
||||
|
||||
### Basic Usage
|
||||
|
||||
#### SM2
|
||||
|
||||
> Public Key Cryptographic Algorithm Based on Elliptic Curves.
|
||||
|
||||
```js
|
||||
const { SM2 } = require('gm-crypto')
|
||||
|
||||
const { publicKey, privateKey } = SM2.generateKeyPair()
|
||||
const originalData = 'SM2 椭圆曲线公钥密码算法'
|
||||
|
||||
const encryptedData = SM2.encrypt(originalData, publicKey, {
|
||||
inputEncoding: 'utf8',
|
||||
outputEncoding: 'base64'
|
||||
})
|
||||
|
||||
const decryptedData = SM2.decrypt(encryptedData, privateKey, {
|
||||
inputEncoding: 'base64',
|
||||
outputEncoding: 'utf8'
|
||||
})
|
||||
```
|
||||
|
||||
#### SM3
|
||||
|
||||
> Cryptographic Hash Algorithm.
|
||||
|
||||
```js
|
||||
const { SM3 } = require('gm-crypto')
|
||||
|
||||
console.log(SM3.digest('abc'))
|
||||
console.log(SM3.digest('YWJj', 'base64'))
|
||||
console.log(SM3.digest('616263', 'hex', 'base64'))
|
||||
```
|
||||
|
||||
#### SM4
|
||||
|
||||
> Block Cipher Algorithm.
|
||||
|
||||
```js
|
||||
const { SM4 } = require('gm-crypto')
|
||||
|
||||
const key = '0123456789abcdeffedcba9876543210' // Any string of 32 hexadecimal digits
|
||||
const originalData = 'SM4 国标对称加密'
|
||||
|
||||
/**
|
||||
* Block cipher modes:
|
||||
* - ECB: electronic codebook
|
||||
* - CBC: cipher block chaining
|
||||
*/
|
||||
|
||||
let encryptedData, decryptedData
|
||||
|
||||
// ECB
|
||||
encryptedData = SM4.encrypt(originalData, key, {
|
||||
inputEncoding: 'utf8',
|
||||
outputEncoding: 'base64'
|
||||
})
|
||||
decryptedData = SM4.decrypt(encryptedData, key, {
|
||||
inputEncoding: 'base64',
|
||||
outputEncoding: 'utf8'
|
||||
})
|
||||
|
||||
// CBC
|
||||
const iv = '0123456789abcdeffedcba9876543210' // Initialization vector(any string of 32 hexadecimal digits)
|
||||
encryptedData = SM4.encrypt(originalData, key, {
|
||||
iv: iv,
|
||||
mode: SM4.constants.CBC,
|
||||
inputEncoding: 'utf8',
|
||||
outputEncoding: 'hex'
|
||||
})
|
||||
decryptedData = SM4.decrypt(encryptedData, key, {
|
||||
iv: iv,
|
||||
mode: SM4.constants.CBC,
|
||||
inputEncoding: 'hex',
|
||||
outputEncoding: 'utf8'
|
||||
})
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
- [SM2](#api)
|
||||
|
||||
- [.generateKeyPair()](#sm2generatekeypair) ⇒ `object`
|
||||
- [.encrypt(data, key[, options]](#sm2encryptdata-key-options) ⇒ `string` | `ArrayBuffer`
|
||||
- [.decrypt(data, key[, options])](#sm2decryptdata-key-options) ⇒ `string` | `ArrayBuffer`
|
||||
|
||||
- [SM3](#api)
|
||||
|
||||
- [.digest(data[, inputEncoding][, outputEncoding])](#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.generateKeyPair()
|
||||
|
||||
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.generateKeyPair()](#sm2generatekeypair) |
|
||||
| options | `object` | | Options |
|
||||
| options.mode | `C1C3C2` \| `C1C2C3` | `C1C3C2` | Concatenation mode |
|
||||
| options.inputEncoding | `string` | `"utf8"` | The encoding of the plain `data` string,if `data` is not a string then `inputEncoding` is ignored. |
|
||||
| options.outputEncoding | `string` | | If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. |
|
||||
| options.pc | `boolean` | `false` | Includes `PC` mark as first byte |
|
||||
|
||||
### SM2.decrypt(data, key[, options])
|
||||
|
||||
Decrypt data.
|
||||
|
||||
| Param | Type | Default | Description |
|
||||
| ---------------------- | --------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| data | `string`\|`ArrayBuffer`\|`Buffer` | | Ciphered data |
|
||||
| key | `string` | | Private key generated by [SM2.generateKeyPair()](#sm2generatekeypair) |
|
||||
| options.mode | `C1C3C2` \| `C1C2C3` | `C1C3C2` | Concatenation mode |
|
||||
| options.inputEncoding | `string` | | The encoding of the plain `data` string,if `data` is not a string then `inputEncoding` is ignored. |
|
||||
| options.outputEncoding | `string` | | If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. |
|
||||
| options.pc | `boolean` | `false` | Includes `PC` mark as first byte |
|
||||
|
||||
### SM3.digest(data, [inputEncoding], [outputEncoding])
|
||||
|
||||
Calculates the digest.
|
||||
|
||||
| Param | Type | Default | Description |
|
||||
| -------------- | --------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| data | `string`\|`ArrayBuffer`\|`Buffer` | | Data message |
|
||||
| inputEncoding | `string` | `"utf8"` | The encoding of the `data` string, if `data` is not a string then `inputEncoding` is ignored. |
|
||||
| outputEncoding | `string` | | If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. |
|
||||
|
||||
### 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.inputEncoding | `string` | `"utf8"` | The encoding of the plain `data` string,if `data` is not a string then `inputEncoding` is ignored. |
|
||||
| options.outputEncoding | `string` | | If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.inputEncoding | `string` | | The encoding of the plain `data` string,if `data` is not a string then `inputEncoding` is ignored. |
|
||||
| options.outputEncoding | `string` | | If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. |
|
||||
1
node_modules/gm-crypto/dist/index.esm.js
generated
vendored
Normal file
1
node_modules/gm-crypto/dist/index.esm.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
1
node_modules/gm-crypto/dist/index.js
generated
vendored
Normal file
1
node_modules/gm-crypto/dist/index.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
1
node_modules/gm-crypto/dist/index.modern.js
generated
vendored
Normal file
1
node_modules/gm-crypto/dist/index.modern.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
1
node_modules/gm-crypto/dist/index.umd.js
generated
vendored
Normal file
1
node_modules/gm-crypto/dist/index.umd.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
64
node_modules/gm-crypto/package.json
generated
vendored
Normal file
64
node_modules/gm-crypto/package.json
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"name": "gm-crypto",
|
||||
"version": "0.1.12",
|
||||
"description": "An implementation of GM/T industry standards",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.esm.js",
|
||||
"esmodule": "dist/index.modern.js",
|
||||
"unpkg": "dist/index.umd.js",
|
||||
"umd:main": "dist/index.umd.js",
|
||||
"types": "types/index.d.ts",
|
||||
"files": [
|
||||
"types",
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"commit": "cz",
|
||||
"clean": "rm -rf dist",
|
||||
"build": "npm run clean && microbundle --entry src/index.js --sourcemap false",
|
||||
"build:coverage": "npm run clean && microbundle --entry src/index.js --sourcemap true",
|
||||
"test": "npm run build:coverage && nyc --reporter=json ava -v",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:byte-fe/gm-crypto.git"
|
||||
},
|
||||
"keywords": [
|
||||
"sm2",
|
||||
"sm3",
|
||||
"sm4",
|
||||
"zuc",
|
||||
"gm",
|
||||
"crypto"
|
||||
],
|
||||
"author": "panjizhi1987@gmail.com",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"buffer": "^5.7.0",
|
||||
"jsbn": "^1.1.0",
|
||||
"to-arraybuffer": "^1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^11.0.0",
|
||||
"@commitlint/config-conventional": "^11.0.0",
|
||||
"ava": "^3.13.0",
|
||||
"commitizen": "^4.2.2",
|
||||
"cz-conventional-changelog": "^3.3.0",
|
||||
"husky": "^4.3.0",
|
||||
"microbundle": "^0.12.4",
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.1.2"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "./node_modules/cz-conventional-changelog"
|
||||
}
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
||||
"pre-push": "npm test"
|
||||
}
|
||||
}
|
||||
}
|
||||
125
node_modules/gm-crypto/types/index.d.ts
generated
vendored
Normal file
125
node_modules/gm-crypto/types/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
declare module "gm-crypto" {
|
||||
export namespace SM2 {
|
||||
export enum constants {
|
||||
C1C2C3,
|
||||
C1C3C2,
|
||||
/** 未压缩 */
|
||||
PC = '04'
|
||||
}
|
||||
/** Generates a new asymmetric key pair */
|
||||
export function generateKeyPair(): {
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
};
|
||||
/**
|
||||
* Encrypt data
|
||||
* @param data Plain message
|
||||
* @param key Public key generated by SM2.generateKeyPair()
|
||||
* @param options encrypt options
|
||||
* */
|
||||
export function encrypt(data: string | ArrayBuffer | Buffer, key: string, options?: EncryptOptions): string;
|
||||
export function encrypt(data: string | ArrayBuffer | Buffer, key: string, options?: Omit<EncryptOptions, "outputEncoding">): ArrayBuffer;
|
||||
/**
|
||||
* Decrypt data
|
||||
* @param data Ciphered data
|
||||
* @param key Public key generated by SM2.generateKeyPair()
|
||||
* @param options
|
||||
* */
|
||||
export function decrypt(data: string | ArrayBuffer | Buffer, key: string, options?: DecryptOptions): string;
|
||||
export function decrypt(data: string | ArrayBuffer | Buffer, key: string, options?: Omit<DecryptOptions, "outputEncoding">): ArrayBuffer;
|
||||
export interface EncryptOptions {
|
||||
/**
|
||||
* Concatenation mode
|
||||
* @default SM2.constants.C1C3C2
|
||||
* */
|
||||
mode?: SM2.constants;
|
||||
/**
|
||||
* The encoding of the plain data string,if `data` is not a string then `inputEncoding` is ignored.
|
||||
* @default "utf8"
|
||||
*/
|
||||
inputEncoding?: BufferEncoding;
|
||||
/** If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. */
|
||||
outputEncoding: BufferEncoding;
|
||||
}
|
||||
export interface DecryptOptions {
|
||||
/**
|
||||
* Concatenation mode
|
||||
* @default SM2.constants.C1C3C2
|
||||
* */
|
||||
mode?: SM2.constants;
|
||||
/**
|
||||
* The encoding of the plain data string,if `data` is not a string then `inputEncoding` is ignored.
|
||||
*/
|
||||
inputEncoding?: BufferEncoding;
|
||||
/** If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. */
|
||||
outputEncoding: BufferEncoding;
|
||||
}
|
||||
}
|
||||
export namespace SM3 {
|
||||
/**
|
||||
* Calculates the digest.
|
||||
* @param data Data message
|
||||
* @param inputEncoding The encoding of the data string, if data is not a string then inputEncoding is ignored.Default is "utf8"
|
||||
* @param outputEncoding If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned.
|
||||
*/
|
||||
export function digest(data: string | ArrayBuffer | Buffer, inputEncoding: string, outputEncoding: BufferEncoding): string;
|
||||
export function digest(data: string | ArrayBuffer | Buffer, inputEncoding?: BufferEncoding): ArrayBuffer;
|
||||
}
|
||||
export namespace SM4 {
|
||||
export enum constants {
|
||||
ECB = 1,
|
||||
CBC
|
||||
}
|
||||
/**
|
||||
* Encrypt data.
|
||||
* @param data Plain message
|
||||
* @param key Cipher key(any string of 32 hexadecimal digits)
|
||||
* @param options Options
|
||||
*/
|
||||
export function encrypt(data: string | ArrayBuffer | Buffer, key: string, options?: EncryptOptions): string;
|
||||
export function encrypt(data: string | ArrayBuffer | Buffer, key: string, options?: Omit<EncryptOptions, "outputEncoding">): ArrayBuffer;
|
||||
export interface EncryptOptions {
|
||||
/**
|
||||
* Block cipher mode
|
||||
* @default SM4.constants.ECB
|
||||
*/
|
||||
mode?: SM4.constants;
|
||||
/**
|
||||
* Initialization vector(any string of 32 hexadecimal digits)
|
||||
*/
|
||||
iv?: string;
|
||||
/**
|
||||
* The encoding of the plain data string,if data is not a string then inputEncoding is ignored.
|
||||
* @default "utf8"
|
||||
*/
|
||||
inputEncoding?: BufferEncoding;
|
||||
/** If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. */
|
||||
outputEncoding: BufferEncoding;
|
||||
}
|
||||
/**
|
||||
* Decrypt data.
|
||||
* @param data Ciphered data
|
||||
* @param key Cipher key(any string of 32 hexadecimal digits)
|
||||
* @param options Options
|
||||
*/
|
||||
export function decrypt(data: string | ArrayBuffer | Buffer, key: string, options?: DecryptOptions): string;
|
||||
export function decrypt(data: string | ArrayBuffer | Buffer, key: string, options?: Omit<DecryptOptions, "outputEncoding">): ArrayBuffer;
|
||||
export interface DecryptOptions {
|
||||
/**
|
||||
* Block cipher mode
|
||||
* @default "ECB"
|
||||
*/
|
||||
mode?: SM4.constants;
|
||||
/**
|
||||
* Initialization vector(any string of 32 hexadecimal digits)
|
||||
*/
|
||||
iv?: string;
|
||||
/**
|
||||
* The encoding of the plain data string,if data is not a string then inputEncoding is ignored.
|
||||
*/
|
||||
inputEncoding?: BufferEncoding;
|
||||
/** If `outputEncoding` is provided, a string will be returned, otherwise a [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) is returned. */
|
||||
outputEncoding: BufferEncoding;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user