From a37d8c0223a96d52cc381149e0c80d25163ce58e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 10 Nov 2020 19:48:36 +0100 Subject: [PATCH] implement AES encryption --- src/platform/web/dom/Crypto.js | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/platform/web/dom/Crypto.js b/src/platform/web/dom/Crypto.js index f80c1a40..77874017 100644 --- a/src/platform/web/dom/Crypto.js +++ b/src/platform/web/dom/Crypto.js @@ -197,6 +197,37 @@ class AESCrypto { throw new Error(`Could not decrypt with AES-CTR: ${err.message}`); } } + + async encryptCTR({key, iv, data}) { + const opts = { + name: "AES-CTR", + counter: iv, + length: 64, + }; + let aesKey; + try { + aesKey = await subtleCryptoResult(this._subtleCrypto.importKey( + "raw", + key, + opts, + false, + ['encrypt'], + ), "importKey"); + } catch (err) { + throw new Error(`Could not import key for AES-CTR decryption: ${err.message}`); + } + try { + const ciphertext = await subtleCryptoResult(this._subtleCrypto.encrypt( + // see https://developer.mozilla.org/en-US/docs/Web/API/AesCtrParams + opts, + aesKey, + data, + ), "encrypt"); + return new Uint8Array(ciphertext); + } catch (err) { + throw new Error(`Could not decrypt with AES-CTR: ${err.message}`); + } + } } @@ -237,6 +268,12 @@ class AESLegacyCrypto { var aesCtr = new aesjs.ModeOfOperation.ctr(new Uint8Array(key), new aesjs.Counter(new Uint8Array(iv))); return aesCtr.decrypt(new Uint8Array(data)); } + + async encryptCTR({key, iv, data}) { + const aesjs = this._aesjs; + var aesCtr = new aesjs.ModeOfOperation.ctr(new Uint8Array(key), new aesjs.Counter(new Uint8Array(iv))); + return aesCtr.encrypt(new Uint8Array(data)); + } } function hashName(name) {