@swf2es/codegen 0.1.35 → 0.1.37
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/dist/codegen.d.ts +69 -3
- package/dist/codegen.js +46 -6
- package/dist/codegen.wasm +0 -0
- package/dist/index.d.ts +31 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +160 -28
- package/dist/index.js.map +1 -1
- package/dist/keys.d.ts +46 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +201 -0
- package/dist/keys.js.map +1 -0
- package/dist/sha256.d.ts +7 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/sha256.js +111 -0
- package/dist/sha256.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +197 -37
- package/src/keys.ts +296 -0
- package/src/sha256.ts +127 -0
package/src/sha256.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// SHA-256 computed here, synchronously: for module keys' chained digests
|
|
2
|
+
// (keys.ts), which a key takes as it is asked for, and for the player where
|
|
3
|
+
// crypto.subtle, which only a secure context has and only asynchronously,
|
|
4
|
+
// will not do.
|
|
5
|
+
|
|
6
|
+
const K = new Uint32Array([
|
|
7
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
8
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
9
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
10
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
11
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
12
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
13
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
14
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
const w = new Uint32Array(64);
|
|
18
|
+
|
|
19
|
+
/** The SHA-256 of `bytes`. */
|
|
20
|
+
export function sha256Here(bytes: Uint8Array): Uint8Array {
|
|
21
|
+
const block = padded(bytes.length);
|
|
22
|
+
block.set(bytes);
|
|
23
|
+
return digest(block, bytes.length);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The SHA-256 of `prefix`, ASCII, and then each of `parts`, as 64 hex digits. */
|
|
27
|
+
export function sha256Joined(prefix: string, ...parts: Uint8Array[]): string {
|
|
28
|
+
let n = prefix.length;
|
|
29
|
+
for (const part of parts) {
|
|
30
|
+
n += part.length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const block = padded(n);
|
|
34
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
35
|
+
block[i] = prefix.charCodeAt(i);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let at = prefix.length;
|
|
39
|
+
for (const part of parts) {
|
|
40
|
+
block.set(part, at);
|
|
41
|
+
at += part.length;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return hex(digest(block, n));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Room for a message of `n` bytes and its padding: 0x80, zeros to 56 mod 64, then the bit length. */
|
|
48
|
+
function padded(n: number): Uint8Array {
|
|
49
|
+
return new Uint8Array(((n + 9 + 63) >> 6) << 6);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** FIPS 180-4, one block at a time, of the `n` bytes of message at the start of `message`, padded in place. */
|
|
53
|
+
function digest(message: Uint8Array, n: number): Uint8Array {
|
|
54
|
+
const h = new Uint32Array([
|
|
55
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
56
|
+
]);
|
|
57
|
+
message[n] = 0x80;
|
|
58
|
+
const view = new DataView(message.buffer, message.byteOffset, message.byteLength);
|
|
59
|
+
view.setUint32(message.length - 8, Math.floor((n * 8) / 0x100000000));
|
|
60
|
+
view.setUint32(message.length - 4, (n * 8) >>> 0);
|
|
61
|
+
|
|
62
|
+
for (let block = 0; block < message.length; block += 64) {
|
|
63
|
+
for (let i = 0; i < 16; i++) {
|
|
64
|
+
w[i] = view.getUint32(block + i * 4);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
for (let i = 16; i < 64; i++) {
|
|
68
|
+
const x = w[i - 15];
|
|
69
|
+
const y = w[i - 2];
|
|
70
|
+
const s0 = ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
|
|
71
|
+
const s1 = ((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10);
|
|
72
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let a = h[0];
|
|
76
|
+
let b = h[1];
|
|
77
|
+
let c = h[2];
|
|
78
|
+
let d = h[3];
|
|
79
|
+
let e = h[4];
|
|
80
|
+
let f = h[5];
|
|
81
|
+
let g = h[6];
|
|
82
|
+
let hh = h[7];
|
|
83
|
+
for (let i = 0; i < 64; i++) {
|
|
84
|
+
const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
85
|
+
const ch = (e & f) ^ (~e & g);
|
|
86
|
+
const t1 = (hh + S1 + ch + K[i] + w[i]) >>> 0;
|
|
87
|
+
const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
88
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
89
|
+
const t2 = (S0 + maj) >>> 0;
|
|
90
|
+
hh = g;
|
|
91
|
+
g = f;
|
|
92
|
+
f = e;
|
|
93
|
+
e = (d + t1) >>> 0;
|
|
94
|
+
d = c;
|
|
95
|
+
c = b;
|
|
96
|
+
b = a;
|
|
97
|
+
a = (t1 + t2) >>> 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
h[0] = (h[0] + a) >>> 0;
|
|
101
|
+
h[1] = (h[1] + b) >>> 0;
|
|
102
|
+
h[2] = (h[2] + c) >>> 0;
|
|
103
|
+
h[3] = (h[3] + d) >>> 0;
|
|
104
|
+
h[4] = (h[4] + e) >>> 0;
|
|
105
|
+
h[5] = (h[5] + f) >>> 0;
|
|
106
|
+
h[6] = (h[6] + g) >>> 0;
|
|
107
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const out = new Uint8Array(32);
|
|
111
|
+
const words = new DataView(out.buffer);
|
|
112
|
+
for (let i = 0; i < 8; i++) {
|
|
113
|
+
words.setUint32(i * 4, h[i]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Bytes as lower-case hex digits, two each. */
|
|
120
|
+
export function hex(bytes: Uint8Array): string {
|
|
121
|
+
let s = "";
|
|
122
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
123
|
+
s += bytes[i].toString(16).padStart(2, "0");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return s;
|
|
127
|
+
}
|