Expand description

Code related to AES_CTR mode

Functions

Decrypt with an AES_CTR key

example
const data = await AES_CTR.decrypt({length, counter}, key.self, data);
example
const data = await key.decrypt({length, counter}, data);

Parameters

Returns Promise<ArrayBuffer>

Encrypt with an AES_CTR key

example
const key = await AES_CTR.generateKey();
const message = new TextEncoder().encode("a message");
const length = 8;
const counter = await AES_CTR.generateCounter(length);
const data = await AES_CTR.encrypt({length, counter}, key.self, message);
example
const key = await AES_CTR.generateKey();
const message = new TextEncoder().encode("a message");
const length = 8;
const counter = await AES_CTR.generateCounter(length);
const data = await key.encrypt({length, counter}, message);

Parameters

Returns Promise<ArrayBuffer>

Export an AES_CTR key into the specified format

example
const key = await AES_CTR.generateKey();
const jwk = await AES_CTR.exportKey("jwk", key.self);
example
const key = await AES_CTR.generateKey();
const jwk = await key.exportKey("jwk");

Parameters

Returns Promise<ArrayBuffer | JsonWebKey>

Generates a counter, with the given length, starting from the count of 1. The nonce is randomized.

example
const counter = await AES_CTR.generateCounter();

Parameters

  • counterLength: number = 8

Returns Promise<Uint8Array>

Generate a new AES_CTR key

example
const key = await AES_CTR.generateKey();

Parameters

  • algorithm: Omit<EnforcedAesKeyGenParams, "name"> = { length: 256, }
  • extractable: boolean = true
  • Optional keyUsages: KeyUsage[]

Returns Promise<AesCtrProxiedCryptoKey>

Import an AES_CTR key from the specified format

example
const key = await AES_CTR.importKey("jwk", jwk, { length: 256 });

Parameters

  • format: KeyFormat
  • key: BufferSource | JsonWebKey
  • algorithm: Omit<AesCtrKeyAlgorithm, "name">
  • Optional extractable: boolean
  • Optional keyUsages: KeyUsage[]

Returns Promise<AesCtrProxiedCryptoKey>

Unwrap a wrapped key using the key encryption key

example
const dek = await AES_CTR.unwrapKey("raw", wrappedKey, {name: "AES_CTR"}, kek.self, {length, counter});
example
const dek = await kek.unwrapKey("raw", wrappedKey, {name: "AES_CTR"}, {length, counter});

Parameters

Returns Promise<CryptoKey>

Wrap another key with an AES_CTR key

example
const kek = await AES_CTR.generateKey({length: 256}, true, ['wrapKey', 'unwrapKey']);
const dek = await AES_CTR.generateKey();
const length = 8;
const counter = await AES_CTR.generateCounter(length);
const wrappedKey = await AES_CTR.wrapKey("raw", dek.self, kek.self, {length, counter});
example
const kek = await AES_CTR.generateKey({length: 256}, true, ['wrapKey', 'unwrapKey']);
const dek = await AES_CTR.generateKey();
const length = 8;
const counter = await AES_CTR.generateCounter(length);
const wrappedKey = await kek.wrapKey("raw", dek.self, {length, counter});

Parameters

Returns Promise<ArrayBuffer>