Web Crypto API

Published on April 06, 2021

← Back to tips

Provides a standard way to work with cryptographic algorithms on the web.

Encrypt, decrypt, verify and/or sign content right in your browser.

You can generate a cryptographic key using 10 different algorithms, from older RSA-PSS to AES-GCM or ECDSA.

The API requires you to specify only the algorithm to be used and the purpose of the key.

Generate key

Once generated, you can use the key object to encrypt/decrypt any data.

In addition, you need an initialization vector(also known as nonce) for encryption, which complements the whole process.

Encrypt Data

And using the same nonce, the other party can decrypt the already encrypted data.

PS: All data operations using the crypto API return array buffers.

Decrypt Data

Who uses it?

Excalidraw (@excalidraw), a virtual whiteboard for sketching hand-drawn like diagrams, uses the crypto API for saving the diagrams for facilitating collaboration.

App: https://excalidraw.com

Code: https://github.com/excalidraw/excalidraw/blob/25fd27515866b5704066d9301dd641c481f6c38c/src/excalidraw-app/data/index.ts#L21

More reading Down pointing backhand index https://w3.org/TR/WebCryptoAPI/