-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathed_algorithm.ts
81 lines (73 loc) · 2.3 KB
/
ed_algorithm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
import { container, injectable } from "tsyringe";
import { diAlgorithm, IAlgorithm } from "./algorithm";
import { HashedAlgorithm } from "./types";
// id-X25519 OBJECT IDENTIFIER ::= { 1 3 101 110 }
export const idX25519 = "1.3.101.110";
// id-X448 OBJECT IDENTIFIER ::= { 1 3 101 111 }
export const idX448 = "1.3.101.111";
// id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }
export const idEd25519 = "1.3.101.112";
// id-Ed448 OBJECT IDENTIFIER ::= { 1 3 101 113 }
export const idEd448 = "1.3.101.113";
/**
* ECDH-ES and EdDSA algorithm provider
*/
@injectable()
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;
break;
case "ed448":
algorithm = idEd448;
break;
}
break;
case "ecdh-es":
// This algorithm works with @peculiar/webcrypto only
switch (alg.namedCurve.toLowerCase()) {
case "x25519":
algorithm = idX25519;
break;
case "x448":
algorithm = idX448;
break;
}
}
if (algorithm) {
return new AlgorithmIdentifier({
algorithm,
});
}
return null;
}
public toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | Algorithm | null {
switch (alg.algorithm) {
case idEd25519:
return { name: "Ed25519" };
case idEd448:
return { name: "EdDSA", namedCurve: "Ed448" };
case idX25519:
return { name: "X25519" };
case idX448:
return { name: "ECDH-ES", namedCurve: "X448" };
}
return null;
}
}
// register ED algorithm provider as a singleton object
container.registerSingleton(diAlgorithm, EdAlgorithm);