summaryrefslogtreecommitdiff
path: root/cli/tests/unit/webcrypto_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit/webcrypto_test.ts')
-rw-r--r--cli/tests/unit/webcrypto_test.ts47
1 files changed, 36 insertions, 11 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts
index 0e4b17b9a..a37e32eba 100644
--- a/cli/tests/unit/webcrypto_test.ts
+++ b/cli/tests/unit/webcrypto_test.ts
@@ -243,22 +243,43 @@ unitTest(async function testSignRSASSAKey() {
assert(signature);
});
+// deno-fmt-ignore
+const rawKey = new Uint8Array([
+ 1, 2, 3, 4, 5, 6, 7, 8,
+ 9, 10, 11, 12, 13, 14, 15, 16
+]);
+
+const jwk: JsonWebKey = {
+ kty: "oct",
+ // unpadded base64 for rawKey.
+ k: "AQIDBAUGBwgJCgsMDQ4PEA",
+ alg: "HS256",
+};
+
unitTest(async function subtleCryptoHmacImportExport() {
- // deno-fmt-ignore
- const rawKey = new Uint8Array([
- 1, 2, 3, 4, 5, 6, 7, 8,
- 9, 10, 11, 12, 13, 14, 15, 16
- ]);
- const key = await crypto.subtle.importKey(
+ const key1 = await crypto.subtle.importKey(
"raw",
rawKey,
{ name: "HMAC", hash: "SHA-256" },
true,
["sign"],
);
- const actual = await crypto.subtle.sign(
+ const key2 = await crypto.subtle.importKey(
+ "jwk",
+ jwk,
+ { name: "HMAC", hash: "SHA-256" },
+ true,
+ ["sign"],
+ );
+ const actual1 = await crypto.subtle.sign(
{ name: "HMAC" },
- key,
+ key1,
+ new Uint8Array([1, 2, 3, 4]),
+ );
+
+ const actual2 = await crypto.subtle.sign(
+ { name: "HMAC" },
+ key2,
new Uint8Array([1, 2, 3, 4]),
);
// deno-fmt-ignore
@@ -269,10 +290,14 @@ unitTest(async function subtleCryptoHmacImportExport() {
23, 122, 222, 1, 146, 46, 182, 87,
]);
assertEquals(
- new Uint8Array(actual),
+ new Uint8Array(actual1),
expected,
);
-
- const exportedKey = await crypto.subtle.exportKey("raw", key);
+ assertEquals(
+ new Uint8Array(actual2),
+ expected,
+ );
+ // TODO(@littledivy): Add a test for exporting JWK key when supported.
+ const exportedKey = await crypto.subtle.exportKey("raw", key1);
assertEquals(new Uint8Array(exportedKey), rawKey);
});