| 61 | * Decrypt an `EncryptedItem` to an `Item`. |
| 62 | */ |
| 63 | async function decrypt(item: EncryptedItem): Promise<Item> { |
| 64 | const { id, ciphertext, iv: iv_str } = item |
| 65 | |
| 66 | const encrypted = base64.toByteArray(ciphertext) |
| 67 | const iv = base64.toByteArray(iv_str) |
| 68 | |
| 69 | const decrypted = await crypto.subtle.decrypt( |
| 70 | { |
| 71 | iv: iv as BufferSource, |
| 72 | name: `AES-GCM`, |
| 73 | }, |
| 74 | key, |
| 75 | encrypted as BufferSource |
| 76 | ) |
| 77 | |
| 78 | const dec = new TextDecoder() |
| 79 | const title = dec.decode(decrypted) |
| 80 | |
| 81 | return { |
| 82 | id, |
| 83 | title, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export const Example = () => { |
| 88 | const [items, setItems] = useState<Item[]>() |