Skip to content

Commit

Permalink
feat: Enhance EdAlgorithm to support Ed25519 and X25519 algorithms fo…
Browse files Browse the repository at this point in the history
…r WebCrypto in Chrome, Safari, and NodeJS
  • Loading branch information
microshine committed May 29, 2024
1 parent a61fb03 commit 035811d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/ed_algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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" };
}
Expand Down
2 changes: 1 addition & 1 deletion test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 035811d

Please sign in to comment.