UNPKG

@mega-apps/node-ex

Version:

Mega-Apps node addon.

335 lines (271 loc) 11 kB
# @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); ```