From 035811d1b4cbd2cd34a55f858ac7d707183f6e71 Mon Sep 17 00:00:00 2001 From: microshine Date: Wed, 29 May 2024 11:23:08 +0200 Subject: [PATCH] feat: Enhance EdAlgorithm to support Ed25519 and X25519 algorithms for WebCrypto in Chrome, Safari, and NodeJS --- src/ed_algorithm.ts | 16 +++++++++++++--- test/crypto.ts | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ed_algorithm.ts b/src/ed_algorithm.ts index 3368e10a..04e222ec 100644 --- a/src/ed_algorithm.ts +++ b/src/ed_algorithm.ts @@ -21,7 +21,16 @@ export class EdAlgorithm implements IAlgorithm { public toAsnAlgorithm(alg: EcKeyGenParams): AlgorithmIdentifier | null { let algorithm: string | null = null; switch (alg.name.toLowerCase()) { + case "ed25519": + // This algorithm is supported by WebCrypto API + algorithm = idEd25519; + break; + case "x25519": + // This algorithm is supported by WebCrypto API + algorithm = idX25519; + break; case "eddsa": + // This algorithm works with @peculiar/webcrypto only switch (alg.namedCurve.toLowerCase()) { case "ed25519": algorithm = idEd25519; @@ -32,6 +41,7 @@ export class EdAlgorithm implements IAlgorithm { } break; case "ecdh-es": + // This algorithm works with @peculiar/webcrypto only switch (alg.namedCurve.toLowerCase()) { case "x25519": algorithm = idX25519; @@ -50,14 +60,14 @@ export class EdAlgorithm implements IAlgorithm { return null; } - public toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | null { + public toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | Algorithm | null { switch (alg.algorithm) { case idEd25519: - return { name: "EdDSA", namedCurve: "Ed25519" }; + return { name: "Ed25519" }; case idEd448: return { name: "EdDSA", namedCurve: "Ed448" }; case idX25519: - return { name: "ECDH-ES", namedCurve: "X25519" }; + return { name: "X25519" }; case idX448: return { name: "ECDH-ES", namedCurve: "X448" }; } diff --git a/test/crypto.ts b/test/crypto.ts index 6cbbb4c0..128441fd 100644 --- a/test/crypto.ts +++ b/test/crypto.ts @@ -762,7 +762,7 @@ ZYYG const ok = await cert.verify({ signatureOnly: true, }); - assert.deepStrictEqual(cert.signatureAlgorithm, { name: "EdDSA", namedCurve: "Ed25519" }); + assert.deepStrictEqual(cert.signatureAlgorithm, { name: "Ed25519" }); assert.strictEqual(ok, true); });