From d2e9f368ccc36bbc70b9a520c699c20ae0d8c284 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sat, 8 May 2021 16:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20sign/verify/encrypt/decryp?= =?UTF-8?q?t=20=E7=9A=84=E5=8F=82=E6=95=B0=E4=B8=BA=E4=B8=80=E6=95=B4?= =?UTF-8?q?=E4=B8=AA=E5=AF=B9=E8=B1=A1=20{...}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b3926a6..219ab63 100644 --- a/index.js +++ b/index.js @@ -176,13 +176,13 @@ class TICrypto { * @return {String} * @memberof TICrypto */ - static async encrypt(data, { tool, keytype = 'pwd', key, input, output, cipher } = {}) { + static async encrypt({ data, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) { if (tool === 'eccrypto') { // data 应当是 utf8 的字符串。key 必须是 pubkey // eccrypto 能用 Uint8Array 和 Buffer // eccrypto-js 只能用 Buffer // 在浏览器里 https://github.com/bitchan/eccrypto 库报错,即使用了 Uint8Array: Failed to execute 'encrypt' on 'SubtleCrypto': The provided value is not of type '(ArrayBuffer or ArrayBufferView)' - let cipherobject = await eccrypto.encrypt(Buffer.from(this.hex2buf(key)), data) // 对 eccrypto 库,使用 + let cipherobject = await eccrypto.encrypt(Buffer.from(this.hex2buf(key)), data) return cipherobject // 返回一个复杂的结构 {iv:Buffer, ciphertext:Buffer, ...}。对同样的key和data,每次返回的结果不一样 } else if (keytype === 'pwd') { // 对称加密 @@ -216,14 +216,15 @@ class TICrypto { * @return {String} * @memberof TICrypto */ - static async decrypt(data = {}, { tool, keytype = 'pwd', key, input, output, cipher } = {}) { + static async decrypt({ data = {}, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) { // data 应当是 encrypt 输出的数据类型 if (tool === 'eccrypto') { try { // eccrypto 只能接受 Buffer, 不接受 Uint8Array, 因为 eccrypto 需要调用 Buffer.compare 方法,不能在这里直接用 hex2buf // eccrypto 也只能接受 Buffer, 不接受 Uint8Array - let plaindata = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // data 需要是 eccrypto 自身encrypt方法返回的 cipherobject - return plaindata.toString('utf8') + // data 需要是 eccrypto 自身encrypt方法返回的 cipherobject. key 是 private key。 + let plainbuffer = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // 返回的是 Buffer + return plainbuffer.toString('utf8') } catch (exception) { // eccrypto 对无法解密的,会抛出异常 return null @@ -264,7 +265,7 @@ class TICrypto { * @return {String} * @memberof TICrypto */ - static async sign(data, seckey, { tool, hasher }) { + static async sign({ data, seckey, tool = 'crypto', hasher }) { // data can be string or buffer or object, results are the same if (this.isHashable(data) && this.isSeckey(seckey)) { if (tool === 'nacl' && seckey.length === 128) { @@ -299,7 +300,7 @@ class TICrypto { * @return {Boolean} * @memberof TICrypto */ - static async verify(data, signature, pubkey, { tool, hasher }) { + static async verify({ data, signature, pubkey, tool = 'crypto', hasher }) { // data could be anything, but converts to string or remains be Buffer/TypedArray/DataView if (this.isHashable(data) && this.isSignature(signature) && this.isPubkey(pubkey)) { if ('nacl' === tool && signature.length === 128) {