@couch-kit/core 0.9.1 → 0.9.2
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/CHANGELOG.md +17 -0
- package/README.md +2 -2
- package/dist/index.cjs +143 -1
- package/dist/index.js +143 -1
- package/lib/constants.d.ts +5 -2
- package/lib/constants.d.ts.map +1 -1
- package/lib/sha256.d.ts +20 -0
- package/lib/sha256.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/constants.ts +12 -4
- package/src/sha256.ts +169 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @couch-kit/core
|
|
2
2
|
|
|
3
|
+
## 0.9.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#85](https://github.com/faluciano/react-native-couch-kit/pull/85) [`0064a42`](https://github.com/faluciano/react-native-couch-kit/commit/0064a42e0593d2ed2734c27926c5df686dcf3b5f) Thanks [@faluciano](https://github.com/faluciano)! - **Security:** Derive public player IDs with a real SHA-256 on React Native / Hermes.
|
|
8
|
+
|
|
9
|
+
`derivePlayerId` hashes the session secret with SHA-256 via the Web Crypto API,
|
|
10
|
+
but Hermes (where the host runs) does not expose `crypto.subtle`. The previous
|
|
11
|
+
fallback used `derivePlayerIdLegacy`, which simply truncated the secret —
|
|
12
|
+
exposing the first half of the raw session token as the public `playerId`
|
|
13
|
+
broadcast in game state.
|
|
14
|
+
|
|
15
|
+
The fallback now uses a dependency-free pure-JS SHA-256 that produces the exact
|
|
16
|
+
same digest as `crypto.subtle`, so player IDs are secure and identical across
|
|
17
|
+
all host runtimes. `derivePlayerIdLegacy` is retained only for reconnect
|
|
18
|
+
migration of players who joined under the old scheme.
|
|
19
|
+
|
|
3
20
|
## 0.9.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -190,9 +190,9 @@ try {
|
|
|
190
190
|
|
|
191
191
|
Validates that a string is a valid UUID format (32+ hex characters, with or without dashes).
|
|
192
192
|
|
|
193
|
-
#### `derivePlayerId(secret: string): string
|
|
193
|
+
#### `derivePlayerId(secret: string): Promise<string>`
|
|
194
194
|
|
|
195
|
-
Derives a stable, public player ID from a secret UUID
|
|
195
|
+
Derives a stable, public player ID from a secret UUID using SHA-256, taking the first 16 hex characters of the hash. Async because it uses the Web Crypto `crypto.subtle.digest` API.
|
|
196
196
|
|
|
197
197
|
### Constants
|
|
198
198
|
|
package/dist/index.cjs
CHANGED
|
@@ -217,6 +217,148 @@ var MessageTypes = {
|
|
|
217
217
|
RECONNECTED: "RECONNECTED",
|
|
218
218
|
ERROR: "ERROR"
|
|
219
219
|
};
|
|
220
|
+
// src/sha256.ts
|
|
221
|
+
var K = new Uint32Array([
|
|
222
|
+
1116352408,
|
|
223
|
+
1899447441,
|
|
224
|
+
3049323471,
|
|
225
|
+
3921009573,
|
|
226
|
+
961987163,
|
|
227
|
+
1508970993,
|
|
228
|
+
2453635748,
|
|
229
|
+
2870763221,
|
|
230
|
+
3624381080,
|
|
231
|
+
310598401,
|
|
232
|
+
607225278,
|
|
233
|
+
1426881987,
|
|
234
|
+
1925078388,
|
|
235
|
+
2162078206,
|
|
236
|
+
2614888103,
|
|
237
|
+
3248222580,
|
|
238
|
+
3835390401,
|
|
239
|
+
4022224774,
|
|
240
|
+
264347078,
|
|
241
|
+
604807628,
|
|
242
|
+
770255983,
|
|
243
|
+
1249150122,
|
|
244
|
+
1555081692,
|
|
245
|
+
1996064986,
|
|
246
|
+
2554220882,
|
|
247
|
+
2821834349,
|
|
248
|
+
2952996808,
|
|
249
|
+
3210313671,
|
|
250
|
+
3336571891,
|
|
251
|
+
3584528711,
|
|
252
|
+
113926993,
|
|
253
|
+
338241895,
|
|
254
|
+
666307205,
|
|
255
|
+
773529912,
|
|
256
|
+
1294757372,
|
|
257
|
+
1396182291,
|
|
258
|
+
1695183700,
|
|
259
|
+
1986661051,
|
|
260
|
+
2177026350,
|
|
261
|
+
2456956037,
|
|
262
|
+
2730485921,
|
|
263
|
+
2820302411,
|
|
264
|
+
3259730800,
|
|
265
|
+
3345764771,
|
|
266
|
+
3516065817,
|
|
267
|
+
3600352804,
|
|
268
|
+
4094571909,
|
|
269
|
+
275423344,
|
|
270
|
+
430227734,
|
|
271
|
+
506948616,
|
|
272
|
+
659060556,
|
|
273
|
+
883997877,
|
|
274
|
+
958139571,
|
|
275
|
+
1322822218,
|
|
276
|
+
1537002063,
|
|
277
|
+
1747873779,
|
|
278
|
+
1955562222,
|
|
279
|
+
2024104815,
|
|
280
|
+
2227730452,
|
|
281
|
+
2361852424,
|
|
282
|
+
2428436474,
|
|
283
|
+
2756734187,
|
|
284
|
+
3204031479,
|
|
285
|
+
3329325298
|
|
286
|
+
]);
|
|
287
|
+
function utf8Bytes(str) {
|
|
288
|
+
const bytes = [];
|
|
289
|
+
for (let i = 0;i < str.length; i++) {
|
|
290
|
+
let code = str.charCodeAt(i);
|
|
291
|
+
if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
|
|
292
|
+
const next = str.charCodeAt(i + 1);
|
|
293
|
+
if (next >= 56320 && next <= 57343) {
|
|
294
|
+
code = 65536 + (code - 55296 << 10) + (next - 56320);
|
|
295
|
+
i++;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (code < 128) {
|
|
299
|
+
bytes.push(code);
|
|
300
|
+
} else if (code < 2048) {
|
|
301
|
+
bytes.push(192 | code >> 6, 128 | code & 63);
|
|
302
|
+
} else if (code < 65536) {
|
|
303
|
+
bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
|
|
304
|
+
} else {
|
|
305
|
+
bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return bytes;
|
|
309
|
+
}
|
|
310
|
+
var rotr = (x, n) => x >>> n | x << 32 - n;
|
|
311
|
+
function sha256Hex(message) {
|
|
312
|
+
const bytes = utf8Bytes(message);
|
|
313
|
+
const bitLen = bytes.length * 8;
|
|
314
|
+
bytes.push(128);
|
|
315
|
+
while (bytes.length % 64 !== 56)
|
|
316
|
+
bytes.push(0);
|
|
317
|
+
const hi = Math.floor(bitLen / 4294967296);
|
|
318
|
+
const lo = bitLen >>> 0;
|
|
319
|
+
bytes.push(hi >>> 24 & 255, hi >>> 16 & 255, hi >>> 8 & 255, hi & 255, lo >>> 24 & 255, lo >>> 16 & 255, lo >>> 8 & 255, lo & 255);
|
|
320
|
+
let h0 = 1779033703, h1 = 3144134277, h2 = 1013904242, h3 = 2773480762, h4 = 1359893119, h5 = 2600822924, h6 = 528734635, h7 = 1541459225;
|
|
321
|
+
const w = new Uint32Array(64);
|
|
322
|
+
for (let offset = 0;offset < bytes.length; offset += 64) {
|
|
323
|
+
for (let i = 0;i < 16; i++) {
|
|
324
|
+
const j = offset + i * 4;
|
|
325
|
+
w[i] = (bytes[j] << 24 | bytes[j + 1] << 16 | bytes[j + 2] << 8 | bytes[j + 3]) >>> 0;
|
|
326
|
+
}
|
|
327
|
+
for (let i = 16;i < 64; i++) {
|
|
328
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
|
|
329
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
|
|
330
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
331
|
+
}
|
|
332
|
+
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
|
|
333
|
+
for (let i = 0;i < 64; i++) {
|
|
334
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
335
|
+
const ch = e & f ^ ~e & g;
|
|
336
|
+
const temp1 = h + S1 + ch + K[i] + w[i] >>> 0;
|
|
337
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
338
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
339
|
+
const temp2 = S0 + maj >>> 0;
|
|
340
|
+
h = g;
|
|
341
|
+
g = f;
|
|
342
|
+
f = e;
|
|
343
|
+
e = d + temp1 >>> 0;
|
|
344
|
+
d = c;
|
|
345
|
+
c = b;
|
|
346
|
+
b = a;
|
|
347
|
+
a = temp1 + temp2 >>> 0;
|
|
348
|
+
}
|
|
349
|
+
h0 = h0 + a >>> 0;
|
|
350
|
+
h1 = h1 + b >>> 0;
|
|
351
|
+
h2 = h2 + c >>> 0;
|
|
352
|
+
h3 = h3 + d >>> 0;
|
|
353
|
+
h4 = h4 + e >>> 0;
|
|
354
|
+
h5 = h5 + f >>> 0;
|
|
355
|
+
h6 = h6 + g >>> 0;
|
|
356
|
+
h7 = h7 + h >>> 0;
|
|
357
|
+
}
|
|
358
|
+
const toHex = (n) => (n >>> 0).toString(16).padStart(8, "0");
|
|
359
|
+
return toHex(h0) + toHex(h1) + toHex(h2) + toHex(h3) + toHex(h4) + toHex(h5) + toHex(h6) + toHex(h7);
|
|
360
|
+
}
|
|
361
|
+
|
|
220
362
|
// src/constants.ts
|
|
221
363
|
var DEFAULT_HTTP_PORT = 8080;
|
|
222
364
|
var DEFAULT_WS_PORT_OFFSET = 2;
|
|
@@ -253,7 +395,7 @@ async function derivePlayerId(secret) {
|
|
|
253
395
|
const hash = await globalThis.crypto.subtle.digest("SHA-256", data);
|
|
254
396
|
return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
|
|
255
397
|
}
|
|
256
|
-
return
|
|
398
|
+
return sha256Hex(secret).slice(0, 16);
|
|
257
399
|
}
|
|
258
400
|
function derivePlayerIdLegacy(secret) {
|
|
259
401
|
return secret.replace(/-/g, "").slice(0, 16);
|
package/dist/index.js
CHANGED
|
@@ -159,6 +159,148 @@ var MessageTypes = {
|
|
|
159
159
|
RECONNECTED: "RECONNECTED",
|
|
160
160
|
ERROR: "ERROR"
|
|
161
161
|
};
|
|
162
|
+
// src/sha256.ts
|
|
163
|
+
var K = new Uint32Array([
|
|
164
|
+
1116352408,
|
|
165
|
+
1899447441,
|
|
166
|
+
3049323471,
|
|
167
|
+
3921009573,
|
|
168
|
+
961987163,
|
|
169
|
+
1508970993,
|
|
170
|
+
2453635748,
|
|
171
|
+
2870763221,
|
|
172
|
+
3624381080,
|
|
173
|
+
310598401,
|
|
174
|
+
607225278,
|
|
175
|
+
1426881987,
|
|
176
|
+
1925078388,
|
|
177
|
+
2162078206,
|
|
178
|
+
2614888103,
|
|
179
|
+
3248222580,
|
|
180
|
+
3835390401,
|
|
181
|
+
4022224774,
|
|
182
|
+
264347078,
|
|
183
|
+
604807628,
|
|
184
|
+
770255983,
|
|
185
|
+
1249150122,
|
|
186
|
+
1555081692,
|
|
187
|
+
1996064986,
|
|
188
|
+
2554220882,
|
|
189
|
+
2821834349,
|
|
190
|
+
2952996808,
|
|
191
|
+
3210313671,
|
|
192
|
+
3336571891,
|
|
193
|
+
3584528711,
|
|
194
|
+
113926993,
|
|
195
|
+
338241895,
|
|
196
|
+
666307205,
|
|
197
|
+
773529912,
|
|
198
|
+
1294757372,
|
|
199
|
+
1396182291,
|
|
200
|
+
1695183700,
|
|
201
|
+
1986661051,
|
|
202
|
+
2177026350,
|
|
203
|
+
2456956037,
|
|
204
|
+
2730485921,
|
|
205
|
+
2820302411,
|
|
206
|
+
3259730800,
|
|
207
|
+
3345764771,
|
|
208
|
+
3516065817,
|
|
209
|
+
3600352804,
|
|
210
|
+
4094571909,
|
|
211
|
+
275423344,
|
|
212
|
+
430227734,
|
|
213
|
+
506948616,
|
|
214
|
+
659060556,
|
|
215
|
+
883997877,
|
|
216
|
+
958139571,
|
|
217
|
+
1322822218,
|
|
218
|
+
1537002063,
|
|
219
|
+
1747873779,
|
|
220
|
+
1955562222,
|
|
221
|
+
2024104815,
|
|
222
|
+
2227730452,
|
|
223
|
+
2361852424,
|
|
224
|
+
2428436474,
|
|
225
|
+
2756734187,
|
|
226
|
+
3204031479,
|
|
227
|
+
3329325298
|
|
228
|
+
]);
|
|
229
|
+
function utf8Bytes(str) {
|
|
230
|
+
const bytes = [];
|
|
231
|
+
for (let i = 0;i < str.length; i++) {
|
|
232
|
+
let code = str.charCodeAt(i);
|
|
233
|
+
if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
|
|
234
|
+
const next = str.charCodeAt(i + 1);
|
|
235
|
+
if (next >= 56320 && next <= 57343) {
|
|
236
|
+
code = 65536 + (code - 55296 << 10) + (next - 56320);
|
|
237
|
+
i++;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (code < 128) {
|
|
241
|
+
bytes.push(code);
|
|
242
|
+
} else if (code < 2048) {
|
|
243
|
+
bytes.push(192 | code >> 6, 128 | code & 63);
|
|
244
|
+
} else if (code < 65536) {
|
|
245
|
+
bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
|
|
246
|
+
} else {
|
|
247
|
+
bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return bytes;
|
|
251
|
+
}
|
|
252
|
+
var rotr = (x, n) => x >>> n | x << 32 - n;
|
|
253
|
+
function sha256Hex(message) {
|
|
254
|
+
const bytes = utf8Bytes(message);
|
|
255
|
+
const bitLen = bytes.length * 8;
|
|
256
|
+
bytes.push(128);
|
|
257
|
+
while (bytes.length % 64 !== 56)
|
|
258
|
+
bytes.push(0);
|
|
259
|
+
const hi = Math.floor(bitLen / 4294967296);
|
|
260
|
+
const lo = bitLen >>> 0;
|
|
261
|
+
bytes.push(hi >>> 24 & 255, hi >>> 16 & 255, hi >>> 8 & 255, hi & 255, lo >>> 24 & 255, lo >>> 16 & 255, lo >>> 8 & 255, lo & 255);
|
|
262
|
+
let h0 = 1779033703, h1 = 3144134277, h2 = 1013904242, h3 = 2773480762, h4 = 1359893119, h5 = 2600822924, h6 = 528734635, h7 = 1541459225;
|
|
263
|
+
const w = new Uint32Array(64);
|
|
264
|
+
for (let offset = 0;offset < bytes.length; offset += 64) {
|
|
265
|
+
for (let i = 0;i < 16; i++) {
|
|
266
|
+
const j = offset + i * 4;
|
|
267
|
+
w[i] = (bytes[j] << 24 | bytes[j + 1] << 16 | bytes[j + 2] << 8 | bytes[j + 3]) >>> 0;
|
|
268
|
+
}
|
|
269
|
+
for (let i = 16;i < 64; i++) {
|
|
270
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
|
|
271
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
|
|
272
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
273
|
+
}
|
|
274
|
+
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
|
|
275
|
+
for (let i = 0;i < 64; i++) {
|
|
276
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
277
|
+
const ch = e & f ^ ~e & g;
|
|
278
|
+
const temp1 = h + S1 + ch + K[i] + w[i] >>> 0;
|
|
279
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
280
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
281
|
+
const temp2 = S0 + maj >>> 0;
|
|
282
|
+
h = g;
|
|
283
|
+
g = f;
|
|
284
|
+
f = e;
|
|
285
|
+
e = d + temp1 >>> 0;
|
|
286
|
+
d = c;
|
|
287
|
+
c = b;
|
|
288
|
+
b = a;
|
|
289
|
+
a = temp1 + temp2 >>> 0;
|
|
290
|
+
}
|
|
291
|
+
h0 = h0 + a >>> 0;
|
|
292
|
+
h1 = h1 + b >>> 0;
|
|
293
|
+
h2 = h2 + c >>> 0;
|
|
294
|
+
h3 = h3 + d >>> 0;
|
|
295
|
+
h4 = h4 + e >>> 0;
|
|
296
|
+
h5 = h5 + f >>> 0;
|
|
297
|
+
h6 = h6 + g >>> 0;
|
|
298
|
+
h7 = h7 + h >>> 0;
|
|
299
|
+
}
|
|
300
|
+
const toHex = (n) => (n >>> 0).toString(16).padStart(8, "0");
|
|
301
|
+
return toHex(h0) + toHex(h1) + toHex(h2) + toHex(h3) + toHex(h4) + toHex(h5) + toHex(h6) + toHex(h7);
|
|
302
|
+
}
|
|
303
|
+
|
|
162
304
|
// src/constants.ts
|
|
163
305
|
var DEFAULT_HTTP_PORT = 8080;
|
|
164
306
|
var DEFAULT_WS_PORT_OFFSET = 2;
|
|
@@ -195,7 +337,7 @@ async function derivePlayerId(secret) {
|
|
|
195
337
|
const hash = await globalThis.crypto.subtle.digest("SHA-256", data);
|
|
196
338
|
return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
|
|
197
339
|
}
|
|
198
|
-
return
|
|
340
|
+
return sha256Hex(secret).slice(0, 16);
|
|
199
341
|
}
|
|
200
342
|
function derivePlayerIdLegacy(secret) {
|
|
201
343
|
return secret.replace(/-/g, "").slice(0, 16);
|
package/lib/constants.d.ts
CHANGED
|
@@ -40,8 +40,11 @@ export declare function isValidSecret(secret: string): boolean;
|
|
|
40
40
|
* Takes the first 16 hex characters of the SHA-256 hash, producing
|
|
41
41
|
* a deterministic ID that cannot be reversed to recover the secret.
|
|
42
42
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
43
|
+
* Uses the Web Crypto API (`crypto.subtle`) when available (browser / Node),
|
|
44
|
+
* and a dependency-free pure-JS SHA-256 fallback otherwise (e.g. React Native
|
|
45
|
+
* / Hermes, which does not expose `crypto.subtle`). Both paths produce the
|
|
46
|
+
* same digest, so the derived ID is identical across host runtimes and never
|
|
47
|
+
* leaks any part of the secret.
|
|
45
48
|
*/
|
|
46
49
|
export declare function derivePlayerId(secret: string): Promise<string>;
|
|
47
50
|
/**
|
package/lib/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,wDAAwD;
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,wDAAwD;AAKxD,oDAAoD;AACpD,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAEtC,4EAA4E;AAC5E,eAAO,MAAM,sBAAsB,IAAI,CAAC;AAExC,0DAA0D;AAC1D,eAAO,MAAM,eAAe,QAAQ,CAAC;AAErC,8FAA8F;AAC9F,eAAO,MAAM,cAAc,QAAc,CAAC;AAE1C,4DAA4D;AAC5D,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC,oEAAoE;AACpE,eAAO,MAAM,kBAAkB,OAAO,CAAC;AAEvC,+DAA+D;AAC/D,eAAO,MAAM,iBAAiB,QAAQ,CAAC;AAEvC,4CAA4C;AAC5C,eAAO,MAAM,qBAAqB,OAAO,CAAC;AAE1C,0DAA0D;AAC1D,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,+EAA+E;AAC/E,eAAO,MAAM,0BAA0B,QAAgB,CAAC;AAExD,8FAA8F;AAC9F,eAAO,MAAM,kBAAkB,QAAQ,CAAC;AAExC,8FAA8F;AAC9F,eAAO,MAAM,iBAAiB,QAAQ,CAAC;AAEvC;;;;;GAKG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAwBnC;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAGrD;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAiBpE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE3D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAGrD"}
|
package/lib/sha256.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free SHA-256 implementation.
|
|
3
|
+
*
|
|
4
|
+
* Couch Kit derives public player IDs from a secret using SHA-256. Browsers and
|
|
5
|
+
* Node expose this via the Web Crypto API (`crypto.subtle.digest`), but React
|
|
6
|
+
* Native / Hermes — where the host actually runs — does **not** provide
|
|
7
|
+
* `crypto.subtle` by default. This pure-JS implementation is used as the
|
|
8
|
+
* fallback so the host still produces a proper one-way hash instead of leaking
|
|
9
|
+
* part of the secret. It is byte-for-byte compatible with
|
|
10
|
+
* `crypto.subtle.digest("SHA-256", ...)`, so player IDs are stable regardless
|
|
11
|
+
* of which runtime the host uses.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Compute the SHA-256 digest of a UTF-8 string.
|
|
15
|
+
*
|
|
16
|
+
* @param message - The string to hash.
|
|
17
|
+
* @returns Lowercase hex-encoded 256-bit digest (64 characters).
|
|
18
|
+
*/
|
|
19
|
+
export declare function sha256Hex(message: string): string;
|
|
20
|
+
//# sourceMappingURL=sha256.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha256.d.ts","sourceRoot":"","sources":["../src/sha256.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAoDH;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAmGjD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@couch-kit/core",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"clean": "rm -rf dist lib"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"typescript": "^
|
|
53
|
+
"typescript": "^6.0.0"
|
|
54
54
|
}
|
|
55
55
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/** Shared constants for Couch Kit protocol defaults. */
|
|
2
2
|
|
|
3
|
+
import { sha256Hex } from "./sha256";
|
|
4
|
+
|
|
5
|
+
|
|
3
6
|
/** Default HTTP port for the static file server. */
|
|
4
7
|
export const DEFAULT_HTTP_PORT = 8080;
|
|
5
8
|
|
|
@@ -82,8 +85,11 @@ export function isValidSecret(secret: string): boolean {
|
|
|
82
85
|
* Takes the first 16 hex characters of the SHA-256 hash, producing
|
|
83
86
|
* a deterministic ID that cannot be reversed to recover the secret.
|
|
84
87
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
88
|
+
* Uses the Web Crypto API (`crypto.subtle`) when available (browser / Node),
|
|
89
|
+
* and a dependency-free pure-JS SHA-256 fallback otherwise (e.g. React Native
|
|
90
|
+
* / Hermes, which does not expose `crypto.subtle`). Both paths produce the
|
|
91
|
+
* same digest, so the derived ID is identical across host runtimes and never
|
|
92
|
+
* leaks any part of the secret.
|
|
87
93
|
*/
|
|
88
94
|
export async function derivePlayerId(secret: string): Promise<string> {
|
|
89
95
|
if (
|
|
@@ -98,8 +104,10 @@ export async function derivePlayerId(secret: string): Promise<string> {
|
|
|
98
104
|
.slice(0, 16);
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
// Web Crypto unavailable (React Native / Hermes) —
|
|
102
|
-
|
|
107
|
+
// Web Crypto unavailable (React Native / Hermes) — use the pure-JS SHA-256
|
|
108
|
+
// fallback. This still produces a secure one-way hash (unlike the deprecated
|
|
109
|
+
// legacy derivation, which exposed the first half of the secret).
|
|
110
|
+
return sha256Hex(secret).slice(0, 16);
|
|
103
111
|
}
|
|
104
112
|
|
|
105
113
|
/**
|
package/src/sha256.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free SHA-256 implementation.
|
|
3
|
+
*
|
|
4
|
+
* Couch Kit derives public player IDs from a secret using SHA-256. Browsers and
|
|
5
|
+
* Node expose this via the Web Crypto API (`crypto.subtle.digest`), but React
|
|
6
|
+
* Native / Hermes — where the host actually runs — does **not** provide
|
|
7
|
+
* `crypto.subtle` by default. This pure-JS implementation is used as the
|
|
8
|
+
* fallback so the host still produces a proper one-way hash instead of leaking
|
|
9
|
+
* part of the secret. It is byte-for-byte compatible with
|
|
10
|
+
* `crypto.subtle.digest("SHA-256", ...)`, so player IDs are stable regardless
|
|
11
|
+
* of which runtime the host uses.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Round constants (first 32 bits of the fractional parts of the cube roots of the first 64 primes). */
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
const K = new Uint32Array([
|
|
17
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
18
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
19
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
20
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
21
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
22
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
23
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
24
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/** Encode a JS string as UTF-8 bytes without relying on the global `TextEncoder`. */
|
|
28
|
+
function utf8Bytes(str: string): number[] {
|
|
29
|
+
const bytes: number[] = [];
|
|
30
|
+
for (let i = 0; i < str.length; i++) {
|
|
31
|
+
let code = str.charCodeAt(i);
|
|
32
|
+
// Combine surrogate pairs into a single code point.
|
|
33
|
+
if (code >= 0xd800 && code <= 0xdbff && i + 1 < str.length) {
|
|
34
|
+
const next = str.charCodeAt(i + 1);
|
|
35
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
36
|
+
code = 0x10000 + ((code - 0xd800) << 10) + (next - 0xdc00);
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (code < 0x80) {
|
|
41
|
+
bytes.push(code);
|
|
42
|
+
} else if (code < 0x800) {
|
|
43
|
+
bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
|
|
44
|
+
} else if (code < 0x10000) {
|
|
45
|
+
bytes.push(
|
|
46
|
+
0xe0 | (code >> 12),
|
|
47
|
+
0x80 | ((code >> 6) & 0x3f),
|
|
48
|
+
0x80 | (code & 0x3f),
|
|
49
|
+
);
|
|
50
|
+
} else {
|
|
51
|
+
bytes.push(
|
|
52
|
+
0xf0 | (code >> 18),
|
|
53
|
+
0x80 | ((code >> 12) & 0x3f),
|
|
54
|
+
0x80 | ((code >> 6) & 0x3f),
|
|
55
|
+
0x80 | (code & 0x3f),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return bytes;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Compute the SHA-256 digest of a UTF-8 string.
|
|
66
|
+
*
|
|
67
|
+
* @param message - The string to hash.
|
|
68
|
+
* @returns Lowercase hex-encoded 256-bit digest (64 characters).
|
|
69
|
+
*/
|
|
70
|
+
export function sha256Hex(message: string): string {
|
|
71
|
+
const bytes = utf8Bytes(message);
|
|
72
|
+
const bitLen = bytes.length * 8;
|
|
73
|
+
|
|
74
|
+
// Padding: append 0x80, then zeros, then the 64-bit big-endian length.
|
|
75
|
+
bytes.push(0x80);
|
|
76
|
+
while (bytes.length % 64 !== 56) bytes.push(0);
|
|
77
|
+
// 64-bit length. JS bitwise ops are 32-bit, so split high/low words.
|
|
78
|
+
const hi = Math.floor(bitLen / 0x100000000);
|
|
79
|
+
const lo = bitLen >>> 0;
|
|
80
|
+
bytes.push(
|
|
81
|
+
(hi >>> 24) & 0xff,
|
|
82
|
+
(hi >>> 16) & 0xff,
|
|
83
|
+
(hi >>> 8) & 0xff,
|
|
84
|
+
hi & 0xff,
|
|
85
|
+
(lo >>> 24) & 0xff,
|
|
86
|
+
(lo >>> 16) & 0xff,
|
|
87
|
+
(lo >>> 8) & 0xff,
|
|
88
|
+
lo & 0xff,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Initial hash values (fractional parts of square roots of first 8 primes).
|
|
92
|
+
let h0 = 0x6a09e667,
|
|
93
|
+
h1 = 0xbb67ae85,
|
|
94
|
+
h2 = 0x3c6ef372,
|
|
95
|
+
h3 = 0xa54ff53a,
|
|
96
|
+
h4 = 0x510e527f,
|
|
97
|
+
h5 = 0x9b05688c,
|
|
98
|
+
h6 = 0x1f83d9ab,
|
|
99
|
+
h7 = 0x5be0cd19;
|
|
100
|
+
|
|
101
|
+
const w = new Uint32Array(64);
|
|
102
|
+
|
|
103
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
104
|
+
for (let i = 0; i < 16; i++) {
|
|
105
|
+
const j = offset + i * 4;
|
|
106
|
+
w[i] =
|
|
107
|
+
((bytes[j] << 24) |
|
|
108
|
+
(bytes[j + 1] << 16) |
|
|
109
|
+
(bytes[j + 2] << 8) |
|
|
110
|
+
bytes[j + 3]) >>>
|
|
111
|
+
0;
|
|
112
|
+
}
|
|
113
|
+
for (let i = 16; i < 64; i++) {
|
|
114
|
+
const s0 =
|
|
115
|
+
rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
116
|
+
const s1 =
|
|
117
|
+
rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
118
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let a = h0,
|
|
122
|
+
b = h1,
|
|
123
|
+
c = h2,
|
|
124
|
+
d = h3,
|
|
125
|
+
e = h4,
|
|
126
|
+
f = h5,
|
|
127
|
+
g = h6,
|
|
128
|
+
h = h7;
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < 64; i++) {
|
|
131
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
132
|
+
const ch = (e & f) ^ (~e & g);
|
|
133
|
+
const temp1 = (h + S1 + ch + K[i] + w[i]) >>> 0;
|
|
134
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
135
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
136
|
+
const temp2 = (S0 + maj) >>> 0;
|
|
137
|
+
|
|
138
|
+
h = g;
|
|
139
|
+
g = f;
|
|
140
|
+
f = e;
|
|
141
|
+
e = (d + temp1) >>> 0;
|
|
142
|
+
d = c;
|
|
143
|
+
c = b;
|
|
144
|
+
b = a;
|
|
145
|
+
a = (temp1 + temp2) >>> 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
h0 = (h0 + a) >>> 0;
|
|
149
|
+
h1 = (h1 + b) >>> 0;
|
|
150
|
+
h2 = (h2 + c) >>> 0;
|
|
151
|
+
h3 = (h3 + d) >>> 0;
|
|
152
|
+
h4 = (h4 + e) >>> 0;
|
|
153
|
+
h5 = (h5 + f) >>> 0;
|
|
154
|
+
h6 = (h6 + g) >>> 0;
|
|
155
|
+
h7 = (h7 + h) >>> 0;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const toHex = (n: number): string => (n >>> 0).toString(16).padStart(8, "0");
|
|
159
|
+
return (
|
|
160
|
+
toHex(h0) +
|
|
161
|
+
toHex(h1) +
|
|
162
|
+
toHex(h2) +
|
|
163
|
+
toHex(h3) +
|
|
164
|
+
toHex(h4) +
|
|
165
|
+
toHex(h5) +
|
|
166
|
+
toHex(h6) +
|
|
167
|
+
toHex(h7)
|
|
168
|
+
);
|
|
169
|
+
}
|