-
Notifications
You must be signed in to change notification settings - Fork 2
/
signature_validator.js
50 lines (45 loc) · 1.14 KB
/
signature_validator.js
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
const jsrsasign = require('jsrsasign');
/** @description A validator of ECDSA-SHA256 signature */
class SignatureValidator {
/**
* @param {String} certificate PEM
*/
constructor(certificate) {
this.publicKey = jsrsasign.KEYUTIL.getPEM(
jsrsasign.KEYUTIL.getKey(certificate),
'PKCS8PUB',
);
}
/**
* @param {Uint8Array} toBeValidated
* @param {Uint8Array} signature
* @return {Boolean}
*/
async validate(toBeValidated, signature) {
if (!this.key) {
const pkcs8 = this.publicKey
.replace('-----BEGIN PUBLIC KEY-----', '')
.replace('-----END PUBLIC KEY-----', '')
.replace(/(\r\n|\n|\r)/gm, '');
this.key = await window.crypto.subtle.importKey(
'spki',
jsrsasign.hextoArrayBuffer(jsrsasign.b64utohex(pkcs8)),
{
name: 'ECDSA',
namedCurve: 'P-256',
},
false,
['verify'],
);
}
return window.crypto.subtle.verify(
{name: 'ECDSA', hash: 'SHA-256'},
this.key,
signature,
toBeValidated,
);
}
}
module.exports = {
SignatureValidator,
};