优化 sign/verify/encrypt/decrypt 的参数为一整个对象 {...}

This commit is contained in:
陆柯 2021-05-08 16:48:34 +08:00
parent 1a329157bd
commit d2e9f368cc

View File

@ -176,13 +176,13 @@ class TICrypto {
* @return {String} * @return {String}
* @memberof TICrypto * @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') { if (tool === 'eccrypto') {
// data 应当是 utf8 的字符串。key 必须是 pubkey // data 应当是 utf8 的字符串。key 必须是 pubkey
// eccrypto 能用 Uint8Array 和 Buffer // eccrypto 能用 Uint8Array 和 Buffer
// eccrypto-js 只能用 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)' // 在浏览器里 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每次返回的结果不一样 return cipherobject // 返回一个复杂的结构 {iv:Buffer, ciphertext:Buffer, ...}。对同样的key和data每次返回的结果不一样
} else if (keytype === 'pwd') { } else if (keytype === 'pwd') {
// 对称加密 // 对称加密
@ -216,14 +216,15 @@ class TICrypto {
* @return {String} * @return {String}
* @memberof TICrypto * @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 输出的数据类型 // data 应当是 encrypt 输出的数据类型
if (tool === 'eccrypto') { if (tool === 'eccrypto') {
try { try {
// eccrypto 只能接受 Buffer, 不接受 Uint8Array, 因为 eccrypto 需要调用 Buffer.compare 方法,不能在这里直接用 hex2buf // eccrypto 只能接受 Buffer, 不接受 Uint8Array, 因为 eccrypto 需要调用 Buffer.compare 方法,不能在这里直接用 hex2buf
// eccrypto 也只能接受 Buffer, 不接受 Uint8Array // eccrypto 也只能接受 Buffer, 不接受 Uint8Array
let plaindata = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // data 需要是 eccrypto 自身encrypt方法返回的 cipherobject // data 需要是 eccrypto 自身encrypt方法返回的 cipherobject. key 是 private key。
return plaindata.toString('utf8') let plainbuffer = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // 返回的是 Buffer
return plainbuffer.toString('utf8')
} catch (exception) { } catch (exception) {
// eccrypto 对无法解密的,会抛出异常 // eccrypto 对无法解密的,会抛出异常
return null return null
@ -264,7 +265,7 @@ class TICrypto {
* @return {String} * @return {String}
* @memberof TICrypto * @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 // data can be string or buffer or object, results are the same
if (this.isHashable(data) && this.isSeckey(seckey)) { if (this.isHashable(data) && this.isSeckey(seckey)) {
if (tool === 'nacl' && seckey.length === 128) { if (tool === 'nacl' && seckey.length === 128) {
@ -299,7 +300,7 @@ class TICrypto {
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @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 // 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 (this.isHashable(data) && this.isSignature(signature) && this.isPubkey(pubkey)) {
if ('nacl' === tool && signature.length === 128) { if ('nacl' === tool && signature.length === 128) {