AES-GCM
January 30, 2019 ยท View on GitHub
Operations
Generate key
const key = await crypto.subtle.generateKey(
{
name: "AES-CTR",
length: 128, // 128, 192, or 256
},
false, // extractable
["encrypt", "decrypt", "wrapKey", "unwrapKey"], // key usages
);
Import key
const key = await crypto.subtle.importKey(
"raw", // raw or jwk
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]), // raw data
"AES-CTR",
false, // extractable
["encrypt", "decrypt"],
);
Export key
const raw = await crypto.subtle.exportKey(
"raw", // raw or jwk
key,
);
Encrypt
const iv = crypto.getRandomValues(new Uint8Array(12));
const additionalData = crypto.getRandomValues(new Uint8Array(4));
const encData = await crypto.subtle.encrypt(
{
name: "AES-GCM",
iv, // BufferSource
additionalData, // Optional. The additional authentication data to include
tagLength: 128, // 32, 64, 96, 104, 112, 120 or 128 (default)
},
key, // AES key
data, // BufferSource
);
Decrypt
const data = await crypto.subtle.decrypt(
{
name: "AES-GCM",
iv, // BufferSource
additionalData, // Optional. The additional authentication data to include
tagLength: 128, // 32, 64, 96, 104, 112, 120 or 128 (default)
},
key, // AES key
encData, // BufferSource
);
Wrap key
const iv = crypto.getRandomValues(new Uint8Array(12));
const additionalData = crypto.getRandomValues(new Uint8Array(4));
const wrappedKey = await crypto.subtle.wrapKey(
"pkcs8", // raw, pkcs8, spki, or jwk
anyKey, // Crypto key
key, // AES key
{
name: "AES-GCM",
iv, // BufferSource
additionalData, // Optional. The additional authentication data to include
tagLength: 128, // 32, 64, 96, 104, 112, 120 or 128 (default)
},
);
Unwrap key
const unwrappedKey = await crypto.subtle.unwrapKey(
"pkcs8", // raw, pkcs8, spki, or jwk
wrappedKey, // BufferSource
key, // AES key
{
name: "AES-GCM",
iv, // BufferSource
additionalData, // Optional. The additional authentication data to include
tagLength: 128, // 32, 64, 96, 104, 112, 120 or 128 (default)
},
{
name: "RSA-PSS",
hash: "SHA-256",
}
false, // extractable
["sign", "verify"],
);