evOo-local/apps/mobile/lib/media-sha256.ts
Claude 4c30af9ea3
feat(media): mobile library index, resumable upload, and project variants
Index photos and videos from the device library into a client-pushed
ios_library source, upload originals in resumable chunks, and review,
approve, and share project variants with expo-video and the native share
sheet. Pure logic for chunking, resume, indexing, and variants is unit
tested.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NLSCTnhge4umMnemGXWr1w
2026-10-03 19:20:42 +00:00

124 lines
4.5 KiB
TypeScript

/*
* Incremental SHA-256 for media uploads. `uploads.begin` needs the hash of the
* whole original before the first chunk, and originals can be gigabytes, so the
* file is hashed chunk by chunk instead of being read into memory.
*/
const K = new Uint32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
export class Sha256 {
private readonly state = new Uint32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
private readonly block = new Uint8Array(64);
private readonly words = new Uint32Array(64);
private blockLength = 0;
private totalBytes = 0;
private finished = false;
update(data: Uint8Array): this {
if (this.finished) throw new Error("Hash already finished");
let offset = 0;
this.totalBytes += data.byteLength;
if (this.blockLength > 0) {
const take = Math.min(64 - this.blockLength, data.byteLength);
this.block.set(data.subarray(0, take), this.blockLength);
this.blockLength += take;
offset = take;
if (this.blockLength < 64) return this;
this.compress(this.block, 0);
this.blockLength = 0;
}
while (offset + 64 <= data.byteLength) {
this.compress(data, offset);
offset += 64;
}
if (offset < data.byteLength) {
this.block.set(data.subarray(offset), 0);
this.blockLength = data.byteLength - offset;
}
return this;
}
/** Lowercase hex digest, the form `uploads.begin` expects. */
hex(): string {
if (this.finished) throw new Error("Hash already finished");
const bitsHigh = Math.floor(this.totalBytes / 0x20000000);
const bitsLow = (this.totalBytes * 8) >>> 0;
const padLength = this.blockLength < 56 ? 56 - this.blockLength : 120 - this.blockLength;
const tail = new Uint8Array(padLength + 8);
tail[0] = 0x80;
const view = new DataView(tail.buffer);
view.setUint32(padLength, bitsHigh);
view.setUint32(padLength + 4, bitsLow);
const total = this.totalBytes;
this.update(tail);
this.totalBytes = total;
this.finished = true;
let out = "";
for (const word of this.state) out += word.toString(16).padStart(8, "0");
return out;
}
private compress(data: Uint8Array, offset: number): void {
const w = this.words;
for (let i = 0; i < 16; i++) {
const j = offset + i * 4;
w[i] =
((data[j] ?? 0) << 24) |
((data[j + 1] ?? 0) << 16) |
((data[j + 2] ?? 0) << 8) |
(data[j + 3] ?? 0);
}
for (let i = 16; i < 64; i++) {
const a = w[i - 15] ?? 0;
const b = w[i - 2] ?? 0;
const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
w[i] = ((w[i - 16] ?? 0) + s0 + (w[i - 7] ?? 0) + s1) | 0;
}
const s = this.state;
let a = s[0] ?? 0;
let b = s[1] ?? 0;
let c = s[2] ?? 0;
let d = s[3] ?? 0;
let e = s[4] ?? 0;
let f = s[5] ?? 0;
let g = s[6] ?? 0;
let h = s[7] ?? 0;
for (let i = 0; i < 64; i++) {
const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
const ch = (e & f) ^ (~e & g);
const t1 = (h + s1 + ch + (K[i] ?? 0) + (w[i] ?? 0)) | 0;
const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
const maj = (a & b) ^ (a & c) ^ (b & c);
const t2 = (s0 + maj) | 0;
h = g;
g = f;
f = e;
e = (d + t1) | 0;
d = c;
c = b;
b = a;
a = (t1 + t2) | 0;
}
s[0] = (s[0] ?? 0) + a;
s[1] = (s[1] ?? 0) + b;
s[2] = (s[2] ?? 0) + c;
s[3] = (s[3] ?? 0) + d;
s[4] = (s[4] ?? 0) + e;
s[5] = (s[5] ?? 0) + f;
s[6] = (s[6] ?? 0) + g;
s[7] = (s[7] ?? 0) + h;
}
}