From 58f04d8e4600445fe681e845e06f28095c888379 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 12 Oct 2021 16:09:46 +0530 Subject: feat(ext/crypto): implement deriveKey (#12117) --- cli/tests/unit/webcrypto_test.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'cli/tests') diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 420fa2a7f..a6e0f28f8 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -513,6 +513,42 @@ unitTest(async function testHkdfDeriveBits() { assertEquals(result.byteLength, 128 / 8); }); +unitTest(async function testDeriveKey() { + // Test deriveKey + const rawKey = await crypto.getRandomValues(new Uint8Array(16)); + const key = await crypto.subtle.importKey( + "raw", + rawKey, + "PBKDF2", + false, + ["deriveKey", "deriveBits"], + ); + + const salt = await crypto.getRandomValues(new Uint8Array(16)); + const derivedKey = await crypto.subtle.deriveKey( + { + name: "PBKDF2", + salt, + iterations: 1000, + hash: "SHA-256", + }, + key, + { name: "HMAC", hash: "SHA-256" }, + true, + ["sign"], + ); + + assert(derivedKey instanceof CryptoKey); + assertEquals(derivedKey.type, "secret"); + assertEquals(derivedKey.extractable, true); + assertEquals(derivedKey.usages, ["sign"]); + + const algorithm = derivedKey.algorithm as HmacKeyAlgorithm; + assertEquals(algorithm.name, "HMAC"); + assertEquals(algorithm.hash.name, "SHA-256"); + assertEquals(algorithm.length, 256); +}); + unitTest(async function testAesCbcEncryptDecrypt() { const key = await crypto.subtle.generateKey( { name: "AES-CBC", length: 128 }, -- cgit v1.2.3