UNPKG

@hackbg/miscreant-esm

Version:

(ESM port) Misuse resistant symmetric encryption library providing AES-SIV (RFC 5297), AES-PMAC-SIV, and STREAM constructions

32 lines (31 loc) 948 B
import Block from "../../internals/block.dist.mjs"; export default class WebCryptoAes { _crypto; _key; static async importKey(crypto, keyData) { 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-CBC", false, ["encrypt"]); return new WebCryptoAes(crypto, key); } _iv = new Block(); _emptyPromise; constructor(_crypto, _key) { this._crypto = _crypto; this._key = _key; this._emptyPromise = Promise.resolve(this); } clear() { return this; } async encryptBlock(block) { const params = { name: "AES-CBC", iv: this._iv.data }; const ctBlock = await this._crypto.subtle.encrypt(params, this._key, block.data); block.data.set(new Uint8Array(ctBlock, 0, Block.SIZE)); return this._emptyPromise; } }