I want to end-to-end encrypt items in a stateless environment. These items have an identifier (random string). Now I want to add encryption by adding a random key. The URL could be https://server.tld/item#key
which users can share with each other.
I want to only use Web Crypto API (SubtleCrypto
) without any additional libraries.
My idea so far:
- Generate random key:
window.crypto.subtle.generateKey()window.crypto.getRandomValues()
- Base64 encode/decode for URI fragment:
btoa()
/atob()
- Create key object:
window.crypto.subtle.importKey()
- Encrypt/Decrypt using AES-CBC:
window.crypto.subtle.encrypt()
Now I need an iv
. I thought about using the hash of the item identifier via window.crypto.subtle.digest()
. However AES-CBC needs 16 bytes.
Is it a good idea to use the first 16 bytes of SHA-1 (20 bytes long) as IV?
https://server.tld/item#key$iv
. $\endgroup$ – dsprenkels Jun 18 '20 at 16:14