Skip to content

Instantly share code, notes, and snippets.

@asanso
Last active January 6, 2020 14:49
Show Gist options
  • Save asanso/a3ece02d174760810cfcfca9ce818a01 to your computer and use it in GitHub Desktop.
Save asanso/a3ece02d174760810cfcfca9ce818a01 to your computer and use it in GitHub Desktop.
//https://github.com/diafygi/webcrypto-examples
window.crypto.subtle.encrypt(
{
name: "AES-GCM",
//Don't re-use initialization vectors!
//Always generate a new iv every time your encrypt!
//Recommended to use 12 bytes length
iv: window.crypto.getRandomValues(new Uint8Array(12)),
//Additional authentication data (optional)
additionalData: ArrayBuffer,
//Tag length (optional)
tagLength: 128, //can be 32, 64, 96, 104, 112, 120 or 128 (default)
},
key, //from generateKey or importKey above
data //ArrayBuffer of data you want to encrypt
)
.then(function(encrypted){
//returns an ArrayBuffer containing the encrypted data
console.log(new Uint8Array(encrypted));
})
.catch(function(err){
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment