This commit is contained in:
陆柯 2022-05-28 13:32:19 +08:00
parent 74694866bc
commit 32b36dff0a

View File

@ -198,8 +198,8 @@ class TICrypto {
// 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.hex_to_buf(key)), data) let cipherObject = await eccrypto.encrypt(Buffer.from(this.hex_to_buf(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') {
// 对称加密 // 对称加密
if (typeof key === 'string') { if (typeof key === 'string') {
@ -238,7 +238,7 @@ class TICrypto {
try { try {
// eccrypto 只能接受 Buffer, 不接受 Uint8Array, 因为 eccrypto 需要调用 Buffer.compare 方法,不能在这里直接用 hex_to_buf // eccrypto 只能接受 Buffer, 不接受 Uint8Array, 因为 eccrypto 需要调用 Buffer.compare 方法,不能在这里直接用 hex_to_buf
// eccrypto 也只能接受 Buffer, 不接受 Uint8Array // eccrypto 也只能接受 Buffer, 不接受 Uint8Array
// data 需要是 eccrypto 自身encrypt方法返回的 cipherobject. key 是 private key。 // data 需要是 eccrypto 自身encrypt方法返回的 cipherObject. key 是 private key。
let plainbuffer = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // 返回的是 Buffer let plainbuffer = await eccrypto.decrypt(Buffer.from(key, 'hex'), data) // 返回的是 Buffer
return plainbuffer.toString('utf8') return plainbuffer.toString('utf8')
} catch (exception) { } catch (exception) {