|
1 | 1 | import bip39Words from '@/lib/bip39-words' |
2 | | -import { fromHex, toHex } from '@/lib/hex' |
3 | 2 |
|
4 | 3 | export async function deriveKey (passphrase, salt) { |
5 | 4 | const enc = new TextEncoder() |
@@ -28,7 +27,7 @@ export async function deriveKey (passphrase, salt) { |
28 | 27 | ) |
29 | 28 |
|
30 | 29 | const rawKey = await window.crypto.subtle.exportKey('raw', key) |
31 | | - const hash = toHex(await window.crypto.subtle.digest('SHA-256', rawKey)) |
| 30 | + const hash = Buffer.from(await window.crypto.subtle.digest('SHA-256', rawKey)).toString('hex') |
32 | 31 | const unextractableKey = await window.crypto.subtle.importKey( |
33 | 32 | 'raw', |
34 | 33 | rawKey, |
@@ -59,19 +58,19 @@ export async function encrypt ({ key, hash }, value) { |
59 | 58 | ) |
60 | 59 | return { |
61 | 60 | keyHash: hash, |
62 | | - iv: toHex(iv.buffer), |
63 | | - value: toHex(encrypted) |
| 61 | + iv: Buffer.from(iv).toString('hex'), |
| 62 | + value: Buffer.from(encrypted).toString('hex') |
64 | 63 | } |
65 | 64 | } |
66 | 65 |
|
67 | 66 | export async function decrypt (key, { iv, value }) { |
68 | 67 | const decrypted = await window.crypto.subtle.decrypt( |
69 | 68 | { |
70 | 69 | name: 'AES-GCM', |
71 | | - iv: fromHex(iv) |
| 70 | + iv: Buffer.from(iv, 'hex') |
72 | 71 | }, |
73 | 72 | key, |
74 | | - fromHex(value) |
| 73 | + Buffer.from(value, 'hex') |
75 | 74 | ) |
76 | 75 | const decoded = new TextDecoder().decode(decrypted) |
77 | 76 | return JSON.parse(decoded) |
|
0 commit comments