UNPKG

miscreant

Version:

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

41 lines (40 loc) 1.81 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * AES-CTR using a WebCrypto (or similar) API */ class WebCryptoAesCtr { constructor(key, crypto) { this.key = key; this.crypto = crypto; } static importKey(crypto, keyData) { return __awaiter(this, void 0, void 0, function* () { // 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 = yield crypto.subtle.importKey("raw", keyData, "AES-CTR", false, ["encrypt"]); return new WebCryptoAesCtr(key, crypto); }); } encryptCtr(iv, plaintext) { return __awaiter(this, void 0, void 0, function* () { const ciphertext = yield 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; } } exports.default = WebCryptoAesCtr;