@hackbg/miscreant-esm
Version:
(ESM port) Misuse resistant symmetric encryption library providing AES-SIV (RFC 5297), AES-PMAC-SIV, and STREAM constructions
28 lines • 997 B
JavaScript
/**
* AES-CTR using a WebCrypto (or similar) API
*/
export default class WebCryptoAesCtr {
key;
crypto;
static async importKey(crypto, keyData) {
// Only AES-128 and AES-256 supported. AES-192 is not.
if (keyData.length !== 16 && keyData.length !== 32) {
throw new Error(`Miscreant: invalid key length: ${keyData.length} (expected 16 or 32 bytes)`);
}
const key = await crypto.subtle.importKey("raw", keyData, "AES-CTR", false, ["encrypt"]);
return new WebCryptoAesCtr(key, crypto);
}
constructor(key, crypto) {
this.key = key;
this.crypto = crypto;
}
async encryptCtr(iv, plaintext) {
const ciphertext = await this.crypto.subtle.encrypt({ name: "AES-CTR", counter: iv, length: 16 }, this.key, plaintext);
return new Uint8Array(ciphertext);
}
clear() {
// TODO: actually clear something. Do we need to?
return this;
}
}
//# sourceMappingURL=aes_ctr.js.map