This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oSSS.js
99 lines (87 loc) · 2.74 KB
/
oSSS.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
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
(function (thiz) {
var O = thiz.O;
O.__.SSS = {
/**
* Shamyr's Shared Secret
* (maximum quorum = 8)
* based on Secret Splitting
* http://www.rlr-uk.com/
**/
ks: O.__.Base64._keyStr + "!$",
split: function (secret, n, q) {
var ks = this.ks;
secret = O.toBase64(secret).replace(/=+$/,'');
var ret = [];
for (var i = 0; i < n; i++) {
ret[i] = ks.charAt(q) + ks.charAt(i + 1);
}
var pol = [];
for (i = 0; i < secret.length; i++) {
pol[0] = ks.indexOf(secret.charAt(i));
for (var j = 1; j < q; j++) {
pol[j] = Math.floor(O.getRandom() * ks.length);
}
var t;
for (j = 0; j < n; j++) {
t = 0;
for (var l = 0; l < q; l++) {
t += pol[l] * Math.pow(j + 1, l);
}
t = t % ks.length;
ret[j] += ks.charAt(t);
}
}
return ret;
},
join: function (keys) {
var ret = "",
ks = this.ks,
q = ks.indexOf(keys[0].charAt(0)),
Points = [],
t;
if (keys.length < q) return 0; // not quorum
for (j = 0; j < q; j++) {
Points[j] = 1.0;
for (var l = 0; l < q; l++)
if (l != j) {
Points[j] *= -ks.indexOf(keys[l].charAt(1));
t = (ks.indexOf(keys[j].charAt(1)) - ks.indexOf(keys[l].charAt(1)));
for (var m = 1; m < ks.length; m++)
if ((m * ks.length + 1) % t == 0) {
t = ((m * ks.length + 1) / t) % ks.length;
break;
}
Points[j] *= t;
}
}
for (var i = 2; i < keys[1].length; i++) {
t = 0;
for (j = 0; j < q; j++) t += (ks.indexOf(keys[j].charAt(i)) * 1.0) * Points[j];
t = t % ks.length;
if (t < 0) t += ks.length;
ret += ks.charAt(t);
}
t = ret.length % 4;
ret += t == 2 ? "==" : t == 3 ? "=" : '';
return O.isBase64(ret) ? O.fromBase64(ret) : 1;
}
};
O.expand({
SSSsplit: function(s,n,q) {
return O.__.SSS.split(s,n,q);
},
SSSjoin: function(o) {
return O.__.SSS.join(o);
}
});
O.extend({
SSSsplit: function (n,q) {
this.O = O.SSSsplit(this.O.toString(),n,q);
return this;
},
SSSjoin: function () {
this.O = O.SSSjoin(this.O);
return this;
}
});
})(this);