-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathsignature-algorithms.ts
106 lines (86 loc) · 3.12 KB
/
signature-algorithms.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as crypto from "crypto";
import { type SignatureAlgorithm, createOptionalCallbackFunction } from "./types";
export class RsaSha1 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA1");
signer.update(signedInfo);
const res = signer.sign(privateKey, "base64");
return res;
},
);
verifySignature = createOptionalCallbackFunction(
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
const verifier = crypto.createVerify("RSA-SHA1");
verifier.update(material);
const res = verifier.verify(key, signatureValue, "base64");
return res;
},
);
getAlgorithmName = () => {
return "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
};
}
export class RsaSha256 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA256");
signer.update(signedInfo);
const res = signer.sign(privateKey, "base64");
return res;
},
);
verifySignature = createOptionalCallbackFunction(
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
const verifier = crypto.createVerify("RSA-SHA256");
verifier.update(material);
const res = verifier.verify(key, signatureValue, "base64");
return res;
},
);
getAlgorithmName = () => {
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
};
}
export class RsaSha512 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA512");
signer.update(signedInfo);
const res = signer.sign(privateKey, "base64");
return res;
},
);
verifySignature = createOptionalCallbackFunction(
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
const verifier = crypto.createVerify("RSA-SHA512");
verifier.update(material);
const res = verifier.verify(key, signatureValue, "base64");
return res;
},
);
getAlgorithmName = () => {
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
};
}
export class HmacSha1 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createHmac("SHA1", privateKey);
signer.update(signedInfo);
const res = signer.digest("base64");
return res;
},
);
verifySignature = createOptionalCallbackFunction(
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
const verifier = crypto.createHmac("SHA1", key);
verifier.update(material);
const res = verifier.digest("base64");
return res === signatureValue;
},
);
getAlgorithmName = () => {
return "http://www.w3.org/2000/09/xmldsig#hmac-sha1";
};
}