From aeab471bea7ad2f2be9270754dad8344259e922f Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 3 Oct 2021 18:54:46 +0530 Subject: [PATCH] fix(ext/crypto): missing Aes key typings (#12307) --- cli/tests/unit/webcrypto_test.ts | 21 +++++++++++++++++++++ ext/crypto/lib.deno_crypto.d.ts | 10 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 56a23bfb5f..dbcd0f1b10 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -499,3 +499,24 @@ unitTest(async function testHkdfDeriveBits() { ); assertEquals(result.byteLength, 128 / 8); }); + +// Doesn't need to cover all cases. +// Only for testing types. +unitTest(async function testAesKeyGen() { + const key = await crypto.subtle.generateKey( + { + name: "AES-GCM", + length: 256, + }, + true, + ["encrypt", "decrypt"], + ); + + assert(key); + assertEquals(key.type, "secret"); + assertEquals(key.extractable, true); + assertEquals(key.usages, ["encrypt", "decrypt"]); + const algorithm = key.algorithm as AesKeyAlgorithm; + assertEquals(algorithm.name, "AES-GCM"); + assertEquals(algorithm.length, 256); +}); diff --git a/ext/crypto/lib.deno_crypto.d.ts b/ext/crypto/lib.deno_crypto.d.ts index 55b94c24de..0ad40d1309 100644 --- a/ext/crypto/lib.deno_crypto.d.ts +++ b/ext/crypto/lib.deno_crypto.d.ts @@ -125,6 +125,14 @@ interface Pbkdf2Params extends Algorithm { salt: BufferSource; } +interface AesKeyGenParams extends Algorithm { + length: number; +} + +interface AesKeyAlgorithm extends KeyAlgorithm { + length: number; +} + /** The CryptoKey dictionary of the Web Crypto API represents a cryptographic key. */ interface CryptoKey { readonly algorithm: KeyAlgorithm; @@ -157,7 +165,7 @@ interface SubtleCrypto { keyUsages: KeyUsage[], ): Promise; generateKey( - algorithm: HmacKeyGenParams, + algorithm: AesKeyGenParams | HmacKeyGenParams, extractable: boolean, keyUsages: KeyUsage[], ): Promise;