@statsig/sha256 0.0.1-beta.8 → 1.0.0
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 +0 -3
- package/package.json +1 -1
- package/src/__tests__/Base64.d.ts +1 -1
- package/src/__tests__/Base64.js +20 -23
- package/src/index.js +1 -1
- package/src/sha256.js +28 -29
package/README.md
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
# Statsig - SHA256
|
|
2
2
|
|
|
3
|
-
> [!IMPORTANT]
|
|
4
|
-
> This version of the SDK is still in beta. The non-beta version can be found [here](https://github.com/statsig-io/js-client).
|
|
5
|
-
|
|
6
3
|
A SHA 256 implementation used in Statsig Javascript packages.
|
|
7
4
|
|
|
8
5
|
Learn more by visiting: https://docs.statsig.com/client/javascript-sdk
|
package/package.json
CHANGED
package/src/__tests__/Base64.js
CHANGED
|
@@ -2,29 +2,27 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Base64 = void 0;
|
|
4
4
|
// Encoding logic from https://stackoverflow.com/a/246813/1524355, with slight modifications to make it work for binary strings
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var len = bytes.byteLength;
|
|
13
|
-
for (var i = 0; i < len; i++) {
|
|
5
|
+
const KEY_STR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
6
|
+
class Base64 {
|
|
7
|
+
static encodeArrayBuffer(buffer) {
|
|
8
|
+
let binary = '';
|
|
9
|
+
const bytes = new Uint8Array(buffer);
|
|
10
|
+
const len = bytes.byteLength;
|
|
11
|
+
for (let i = 0; i < len; i++) {
|
|
14
12
|
binary += String.fromCharCode(bytes[i]);
|
|
15
13
|
}
|
|
16
14
|
return this._encodeBinary(binary);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
}
|
|
16
|
+
static _encodeBinary(value) {
|
|
17
|
+
let output = '';
|
|
18
|
+
let chr1;
|
|
19
|
+
let chr2;
|
|
20
|
+
let chr3;
|
|
21
|
+
let enc1;
|
|
22
|
+
let enc2;
|
|
23
|
+
let enc3;
|
|
24
|
+
let enc4;
|
|
25
|
+
let i = 0;
|
|
28
26
|
while (i < value.length) {
|
|
29
27
|
chr1 = value.charCodeAt(i++);
|
|
30
28
|
chr2 = value.charCodeAt(i++);
|
|
@@ -47,7 +45,6 @@ var Base64 = /** @class */ (function () {
|
|
|
47
45
|
KEY_STR.charAt(enc4);
|
|
48
46
|
}
|
|
49
47
|
return output;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
}());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
53
50
|
exports.Base64 = Base64;
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SHA256 = void 0;
|
|
4
|
-
|
|
4
|
+
const sha256_1 = require("./sha256");
|
|
5
5
|
Object.defineProperty(exports, "SHA256", { enumerable: true, get: function () { return sha256_1.SHA256; } });
|
package/src/sha256.js
CHANGED
|
@@ -5,9 +5,9 @@ function SHA256(input) {
|
|
|
5
5
|
return new Sha256().update(input);
|
|
6
6
|
}
|
|
7
7
|
exports.SHA256 = SHA256;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const EXTRA = [-2147483648, 8388608, 32768, 128];
|
|
9
|
+
const SHIFT = [24, 16, 8, 0];
|
|
10
|
+
const K = [
|
|
11
11
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
12
12
|
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
13
13
|
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
@@ -20,8 +20,8 @@ var K = [
|
|
|
20
20
|
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
21
21
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
22
22
|
];
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
class Sha256 {
|
|
24
|
+
constructor() {
|
|
25
25
|
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
26
26
|
this.h0 = 0x6a09e667;
|
|
27
27
|
this.h1 = 0xbb67ae85;
|
|
@@ -36,17 +36,17 @@ var Sha256 = /** @class */ (function () {
|
|
|
36
36
|
this.first = true;
|
|
37
37
|
this.lastByteIndex = -1;
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
update(message) {
|
|
40
40
|
if (this.finalized) {
|
|
41
41
|
return this;
|
|
42
42
|
}
|
|
43
43
|
if (typeof message !== 'string') {
|
|
44
44
|
throw new Error('Must be of type "string"');
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
let code;
|
|
47
|
+
let index = 0;
|
|
48
|
+
let i;
|
|
49
|
+
const length = message.length, blocks = this.blocks;
|
|
50
50
|
while (index < length) {
|
|
51
51
|
if (this.hashed) {
|
|
52
52
|
this.hashed = false;
|
|
@@ -110,13 +110,13 @@ var Sha256 = /** @class */ (function () {
|
|
|
110
110
|
this.bytes = this.bytes % 4294967296;
|
|
111
111
|
}
|
|
112
112
|
return this;
|
|
113
|
-
}
|
|
114
|
-
|
|
113
|
+
}
|
|
114
|
+
finalize() {
|
|
115
115
|
if (this.finalized) {
|
|
116
116
|
return;
|
|
117
117
|
}
|
|
118
118
|
this.finalized = true;
|
|
119
|
-
|
|
119
|
+
const blocks = this.blocks, i = this.lastByteIndex;
|
|
120
120
|
blocks[16] = this.block;
|
|
121
121
|
blocks[i >> 2] |= EXTRA[i & 3];
|
|
122
122
|
this.block = blocks[16];
|
|
@@ -146,10 +146,10 @@ var Sha256 = /** @class */ (function () {
|
|
|
146
146
|
blocks[14] = (this.hBytes << 3) | (this.bytes >>> 29);
|
|
147
147
|
blocks[15] = this.bytes << 3;
|
|
148
148
|
this.hash();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
149
|
+
}
|
|
150
|
+
hash() {
|
|
151
|
+
const blocks = this.blocks;
|
|
152
|
+
let 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
153
|
for (j = 16; j < 64; ++j) {
|
|
154
154
|
// rightrotate
|
|
155
155
|
t1 = blocks[j - 15];
|
|
@@ -242,17 +242,17 @@ var Sha256 = /** @class */ (function () {
|
|
|
242
242
|
this.h5 = (this.h5 + f) << 0;
|
|
243
243
|
this.h6 = (this.h6 + g) << 0;
|
|
244
244
|
this.h7 = (this.h7 + h) << 0;
|
|
245
|
-
}
|
|
246
|
-
|
|
245
|
+
}
|
|
246
|
+
arrayBuffer() {
|
|
247
247
|
return this._getOutputs().buffer;
|
|
248
|
-
}
|
|
249
|
-
|
|
248
|
+
}
|
|
249
|
+
dataView() {
|
|
250
250
|
return this._getOutputs().dataView;
|
|
251
|
-
}
|
|
252
|
-
|
|
251
|
+
}
|
|
252
|
+
_getOutputs() {
|
|
253
253
|
this.finalize();
|
|
254
|
-
|
|
255
|
-
|
|
254
|
+
const buffer = new ArrayBuffer(32);
|
|
255
|
+
const dataView = new DataView(buffer);
|
|
256
256
|
dataView.setUint32(0, this.h0);
|
|
257
257
|
dataView.setUint32(4, this.h1);
|
|
258
258
|
dataView.setUint32(8, this.h2);
|
|
@@ -261,7 +261,6 @@ var Sha256 = /** @class */ (function () {
|
|
|
261
261
|
dataView.setUint32(20, this.h5);
|
|
262
262
|
dataView.setUint32(24, this.h6);
|
|
263
263
|
dataView.setUint32(28, this.h7);
|
|
264
|
-
return { dataView
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
}());
|
|
264
|
+
return { dataView, buffer };
|
|
265
|
+
}
|
|
266
|
+
}
|