Skip to content

Commit cb0f793

Browse files
committed
Wrote my own SHA-256 to avoid crypto in bundle
1 parent c1e507e commit cb0f793

File tree

15 files changed

+216
-72
lines changed

15 files changed

+216
-72
lines changed

compiled/download.js

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload-download.js

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload.js

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/sha-256.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const _default: (input: ArrayBuffer) => ArrayBuffer;
2+
export default _default;

dist/lib/sha-256.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const assert_1 = require("./assert");
4+
const K = new Uint32Array([
5+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
6+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
7+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
8+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
9+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
10+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
11+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
12+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
13+
]);
14+
function rightRotate(value, bits) {
15+
return (value >>> bits) | (value << (32 - bits));
16+
}
17+
function pos(value) {
18+
return new Uint32Array([value])[0];
19+
}
20+
exports.default = (input) => {
21+
const lBytes = input.byteLength;
22+
const l = lBytes * 8; //not using bitwise math in case this overflows a 32-bit integer
23+
assert_1.default(l === pos(l | 0), 'Bit length does not fit in a 32-bit integer');
24+
const extraBytes = 64 - ((lBytes + 72) & 63);
25+
const messageLength = lBytes + extraBytes + 8;
26+
const message = new ArrayBuffer(messageLength);
27+
const castMessage = new Uint8Array(message);
28+
castMessage.set(new Uint8Array(input));
29+
castMessage[lBytes] = 128;
30+
new DataView(message).setUint32(messageLength - 4, l);
31+
const h = new Uint32Array([
32+
0x6a09e667,
33+
0xbb67ae85,
34+
0x3c6ef372,
35+
0xa54ff53a,
36+
0x510e527f,
37+
0x9b05688c,
38+
0x1f83d9ab,
39+
0x5be0cd19
40+
]);
41+
for (let chunkStart = 0; chunkStart < messageLength; chunkStart += 64) {
42+
const w = new Uint32Array(64);
43+
const chunkDataView = new DataView(message, chunkStart, 64);
44+
for (let i = 0; i < 16; i++)
45+
w[i] = chunkDataView.getUint32(i << 2);
46+
for (let i = 16; i < 64; i++) {
47+
const wMinus15 = w[i - 15];
48+
const s0 = rightRotate(wMinus15, 7) ^ rightRotate(wMinus15, 18) ^ (wMinus15 >>> 3);
49+
const wMinus2 = w[i - 2];
50+
const s1 = rightRotate(wMinus2, 17) ^ rightRotate(wMinus2, 19) ^ (wMinus2 >>> 10);
51+
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
52+
}
53+
const hIncrement = h.slice();
54+
for (let i = 0; i < 64; i++) {
55+
/*tslint:disable:indent*/
56+
const a = hIncrement[0], b = hIncrement[1], c = hIncrement[2], e = hIncrement[4], f = hIncrement[5], g = hIncrement[6];
57+
/*tslint:enable:indent*/
58+
const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
59+
const ch = (e & f) ^ (~e & g);
60+
const temp1 = hIncrement[7] + S1 + ch + K[i] + w[i];
61+
const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
62+
const maj = (a & b) ^ (a & c) ^ (b & c);
63+
const temp2 = S0 + maj;
64+
hIncrement[7] = g;
65+
hIncrement[6] = f;
66+
hIncrement[5] = e;
67+
hIncrement[4] = hIncrement[3] + temp1;
68+
hIncrement[3] = c;
69+
hIncrement[2] = b;
70+
hIncrement[1] = a;
71+
hIncrement[0] = temp1 + temp2;
72+
}
73+
for (let i = 0; i < 8; i++)
74+
h[i] += hIncrement[i];
75+
}
76+
const result = new ArrayBuffer(32);
77+
const resultDataView = new DataView(result);
78+
for (let i = 0; i < 8; i++)
79+
resultDataView.setUint32(i << 2, h[i]);
80+
return result;
81+
};

dist/types/abstract.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const base64 = require("base64-js");
4-
const js_sha256_1 = require("js-sha256");
4+
const sha_256_1 = require("../lib/sha-256");
55
const config_1 = require("../config");
66
const assert_1 = require("../lib/assert");
77
const constants_1 = require("../lib/constants");
@@ -93,9 +93,7 @@ class AbstractType {
9393
* @return A hash of the buffer given by [[toBuffer]]
9494
*/
9595
_getHash() {
96-
const hash = js_sha256_1.sha256.create();
97-
hash.update(this.toBuffer());
98-
const bytes = new Uint8Array(hash.arrayBuffer());
96+
const bytes = new Uint8Array(sha_256_1.default(this.toBuffer()));
9997
return base64.fromByteArray(bytes);
10098
}
10199
/**

dist/typings/js-sha256.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

dist/typings/js-sha256.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/sha-256.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import assert from './assert'
2+
3+
const K = new Uint32Array([
4+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
5+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
6+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
7+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
8+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
10+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
11+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
12+
])
13+
14+
function rightRotate(value: number, bits: number): number {
15+
return (value >>> bits) | (value << (32 - bits))
16+
}
17+
function pos(value: number): number {
18+
return new Uint32Array([value])[0]
19+
}
20+
21+
export default (input: ArrayBuffer): ArrayBuffer => {
22+
const lBytes = input.byteLength
23+
const l = lBytes * 8 //not using bitwise math in case this overflows a 32-bit integer
24+
assert(l === pos(l | 0), 'Bit length does not fit in a 32-bit integer')
25+
const extraBytes = 64 - ((lBytes + 72) & 63)
26+
const messageLength = lBytes + extraBytes + 8
27+
const message = new ArrayBuffer(messageLength)
28+
const castMessage = new Uint8Array(message)
29+
castMessage.set(new Uint8Array(input))
30+
castMessage[lBytes] = 128
31+
new DataView(message).setUint32(messageLength - 4, l)
32+
33+
const h = new Uint32Array([
34+
0x6a09e667,
35+
0xbb67ae85,
36+
0x3c6ef372,
37+
0xa54ff53a,
38+
0x510e527f,
39+
0x9b05688c,
40+
0x1f83d9ab,
41+
0x5be0cd19
42+
])
43+
for (let chunkStart = 0; chunkStart < messageLength; chunkStart += 64) {
44+
const w = new Uint32Array(64)
45+
const chunkDataView = new DataView(message, chunkStart, 64)
46+
for (let i = 0; i < 16; i++) w[i] = chunkDataView.getUint32(i << 2)
47+
for (let i = 16; i < 64; i++) {
48+
const wMinus15 = w[i - 15]
49+
const s0 = rightRotate(wMinus15, 7) ^ rightRotate(wMinus15, 18) ^ (wMinus15 >>> 3)
50+
const wMinus2 = w[i - 2]
51+
const s1 = rightRotate(wMinus2, 17) ^ rightRotate(wMinus2, 19) ^ (wMinus2 >>> 10)
52+
w[i] = w[i - 16] + s0 + w[i - 7] + s1
53+
}
54+
const hIncrement = h.slice()
55+
for (let i = 0; i < 64; i++) {
56+
/*tslint:disable:indent*/
57+
const a = hIncrement[0],
58+
b = hIncrement[1],
59+
c = hIncrement[2],
60+
e = hIncrement[4],
61+
f = hIncrement[5],
62+
g = hIncrement[6]
63+
/*tslint:enable:indent*/
64+
const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)
65+
const ch = (e & f) ^ (~e & g)
66+
const temp1 = hIncrement[7] + S1 + ch + K[i] + w[i]
67+
const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)
68+
const maj = (a & b) ^ (a & c) ^ (b & c)
69+
const temp2 = S0 + maj
70+
hIncrement[7] = g
71+
hIncrement[6] = f
72+
hIncrement[5] = e
73+
hIncrement[4] = hIncrement[3] + temp1
74+
hIncrement[3] = c
75+
hIncrement[2] = b
76+
hIncrement[1] = a
77+
hIncrement[0] = temp1 + temp2
78+
}
79+
for (let i = 0; i < 8; i++) h[i] += hIncrement[i]
80+
}
81+
const result = new ArrayBuffer(32)
82+
const resultDataView = new DataView(result)
83+
for (let i = 0; i < 8; i++) resultDataView.setUint32(i << 2, h[i])
84+
return result
85+
}

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)