-
Notifications
You must be signed in to change notification settings - Fork 8
/
encrypt.js
236 lines (231 loc) · 8.13 KB
/
encrypt.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var e = {}
function t(e, i, t) {
this.e = m(e), this.d = m(i), this.m = m(t), this.chunkSize = 2 * N(this.m), this.radix = 16, this.barrett = new o(this.m)
}
function n(e, i) {
for (var t = new Array, n = i.length, o = 0; n > o;) t[o] = i.charCodeAt(o), o++;
for (; t.length % e.chunkSize != 0;) t[o++] = 0;
var s, r, a, l = t.length,
c = "";
for (o = 0; l > o; o += e.chunkSize) {
for (a = new d, s = 0, r = o; r < o + e.chunkSize; ++s) a.digits[s] = t[r++], a.digits[s] += t[r++] << 8;
var u = e.barrett.powMod(a, e.e),
g = 16 == e.radix ? p(u) : f(u, e.radix);
c += g + " "
}
return c.substring(0, c.length - 1)
}
function o(e) {
this.modulus = c(e), this.k = N(this.modulus) + 1;
var i = new d;
i.digits[2 * this.k] = 1, this.mu = S(i, this.modulus), this.bkplus1 = new d, this.bkplus1.digits[this.k + 1] = 1, this.modulo = s, this.multiplyMod = r, this.powMod = a
}
function s(e) {
var i = X(e, this.k - 1),
t = C(i, this.mu),
n = X(t, this.k + 1),
o = D(e, this.k + 1),
s = C(n, this.modulus),
r = D(s, this.k + 1),
a = b(o, r);
a.isNeg && (a = w(a, this.bkplus1));
for (var l = L(a, this.modulus) >= 0; l;) a = b(a, this.modulus), l = L(a, this.modulus) >= 0;
return a
}
function r(e, i) {
var t = C(e, i);
return this.modulo(t)
}
function a(e, i) {
var t = new d;
t.digits[0] = 1;
for (var n = e, o = i;;) {
if (0 != (1 & o.digits[0]) && (t = this.multiplyMod(t, n)), o = E(o, 1), 0 == o.digits[0] && 0 == N(o)) break;
n = this.multiplyMod(n, n)
}
return t
}
function l(e) {
B = e, z = new Array(B);
for (var i = 0; i < z.length; i++) z[i] = 0;
O = new d, I = new d, I.digits[0] = 1
}
function d(e) {
"boolean" == typeof e && 1 == e ? this.digits = null : this.digits = z.slice(0), this.isNeg = !1
}
function c(e) {
var i = new d(!0);
return i.digits = e.digits.slice(0), i.isNeg = e.isNeg, i
}
function u(e) {
var i = new d;
i.isNeg = 0 > e, e = Math.abs(e);
for (var t = 0; e > 0;) i.digits[t++] = e & H, e >>= P;
return i
}
function g(e) {
for (var i = "", t = e.length - 1; t > -1; --t) i += e.charAt(t);
return i
}
function f(e, i) {
var t = new d;
t.digits[0] = i;
for (var n = A(e, t), o = Q[n[1].digits[0]]; 1 == L(n[0], O);) n = A(n[0], t), digit = n[1].digits[0], o += Q[n[1].digits[0]];
return (e.isNeg ? "-" : "") + g(o)
}
function v(e) {
var t = 15,
n = "";
for (i = 0; i < 4; ++i) n += Z[e & t], e >>>= 4;
return g(n)
}
function p(e) {
for (var i = "", t = (N(e), N(e)); t > -1; --t) i += v(e.digits[t]);
return i
}
function h(e) {
var i, t = 48,
n = t + 9,
o = 97,
s = o + 25,
r = 65,
a = 90;
return i = e >= t && n >= e ? e - t : e >= r && a >= e ? 10 + e - r : e >= o && s >= e ? 10 + e - o : 0
}
function y(e) {
for (var i = 0, t = Math.min(e.length, 4), n = 0; t > n; ++n) i <<= 4, i |= h(e.charCodeAt(n));
return i
}
function m(e) {
for (var i = new d, t = e.length, n = t, o = 0; n > 0; n -= 4, ++o) i.digits[o] = y(e.substr(Math.max(n - 4, 0), Math.min(n, 4)));
return i
}
function w(e, i) {
var t;
if (e.isNeg != i.isNeg) i.isNeg = !i.isNeg, t = b(e, i), i.isNeg = !i.isNeg;
else {
t = new d;
for (var n, o = 0, s = 0; s < e.digits.length; ++s) n = e.digits[s] + i.digits[s] + o, t.digits[s] = 65535 & n, o = Number(n >= K);
t.isNeg = e.isNeg
}
return t
}
function b(e, i) {
var t;
if (e.isNeg != i.isNeg) i.isNeg = !i.isNeg, t = w(e, i), i.isNeg = !i.isNeg;
else {
t = new d;
var n, o;
o = 0;
for (var s = 0; s < e.digits.length; ++s) n = e.digits[s] - i.digits[s] + o, t.digits[s] = 65535 & n, t.digits[s] < 0 && (t.digits[s] += K), o = 0 - Number(0 > n);
if (-1 == o) {
o = 0;
for (var s = 0; s < e.digits.length; ++s) n = 0 - t.digits[s] + o, t.digits[s] = 65535 & n, t.digits[s] < 0 && (t.digits[s] += K), o = 0 - Number(0 > n);
t.isNeg = !e.isNeg
} else t.isNeg = e.isNeg
}
return t
}
function N(e) {
for (var i = e.digits.length - 1; i > 0 && 0 == e.digits[i];)--i;
return i
}
function k(e) {
var i, t = N(e),
n = e.digits[t],
o = (t + 1) * q;
for (i = o; i > o - q && 0 == (32768 & n); --i) n <<= 1;
return i
}
function C(e, i) {
for (var t, n, o, s = new d, r = N(e), a = N(i), l = 0; a >= l; ++l) {
for (t = 0, o = l, j = 0; j <= r; ++j, ++o) n = s.digits[o] + e.digits[j] * i.digits[l] + t, s.digits[o] = n & H, t = n >>> P;
s.digits[l + r + 1] = t
}
return s.isNeg = e.isNeg != i.isNeg, s
}
function $(e, i) {
var t, n, o;
result = new d, t = N(e), n = 0;
for (var s = 0; t >= s; ++s) o = result.digits[s] + e.digits[s] * i + n, result.digits[s] = o & H, n = o >>> P;
return result.digits[1 + t] = n, result
}
function x(e, i, t, n, o) {
for (var s = Math.min(i + o, e.length), r = i, a = n; s > r; ++r, ++a) t[a] = e[r]
}
function F(e, i) {
var t = Math.floor(i / q),
n = new d;
x(e.digits, 0, n.digits, t, n.digits.length - t);
for (var o = i % q, s = q - o, r = n.digits.length - 1, a = r - 1; r > 0; --r, --a) n.digits[r] = n.digits[r] << o & H | (n.digits[a] & J[o]) >>> s;
return n.digits[0] = n.digits[r] << o & H, n.isNeg = e.isNeg, n
}
function E(e, i) {
var t = Math.floor(i / q),
n = new d;
x(e.digits, t, n.digits, 0, e.digits.length - t);
for (var o = i % q, s = q - o, r = 0, a = r + 1; r < n.digits.length - 1; ++r, ++a) n.digits[r] = n.digits[r] >>> o | (n.digits[a] & W[o]) << s;
return n.digits[n.digits.length - 1] >>>= o, n.isNeg = e.isNeg, n
}
function M(e, i) {
var t = new d;
return x(e.digits, 0, t.digits, i, t.digits.length - i), t
}
function X(e, i) {
var t = new d;
return x(e.digits, i, t.digits, 0, t.digits.length - i), t
}
function D(e, i) {
var t = new d;
return x(e.digits, 0, t.digits, 0, i), t
}
function L(e, i) {
if (e.isNeg != i.isNeg) return 1 - 2 * Number(e.isNeg);
for (var t = e.digits.length - 1; t >= 0; --t) if (e.digits[t] != i.digits[t]) return e.isNeg ? 1 - 2 * Number(e.digits[t] > i.digits[t]) : 1 - 2 * Number(e.digits[t] < i.digits[t]);
return 0
}
function A(e, i) {
var t, n, o = k(e),
s = k(i),
r = i.isNeg;
if (s > o) return e.isNeg ? (t = c(I), t.isNeg = !i.isNeg, e.isNeg = !1, i.isNeg = !1, n = b(i, e), e.isNeg = !0, i.isNeg = r) : (t = new d, n = c(e)), new Array(t, n);
t = new d, n = e;
for (var a = Math.ceil(s / q) - 1, l = 0; i.digits[a] < R;) i = F(i, 1), ++l, ++s, a = Math.ceil(s / q) - 1;
n = F(n, l), o += l;
for (var u = Math.ceil(o / q) - 1, g = M(i, u - a); - 1 != L(n, g);)++t.digits[u - a], n = b(n, g);
for (var f = u; f > a; --f) {
var v = f >= n.digits.length ? 0 : n.digits[f],
p = f - 1 >= n.digits.length ? 0 : n.digits[f - 1],
h = f - 2 >= n.digits.length ? 0 : n.digits[f - 2],
y = a >= i.digits.length ? 0 : i.digits[a],
m = a - 1 >= i.digits.length ? 0 : i.digits[a - 1];
v == y ? t.digits[f - a - 1] = H : t.digits[f - a - 1] = Math.floor((v * K + p) / y);
for (var C = t.digits[f - a - 1] * (y * K + m), x = v * U + (p * K + h); C > x;)--t.digits[f - a - 1], C = t.digits[f - a - 1] * (y * K | m), x = v * K * K + (p * K + h);
g = M(i, f - a - 1), n = b(n, $(g, t.digits[f - a - 1])), n.isNeg && (n = w(n, g), --t.digits[f - a - 1])
}
return n = E(n, l), t.isNeg = e.isNeg != r, e.isNeg && (t = r ? w(t, I) : b(t, I), i = E(i, l), n = b(i, n)), 0 == n.digits[0] && 0 == N(n) && (n.isNeg = !1), new Array(t, n)
}
function S(e, i) {
return A(e, i)[0]
}
var T = null;
e.getKeys = function(e, n, maxdigits) {
l(2 * maxdigits)
T = new t(e, "null", n)
}
e.encrypt = function(e) {
encrypted = n(T, e)
return encrypted
};
var B, z, O, I, P = 16,
q = P,
K = 65536,
R = K >>> 1,
U = K * K,
H = K - 1;
l(20);
var Q = (u(1e15), new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")),
Z = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"),
J = new Array(0, 32768, 49152, 57344, 61440, 63488, 64512, 65024, 65280, 65408, 65472, 65504, 65520, 65528, 65532, 65534, 65535),
W = new Array(0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535)
module.exports = e