From ab105032ad1d53ca6414d61604347e2d37013454 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sat, 12 Nov 2022 08:42:06 +0800 Subject: [PATCH] rename ethrsa to ecrsa --- ticc.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ticc.js b/ticc.js index 0c8b037..1869310 100644 --- a/ticc.js +++ b/ticc.js @@ -8,7 +8,7 @@ const uuid = require('uuid') const keccak = require('keccak') const ecc = require('eccrypto-js') // 用于加解密。eccrypto 在 windows 上和 openssl 的版本兼容性有点麻烦,所以换用 eccrypto-js const keyman = require('js-crypto-key-utils') // 转换原始密钥和 PER/DER 格式。 -const ethrsa = require('ethereum-rsa') +const ecrsa = require('ethereum-rsa') // const BitcoreMnemonic = require('bitcore-mnemonic') // https://bitcore.io/api/mnemonic/ https://github.com/bitpay/bitcore-mnemonic // 打包成 app 里常有问题,试图访问 window 变量,无法生成 secword const bip39 = require('bip39') // https://github.com/bitcoinjs/bip39 // 有更多语言,但不方便选择语言,也不能使用 pass const hdkey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或者用 bitcore-mnemonic 或者 ethers 里的相同功能 @@ -220,11 +220,11 @@ class TicCrypto { */ static async encrypt_easy ({ data, mode = 'semkey', key, input = my.INPUT, output = my.OUTPUT, cipher = my.CIPHER } = {}) { if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data) - if (mode === 'ethrsa') { + if (mode === 'ecrsa') { if (key?.prikey && key?.pubkey) { - return ethrsa.encryptMessage(data, key?.prikey, key?.pubkey) + return ecrsa.encryptMessage(data, key?.prikey, key?.pubkey) } else { - return ethrsa.encryptMessage(data, key?.senderPrikey, key?.receiverPubkey) + return ecrsa.encryptMessage(data, key?.senderPrikey, key?.receiverPubkey) } } else if (mode === 'ecc') { // data 应当是 utf8 的字符串。key 必须是 pubkey @@ -265,11 +265,11 @@ class TicCrypto { */ static async decrypt_easy ({ data = {}, mode = 'semkey', key, input = my.OUTPUT, output = 'utf8', cipher = my.CIPHER } = {}) { // data 应当是 encrypt 输出的数据类型 - if (mode === 'ethrsa') { + if (mode === 'ecrsa') { if (key?.prikey && key?.pubkey) { - return ethrsa.decryptMessage(data, key?.prikey, key?.pubkey) + return ecrsa.decryptMessage(data, key?.prikey, key?.pubkey) } else { - return ethrsa.decryptMessage(data, key?.receiverPrikey, key?.senderPubkey) + return ecrsa.decryptMessage(data, key?.receiverPrikey, key?.senderPubkey) } } else if (mode === 'ecc') { try { @@ -282,7 +282,7 @@ class TicCrypto { // eccrypto 对无法解密的,会抛出异常 return null } - } else if (mode === 'semkey') { + } else if (mode === 'semkey' && data?.cipher && data?.iv) { // 对称解密 if (typeof data.ciphertext === 'string' || data.ciphertext instanceof Buffer) { let inputEncoding = input // input (=output of encrypt) could be 'latin1', 'base64', 'hex' by default for string or ignored for Buffer @@ -296,11 +296,11 @@ class TicCrypto { } else if (mode === 'prikey') { // 只能用于 crypto.generateKeyPairSync('rsa') 生成的 rsa 公私钥,不能用于 Elliptic Curve 的公私钥 let prikeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。 - return crypto.privateDecrypt(prikeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。 + return crypto.privateDecrypt(prikeyPEM, Buffer.from(data)) // 返回 Buffer。 } else if (mode === 'pubkey') { // 只能用于 crypto.generateKeyPairSync('rsa') 生成的 rsa 公私钥,不能用于 Elliptic Curve 的公私钥 let pubkeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') - return crypto.publicDecrypt(pubkeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果不一样。 + return crypto.publicDecrypt(pubkeyPEM, Buffer.from(data)) // 返回 Buffer。 } return null }