# @mega-apps/node-ex

> Mega-Apps node addon.

Latest version **1.0.0-alpha.15** (published 2022-07-20) · 0 weekly downloads

## Install

```sh
npm install @mega-apps/node-ex
pnpm add @mega-apps/node-ex
yarn add @mega-apps/node-ex
bun add @mega-apps/node-ex
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.15 |
| Published | 2022-07-20 |
| First published | 2021-11-25 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 4 |
| Unpacked size | 3.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | zhifeng.sun.auo.com |
| Keywords | Node, Rust, Mega, Mega-apps, napi-rs, node-addon, node-addon-api |

## Links

- npm: https://www.npmjs.com/package/@mega-apps/node-ex
- npm.io page: https://npm.io/package/@mega-apps/node-ex

## Dependencies (4)

- [@node-rs/helper](https://npm.io/package/@node-rs/helper.md) ^1.3.3
- [@mega-apps/node-ex-darwin-x64](https://npm.io/package/@mega-apps/node-ex-darwin-x64.md) ^1.0.0-alpha.1
- [@mega-apps/node-ex-linux-x64-gnu](https://npm.io/package/@mega-apps/node-ex-linux-x64-gnu.md) ^1.0.0-alpha.1
- [@mega-apps/node-ex-win32-x64-msvc](https://npm.io/package/@mega-apps/node-ex-win32-x64-msvc.md) ^1.0.0-alpha.1

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.0.0-alpha.15 (latest) — 2022-07-20
- 1.0.0-alpha.14 — 2022-06-20
- 1.0.0-alpha.13 — 2022-05-25
- 1.0.0-alpha.12 — 2022-05-25
- 1.0.0-alpha.11 — 2022-01-27
- 1.0.0-alpha.10 — 2022-01-21
- 1.0.0-alpha.9 — 2022-01-19
- 1.0.0-alpha.8 — 2022-01-12
- 1.0.0-alpha.7 — 2022-01-12
- 1.0.0-alpha.6 — 2022-01-11
- 1.0.0-alpha.5 — 2021-11-29
- 1.0.0-alpha.4 — 2021-11-29
- 1.0.0-alpha.3 — 2021-11-26
- 1.0.0-alpha.2 — 2021-11-26
- 1.0.0-alpha.1 — 2021-11-25

## README

# @mega-apps/node-ex

Mega-Apps Node原生扩展模块 （采用 Rust 语言编写扩展）

## 1. API

### 1.1. Hash 计算

#### 1.1.1. 获得代码的Hash值

##### 1.1.1.1. 接口函数说明

```js
/**
  * @param {Buffer} code 代码
  * @param {string} digest Hash 算法, 支持 md5, sha1, sha256, sha512，更多请参照：`openssl dgst -list`
  * 常用的 Hash 算法有：
    blake2b512                blake2s256                md4
    md5                       md5-sha1                  mdc2
    ripemd                    ripemd160                 rmd160
    sha1                      sha224                    sha256
    sha3-224                  sha3-256                  sha3-384
    sha3-512                  sha384                    sha512
    sha512-224                sha512-256                shake128
    shake256                  sm3                       ssl3-md5
    ssl3-sha1                 whirlpool
  * @return {Buffer} Hash值
  */
function get_code_hash(code, digest){}
```

##### 1.1.1.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const hash = native.get_code_hash(Buffer.from('Hello, world!'),'sha256');
console.log(hash.toString()); // 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
```

#### 1.1.2. 获得文件的Hash值

##### 1.1.2.1. 接口函数说明

```js
/**
  * @param {string} filePath 文件路径
  * @param {string} digest Hash 算法, 支持 md5, sha1, sha256, sha512，更多请参照：`openssl dgst -list`
  * 常用的 Hash 算法有：
    blake2b512                blake2s256                md4
    md5                       md5-sha1                  mdc2
    ripemd                    ripemd160                 rmd160
    sha1                      sha224                    sha256
    sha3-224                  sha3-256                  sha3-384
    sha3-512                  sha384                    sha512
    sha512-224                sha512-256                shake128
    shake256                  sm3                       ssl3-md5
    ssl3-sha1                 whirlpool
  * @return {Buffer} Hash值
  */
function get_code_file_hash(filePath, digest){}
```

##### 1.1.2.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const hash = native.get_code_file_hash('/path/to/file','sha256');
```

### 1.2. 生成密钥

#### 1.2.1. 生成非对称密钥对（RSA）

##### 1.2.1.1. 接口函数说明

```js
/**
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * - @param {string} options.pk_name 公钥文件路径，默认 rsa_public.pem
  * - @param {string} options.sk_name 私钥文件路径，默认 rsa_private.pem
  * @return {Object} {pk:Buffer, sk:Buffer}密钥对数据
  *  - @param {Buffer} pk 公钥
  *  - @param {Buffer} sk 私钥
  */
function generate_rsa_key_pair(optioins = {
  passphrase: '',
  pk_name: './key_public.pem',
  sk_name: './key_private.pem'
}){}
```

##### 1.2.1.2. 代码示例

```js

// 默认生成密钥对，并保存在当前目录下，密码为空，公钥文件名为 key_public.pem，私钥文件名为 key_private.pem
const native = require('@mega-apps/node-ex');
const {pk, sk} = native.generate_rsa_key_pair();

```

```js

// 生成密钥对, 并保存到文件，密码为 123456，公钥文件名为 key_public.pem，私钥文件名为 key_private.pem
const native = require('@mega-apps/node-ex');
const {pk, sk} = native.generate_rsa_key_pair({
  passphrase: '123456',
  pk_name: './key_public.pem',
  sk_name: './key_private.pem'
});

```

#### 1.2.2. 生成对称密钥（AES）

##### 1.2.2.1. 接口函数说明

```js
/**
  * @param {Object} options 选项
  * @param {string} options.file_name 密钥输出的文件的路径，默认为空字符串，不输出内容到文件
  * @return {Buffer} 密钥Buffer 数据
  */
function generate_aes_key(optioins = {
  file_name: '/path/to/file'
}){}
```

##### 1.2.2.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const key = native.generate_aes_key();
```

### 1.3. 使用非对称密钥加密/解密

#### 1.3.1. 接口函数说明

```js
/**
  * @param {Buffer} data 要加密的数据
  * @param {string/Buffer} key 加密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 加密后的数据
  */
function encrypt_key_with_buffer(data, key_data, options= {}){}

/**
  * @param {string} file 要加密的文件的路径
  * @param {string/Buffer} key 加密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
  * @param {string} output  输出加密后的文件的路径，为空字符串时，不输出到文件
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 加密后的数据
  */
function encrypt_key_with_file(file, key, output, options= {}){}

/**
  * @param {Buffer} data 要解密的数据
  * @param {string/Buffer} key 解密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 解密后的数据
  */
function decrypt_key_with_buffer(data, key_data, options= {}){}

/**
  * @param {string} file 要解密的文件的路径
  * @param {string/Buffer} key  解密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
  * @param {string} output  输出解密后的文件的路径，为空字符串时，不输出到文件
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 解密后的数据
  */
function decrypt_key_with_file(file, key, output, options= {}){}
```

#### 1.3.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const plainText = `sunzhifeng <ian.sun@auodigitech.com>`;

const pub_key = join(__dirname,'../../','./certs/rsa_public.pem');
const pri_key = join(__dirname,'../../','./certs/rsa_private.pem');

// 加密
const cipher = native.encrypt_key_with_buffer(Buffer.from(plainText),pub_key);
native.encrypt_key_with_file('/path/a.txt',pub_key, '/path/a.txt.enc');

// 解密
native.decrypt_key_with_buffer(cipher, pri_key);
native.decrypt_key_with_file('/path/a.txt.enc', pri_key, '/path/a.txt.dec');

```

### 1.4. 使用对称密钥加密/解密

#### 1.4.1. 接口函数说明

```js
/**
  * 对称加密
  * @param {Buffer} data 要加密的数据
  * @param {string/Buffer} key 加密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
 * * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 加密后的数据
  */
function code_encrypt_with_buffer(data, key_data, options= {}){}

/**
  * 对称解密
  * @param {Buffer} data 要解密的数据
  * @param {string/Buffer} key 解密密钥; string 类型为文件的路径; Buffer 类型为密钥buf数据
  * @param {Object} options 选项
  * - @param {string} options.passphrase 密码，默认空
  * @return {Buffer} 加密后的数据
  */
function code_decrypt_with_buffer(data, key_data, options= {}){}
```

##### 1.4.1.1. 代码示例

```js
const native = require('@mega-apps/node-ex');

// 原文
const plain_buf = fs.readFileSync(join(rootDir,'./tests/fixtures/app.js'));
// 对称密钥
const key_buf = Buffer.from("DKA4EPOBOHnj8l0sS9hAbwGqZfH8VU9i^^");

const cipher_buf = native.code_encrypt_with_buffer(plain_buf,key_buf);
// fs.writeFileSync(join(rootDir,'./tests/fixtures/app.vue.cipher'),cipher_buf);
const plain_buf2 = native.code_decrypt_with_buffer(cipher_buf,key_buf);
// fs.writeFileSync(join(rootDir,'./tests/fixtures/app.vue.txt'),plain_buf2);
```

### 1.5. 数字签名及校验

#### 1.5.1. 生成数字签名

##### 1.5.1.1. 接口函数说明

```js
/**
  * 生成数字签名
  * @param {Buffer} data 要签名的数据
  * @param {Buffer} key 签名密钥Buffer 数据
  * @param {string} digest Hash 算法, 支持 md5, sha1, sha256, sha512，更多请参照：`openssl dgst -list`
  * 常用的 Hash 算法有：
    blake2b512                blake2s256                md4
    md5                       md5-sha1                  mdc2
    ripemd                    ripemd160                 rmd160
    sha1                      sha224                    sha256
    sha3-224                  sha3-256                  sha3-384
    sha3-512                  sha384                    sha512
    sha512-224                sha512-256                shake128
    shake256                  sm3                       ssl3-md5
    ssl3-sha1                 whirlpool
  * @param {Object} options 选项
  * @return {Buffer} 签名后的数据
  */
function sign(data, key, digest, options= {}){}
```

##### 1.5.1.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const plainText = `sunzhifeng <ian.sun@auodigitech.com>`;

// 原文
const plain_buf = Buffer.from(plainText);
// 私钥
const key_buf = fs.readFileSync(join(rootDir,'./certs/key_private.pem'));
// 签名算法
const sign_algorithm = 'sha256';
// 调用签名方法
const sign_buf = native.sign(plain_buf,key_buf,sign_algorithm);
```

#### 1.5.2. 校验数字签名

##### 1.5.2.1. 接口函数说明

```js
/**
  * 校验数字签名
  * @param {Buffer} data 要校验的数据
  * @param {Buffer} key 签名密钥Buffer 数据
  * @param {Buffer} signature 签名Buffer 数据
  * @param {string} digest Hash 算法, 支持 md5, sha1, sha256, sha512，更多请参照：`openssl dgst -list`
  * 常用的 Hash 算法有：
    blake2b512                blake2s256                md4
    md5                       md5-sha1                  mdc2
    ripemd                    ripemd160                 rmd160
    sha1                      sha224                    sha256
    sha3-224                  sha3-256                  sha3-384
    sha3-512                  sha384                    sha512
    sha512-224                sha512-256                shake128
    shake256                  sm3                       ssl3-md5
    ssl3-sha1                 whirlpool
  * @param {Object} options 选项
  * @return {boolean} 校验结果
  */
function verify(data, key, signature, digest, options= {}){} 
```

##### 1.5.2.2. 代码示例

```js
const native = require('@mega-apps/node-ex');
const plainText = `sunzhifeng <ian.sun@auodigitech.com>`;

// 原文
const plain_buf = Buffer.from(plainText);
// 私钥
const skey_buf = fs.readFileSync(join(rootDir,'./certs/key_private.pem'));
// 签名算法
const sign_algorithm = 'sha256';
// 调用签名方法
const sign_buf = native.sign(plain_buf,skey_buf,sign_algorithm);

// 公钥
const pkey_buf = fs.readFileSync(join(rootDir,'./certs/key_public.pem'));
// 验签算法
const verify_algorithm = 'sha256';
// 调用验签方法
const verify_result = native.verify(plain_buf,pkey_buf,sign_buf,verify_algorithm);
```

---
_Source: https://npm.io/package/@mega-apps/node-ex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
