-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignature.js
40 lines (32 loc) · 942 Bytes
/
signature.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
var Signature = function(r, s) {
this.r = r;
this.s = s;
this.toDer = function() {
return Der.encodeSequence(Der.encodeInteger(this.r), Der.encodeInteger(this.s));
}
this.toBase64 = function() {
return Base64.encode(this.toDer());
}
}
Signature.fromDer = function(string) {
let result = Der.removeSequence(string);
let rs = result[0];
let empty = result[1];
if (empty) {
throw new Error("trailing junk after DER signature: " + BinaryAscii.hexFromBinary(empty));
}
result = Der.removeInteger(rs);
let r = result[0];
let rest = result[1];
result = Der.removeInteger(rest);
let s = result[0];
empty = result[1];
if (empty) {
throw new Error("trailing junk after DER numbers: " + BinaryAscii.hexFromBinary(empty));
}
return new Signature(r, s)
}
Signature.fromBase64 = function(string) {
let derString = Base64.decode(string);
return Signature.fromDer(derString);
}