diff --git a/src/keys/key.ts b/src/keys/key.ts index c8fd240..f9e1c9d 100644 --- a/src/keys/key.ts +++ b/src/keys/key.ts @@ -17,6 +17,6 @@ export class CryptoKey extends core.CryptoKey { @JsonProp({ type: JsonPropTypes.String }) protected kty = "oct"; - @JsonProp({ type: JsonPropTypes.String }) + @JsonProp({ type: JsonPropTypes.String, optional: true }) protected alg = ""; } diff --git a/test/crypto.ts b/test/crypto.ts index 7c895b8..2f48d68 100644 --- a/test/crypto.ts +++ b/test/crypto.ts @@ -67,7 +67,7 @@ context("Crypto", () => { it("Ed25519", async () => { const keys = await crypto.subtle.generateKey({ name: "eddsa", namedCurve: "ed25519" } as globalThis.EcKeyGenParams, false, ["sign", "verify"]); - + assert.strictEqual(keys.privateKey.algorithm.name, "EdDSA"); assert.strictEqual((keys.privateKey.algorithm as EcKeyAlgorithm).namedCurve, "Ed25519"); }); @@ -195,4 +195,13 @@ context("Crypto", () => { }); }); + it("Import Secret JWK without 'alg' and 'key_ops' fields", async () => { + const aesKey = await crypto.subtle.generateKey({ name: "AES-CBC", length: 256 }, true, ["encrypt", "decrypt"]); + const jwk = await crypto.subtle.exportKey("jwk", aesKey); + delete jwk.key_ops; + delete jwk.alg; + const hmacKey = await crypto.subtle.importKey("jwk", jwk, { name: "HMAC", hash: "SHA-256" } as Algorithm, false, ["sign", "verify"]); + assert.strictEqual(hmacKey.algorithm.name, "HMAC"); + }); + });