mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 19:08:15 +00:00
feat(ext/crypto): support JWK import for HMAC (#11716)
This commit is contained in:
parent
ad037b3b1a
commit
1f57cd2c0f
5 changed files with 386 additions and 94 deletions
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue