@statsig/sha256 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/package.json +11 -0
- package/src/__tests__/Base64.d.ts +4 -0
- package/src/__tests__/Base64.js +53 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +5 -0
- package/src/sha256.d.ts +28 -0
- package/src/sha256.js +267 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Base64 = void 0;
|
|
4
|
+
// Encoding logic from https://stackoverflow.com/a/246813/1524355, with slight modifications to make it work for binary strings
|
|
5
|
+
var KEY_STR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
6
|
+
var Base64 = /** @class */ (function () {
|
|
7
|
+
function Base64() {
|
|
8
|
+
}
|
|
9
|
+
Base64.encodeArrayBuffer = function (buffer) {
|
|
10
|
+
var binary = '';
|
|
11
|
+
var bytes = new Uint8Array(buffer);
|
|
12
|
+
var len = bytes.byteLength;
|
|
13
|
+
for (var i = 0; i < len; i++) {
|
|
14
|
+
binary += String.fromCharCode(bytes[i]);
|
|
15
|
+
}
|
|
16
|
+
return this._encodeBinary(binary);
|
|
17
|
+
};
|
|
18
|
+
Base64._encodeBinary = function (value) {
|
|
19
|
+
var output = '';
|
|
20
|
+
var chr1;
|
|
21
|
+
var chr2;
|
|
22
|
+
var chr3;
|
|
23
|
+
var enc1;
|
|
24
|
+
var enc2;
|
|
25
|
+
var enc3;
|
|
26
|
+
var enc4;
|
|
27
|
+
var i = 0;
|
|
28
|
+
while (i < value.length) {
|
|
29
|
+
chr1 = value.charCodeAt(i++);
|
|
30
|
+
chr2 = value.charCodeAt(i++);
|
|
31
|
+
chr3 = value.charCodeAt(i++);
|
|
32
|
+
enc1 = chr1 >> 2;
|
|
33
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
34
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
35
|
+
enc4 = chr3 & 63;
|
|
36
|
+
if (isNaN(chr2)) {
|
|
37
|
+
enc3 = enc4 = 64;
|
|
38
|
+
}
|
|
39
|
+
else if (isNaN(chr3)) {
|
|
40
|
+
enc4 = 64;
|
|
41
|
+
}
|
|
42
|
+
output =
|
|
43
|
+
output +
|
|
44
|
+
KEY_STR.charAt(enc1) +
|
|
45
|
+
KEY_STR.charAt(enc2) +
|
|
46
|
+
KEY_STR.charAt(enc3) +
|
|
47
|
+
KEY_STR.charAt(enc4);
|
|
48
|
+
}
|
|
49
|
+
return output;
|
|
50
|
+
};
|
|
51
|
+
return Base64;
|
|
52
|
+
}());
|
|
53
|
+
exports.Base64 = Base64;
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/sha256.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare function SHA256(input: string): Sha256;
|
|
2
|
+
declare class Sha256 {
|
|
3
|
+
blocks: number[];
|
|
4
|
+
h0: number;
|
|
5
|
+
h1: number;
|
|
6
|
+
h2: number;
|
|
7
|
+
h3: number;
|
|
8
|
+
h4: number;
|
|
9
|
+
h5: number;
|
|
10
|
+
h6: number;
|
|
11
|
+
h7: number;
|
|
12
|
+
block: number;
|
|
13
|
+
start: number;
|
|
14
|
+
bytes: number;
|
|
15
|
+
hBytes: number;
|
|
16
|
+
finalized: boolean;
|
|
17
|
+
hashed: boolean;
|
|
18
|
+
first: boolean;
|
|
19
|
+
lastByteIndex: number;
|
|
20
|
+
constructor();
|
|
21
|
+
update(message: string): Sha256;
|
|
22
|
+
finalize(): void;
|
|
23
|
+
hash(): void;
|
|
24
|
+
arrayBuffer(): ArrayBuffer;
|
|
25
|
+
dataView(): DataView;
|
|
26
|
+
private _getOutputs;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
package/src/sha256.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SHA256 = void 0;
|
|
4
|
+
function SHA256(input) {
|
|
5
|
+
return new Sha256().update(input);
|
|
6
|
+
}
|
|
7
|
+
exports.SHA256 = SHA256;
|
|
8
|
+
var EXTRA = [-2147483648, 8388608, 32768, 128];
|
|
9
|
+
var SHIFT = [24, 16, 8, 0];
|
|
10
|
+
var K = [
|
|
11
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
12
|
+
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
13
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
14
|
+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
15
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
16
|
+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
17
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
18
|
+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
19
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
20
|
+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
21
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
22
|
+
];
|
|
23
|
+
var Sha256 = /** @class */ (function () {
|
|
24
|
+
function Sha256() {
|
|
25
|
+
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
26
|
+
this.h0 = 0x6a09e667;
|
|
27
|
+
this.h1 = 0xbb67ae85;
|
|
28
|
+
this.h2 = 0x3c6ef372;
|
|
29
|
+
this.h3 = 0xa54ff53a;
|
|
30
|
+
this.h4 = 0x510e527f;
|
|
31
|
+
this.h5 = 0x9b05688c;
|
|
32
|
+
this.h6 = 0x1f83d9ab;
|
|
33
|
+
this.h7 = 0x5be0cd19;
|
|
34
|
+
this.block = this.start = this.bytes = this.hBytes = 0;
|
|
35
|
+
this.finalized = this.hashed = false;
|
|
36
|
+
this.first = true;
|
|
37
|
+
this.lastByteIndex = -1;
|
|
38
|
+
}
|
|
39
|
+
Sha256.prototype.update = function (message) {
|
|
40
|
+
if (this.finalized) {
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
if (typeof message !== 'string') {
|
|
44
|
+
throw new Error('Must be of type "string"');
|
|
45
|
+
}
|
|
46
|
+
var code;
|
|
47
|
+
var index = 0;
|
|
48
|
+
var i;
|
|
49
|
+
var length = message.length, blocks = this.blocks;
|
|
50
|
+
while (index < length) {
|
|
51
|
+
if (this.hashed) {
|
|
52
|
+
this.hashed = false;
|
|
53
|
+
blocks[0] = this.block;
|
|
54
|
+
blocks[16] =
|
|
55
|
+
blocks[1] =
|
|
56
|
+
blocks[2] =
|
|
57
|
+
blocks[3] =
|
|
58
|
+
blocks[4] =
|
|
59
|
+
blocks[5] =
|
|
60
|
+
blocks[6] =
|
|
61
|
+
blocks[7] =
|
|
62
|
+
blocks[8] =
|
|
63
|
+
blocks[9] =
|
|
64
|
+
blocks[10] =
|
|
65
|
+
blocks[11] =
|
|
66
|
+
blocks[12] =
|
|
67
|
+
blocks[13] =
|
|
68
|
+
blocks[14] =
|
|
69
|
+
blocks[15] =
|
|
70
|
+
0;
|
|
71
|
+
}
|
|
72
|
+
for (i = this.start; index < length && i < 64; ++index) {
|
|
73
|
+
code = message.charCodeAt(index);
|
|
74
|
+
if (code < 0x80) {
|
|
75
|
+
blocks[i >> 2] |= code << SHIFT[i++ & 3];
|
|
76
|
+
}
|
|
77
|
+
else if (code < 0x800) {
|
|
78
|
+
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
|
|
79
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
80
|
+
}
|
|
81
|
+
else if (code < 0xd800 || code >= 0xe000) {
|
|
82
|
+
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
|
|
83
|
+
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
|
84
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
code =
|
|
88
|
+
0x10000 +
|
|
89
|
+
(((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
|
|
90
|
+
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
|
|
91
|
+
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
|
|
92
|
+
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
|
93
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
this.lastByteIndex = i;
|
|
97
|
+
this.bytes += i - this.start;
|
|
98
|
+
if (i >= 64) {
|
|
99
|
+
this.block = blocks[16];
|
|
100
|
+
this.start = i - 64;
|
|
101
|
+
this.hash();
|
|
102
|
+
this.hashed = true;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
this.start = i;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (this.bytes > 4294967295) {
|
|
109
|
+
this.hBytes += (this.bytes / 4294967296) << 0;
|
|
110
|
+
this.bytes = this.bytes % 4294967296;
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
};
|
|
114
|
+
Sha256.prototype.finalize = function () {
|
|
115
|
+
if (this.finalized) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.finalized = true;
|
|
119
|
+
var blocks = this.blocks, i = this.lastByteIndex;
|
|
120
|
+
blocks[16] = this.block;
|
|
121
|
+
blocks[i >> 2] |= EXTRA[i & 3];
|
|
122
|
+
this.block = blocks[16];
|
|
123
|
+
if (i >= 56) {
|
|
124
|
+
if (!this.hashed) {
|
|
125
|
+
this.hash();
|
|
126
|
+
}
|
|
127
|
+
blocks[0] = this.block;
|
|
128
|
+
blocks[16] =
|
|
129
|
+
blocks[1] =
|
|
130
|
+
blocks[2] =
|
|
131
|
+
blocks[3] =
|
|
132
|
+
blocks[4] =
|
|
133
|
+
blocks[5] =
|
|
134
|
+
blocks[6] =
|
|
135
|
+
blocks[7] =
|
|
136
|
+
blocks[8] =
|
|
137
|
+
blocks[9] =
|
|
138
|
+
blocks[10] =
|
|
139
|
+
blocks[11] =
|
|
140
|
+
blocks[12] =
|
|
141
|
+
blocks[13] =
|
|
142
|
+
blocks[14] =
|
|
143
|
+
blocks[15] =
|
|
144
|
+
0;
|
|
145
|
+
}
|
|
146
|
+
blocks[14] = (this.hBytes << 3) | (this.bytes >>> 29);
|
|
147
|
+
blocks[15] = this.bytes << 3;
|
|
148
|
+
this.hash();
|
|
149
|
+
};
|
|
150
|
+
Sha256.prototype.hash = function () {
|
|
151
|
+
var blocks = this.blocks;
|
|
152
|
+
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6, h = this.h7, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
|
|
153
|
+
for (j = 16; j < 64; ++j) {
|
|
154
|
+
// rightrotate
|
|
155
|
+
t1 = blocks[j - 15];
|
|
156
|
+
s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
|
|
157
|
+
t1 = blocks[j - 2];
|
|
158
|
+
s1 =
|
|
159
|
+
((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
|
|
160
|
+
blocks[j] = (blocks[j - 16] + s0 + blocks[j - 7] + s1) << 0;
|
|
161
|
+
}
|
|
162
|
+
bc = b & c;
|
|
163
|
+
for (j = 0; j < 64; j += 4) {
|
|
164
|
+
if (this.first) {
|
|
165
|
+
ab = 704751109;
|
|
166
|
+
t1 = blocks[0] - 210244248;
|
|
167
|
+
h = (t1 - 1521486534) << 0;
|
|
168
|
+
d = (t1 + 143694565) << 0;
|
|
169
|
+
this.first = false;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
s0 =
|
|
173
|
+
((a >>> 2) | (a << 30)) ^
|
|
174
|
+
((a >>> 13) | (a << 19)) ^
|
|
175
|
+
((a >>> 22) | (a << 10));
|
|
176
|
+
s1 =
|
|
177
|
+
((e >>> 6) | (e << 26)) ^
|
|
178
|
+
((e >>> 11) | (e << 21)) ^
|
|
179
|
+
((e >>> 25) | (e << 7));
|
|
180
|
+
ab = a & b;
|
|
181
|
+
maj = ab ^ (a & c) ^ bc;
|
|
182
|
+
ch = (e & f) ^ (~e & g);
|
|
183
|
+
t1 = h + s1 + ch + K[j] + blocks[j];
|
|
184
|
+
t2 = s0 + maj;
|
|
185
|
+
h = (d + t1) << 0;
|
|
186
|
+
d = (t1 + t2) << 0;
|
|
187
|
+
}
|
|
188
|
+
s0 =
|
|
189
|
+
((d >>> 2) | (d << 30)) ^
|
|
190
|
+
((d >>> 13) | (d << 19)) ^
|
|
191
|
+
((d >>> 22) | (d << 10));
|
|
192
|
+
s1 =
|
|
193
|
+
((h >>> 6) | (h << 26)) ^
|
|
194
|
+
((h >>> 11) | (h << 21)) ^
|
|
195
|
+
((h >>> 25) | (h << 7));
|
|
196
|
+
da = d & a;
|
|
197
|
+
maj = da ^ (d & b) ^ ab;
|
|
198
|
+
ch = (h & e) ^ (~h & f);
|
|
199
|
+
t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
|
|
200
|
+
t2 = s0 + maj;
|
|
201
|
+
g = (c + t1) << 0;
|
|
202
|
+
c = (t1 + t2) << 0;
|
|
203
|
+
s0 =
|
|
204
|
+
((c >>> 2) | (c << 30)) ^
|
|
205
|
+
((c >>> 13) | (c << 19)) ^
|
|
206
|
+
((c >>> 22) | (c << 10));
|
|
207
|
+
s1 =
|
|
208
|
+
((g >>> 6) | (g << 26)) ^
|
|
209
|
+
((g >>> 11) | (g << 21)) ^
|
|
210
|
+
((g >>> 25) | (g << 7));
|
|
211
|
+
cd = c & d;
|
|
212
|
+
maj = cd ^ (c & a) ^ da;
|
|
213
|
+
ch = (g & h) ^ (~g & e);
|
|
214
|
+
t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
|
|
215
|
+
t2 = s0 + maj;
|
|
216
|
+
f = (b + t1) << 0;
|
|
217
|
+
b = (t1 + t2) << 0;
|
|
218
|
+
s0 =
|
|
219
|
+
((b >>> 2) | (b << 30)) ^
|
|
220
|
+
((b >>> 13) | (b << 19)) ^
|
|
221
|
+
((b >>> 22) | (b << 10));
|
|
222
|
+
s1 =
|
|
223
|
+
((f >>> 6) | (f << 26)) ^
|
|
224
|
+
((f >>> 11) | (f << 21)) ^
|
|
225
|
+
((f >>> 25) | (f << 7));
|
|
226
|
+
bc = b & c;
|
|
227
|
+
maj = bc ^ (b & d) ^ cd;
|
|
228
|
+
ch = (f & g) ^ (~f & h);
|
|
229
|
+
t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
|
|
230
|
+
t2 = s0 + maj;
|
|
231
|
+
e = (a + t1) << 0;
|
|
232
|
+
a = (t1 + t2) << 0;
|
|
233
|
+
(function (_a) {
|
|
234
|
+
// capture a to prevent hashing bug
|
|
235
|
+
})(a);
|
|
236
|
+
}
|
|
237
|
+
this.h0 = (this.h0 + a) << 0;
|
|
238
|
+
this.h1 = (this.h1 + b) << 0;
|
|
239
|
+
this.h2 = (this.h2 + c) << 0;
|
|
240
|
+
this.h3 = (this.h3 + d) << 0;
|
|
241
|
+
this.h4 = (this.h4 + e) << 0;
|
|
242
|
+
this.h5 = (this.h5 + f) << 0;
|
|
243
|
+
this.h6 = (this.h6 + g) << 0;
|
|
244
|
+
this.h7 = (this.h7 + h) << 0;
|
|
245
|
+
};
|
|
246
|
+
Sha256.prototype.arrayBuffer = function () {
|
|
247
|
+
return this._getOutputs().buffer;
|
|
248
|
+
};
|
|
249
|
+
Sha256.prototype.dataView = function () {
|
|
250
|
+
return this._getOutputs().dataView;
|
|
251
|
+
};
|
|
252
|
+
Sha256.prototype._getOutputs = function () {
|
|
253
|
+
this.finalize();
|
|
254
|
+
var buffer = new ArrayBuffer(32);
|
|
255
|
+
var dataView = new DataView(buffer);
|
|
256
|
+
dataView.setUint32(0, this.h0);
|
|
257
|
+
dataView.setUint32(4, this.h1);
|
|
258
|
+
dataView.setUint32(8, this.h2);
|
|
259
|
+
dataView.setUint32(12, this.h3);
|
|
260
|
+
dataView.setUint32(16, this.h4);
|
|
261
|
+
dataView.setUint32(20, this.h5);
|
|
262
|
+
dataView.setUint32(24, this.h6);
|
|
263
|
+
dataView.setUint32(28, this.h7);
|
|
264
|
+
return { dataView: dataView, buffer: buffer };
|
|
265
|
+
};
|
|
266
|
+
return Sha256;
|
|
267
|
+
}());
|