From 0520ae62dd2f4e61287315bbfcd548864d46da45 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 13 Sep 2021 02:32:49 +0530 Subject: [PATCH] fix(ext/crypto): add HkdfParams and Pkdf2Params types (#11991) --- cli/tests/unit/webcrypto_test.ts | 24 ++++++++++++++++++++++++ ext/crypto/lib.deno_crypto.d.ts | 17 +++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index a5de9fa367..57ab051d10 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -356,3 +356,27 @@ unitTest(async function subtleCryptoHmacImportExport() { const exportedKey2 = await crypto.subtle.exportKey("jwk", key2); assertEquals(exportedKey2, jwk); }); + +unitTest(async function testHkdfDeriveBits() { + const rawKey = await crypto.getRandomValues(new Uint8Array(16)); + const key = await crypto.subtle.importKey( + "raw", + rawKey, + { name: "HKDF", hash: "SHA-256" }, + false, + ["deriveBits"], + ); + const salt = await crypto.getRandomValues(new Uint8Array(16)); + const info = await crypto.getRandomValues(new Uint8Array(16)); + const result = await crypto.subtle.deriveBits( + { + name: "HKDF", + hash: "SHA-256", + salt: salt, + info: info, + }, + key, + 128, + ); + assertEquals(result.byteLength, 128 / 8); +}); diff --git a/ext/crypto/lib.deno_crypto.d.ts b/ext/crypto/lib.deno_crypto.d.ts index a62e696328..5a554015a9 100644 --- a/ext/crypto/lib.deno_crypto.d.ts +++ b/ext/crypto/lib.deno_crypto.d.ts @@ -109,6 +109,18 @@ interface RsaKeyAlgorithm extends KeyAlgorithm { publicExponent: Uint8Array; } +interface HkdfParams extends Algorithm { + hash: HashAlgorithmIdentifier; + info: BufferSource; + salt: BufferSource; +} + +interface Pbkdf2Params extends Algorithm { + hash: HashAlgorithmIdentifier; + iterations: number; + salt: BufferSource; +} + /** The CryptoKey dictionary of the Web Crypto API represents a cryptographic key. */ interface CryptoKey { readonly algorithm: KeyAlgorithm; @@ -194,6 +206,11 @@ interface SubtleCrypto { key: CryptoKey, data: BufferSource, ): Promise; + deriveBits( + algorithm: AlgorithmIdentifier | HkdfParams | Pbkdf2Params, + baseKey: CryptoKey, + length: number, + ): Promise; } declare interface Crypto {