File size: 1,879 Bytes
21dd449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import WasmModule from "./sha256";

export async function createSHA256(isInsideWorker = false): Promise<{
	init(): void;
	update(data: Uint8Array): void;
	digest(method: "hex"): string;
}> {
	const BUFFER_MAX_SIZE = 8 * 1024 * 1024;
	const wasm: Awaited<ReturnType<typeof WasmModule>> = isInsideWorker
		? // @ts-expect-error WasmModule will be populated inside self object
		  await self["SHA256WasmModule"]()
		: await WasmModule();
	const heap = wasm.HEAPU8.subarray(wasm._GetBufferPtr());
	return {
		init() {
			wasm._Hash_Init(256);
		},
		update(data: Uint8Array) {
			let byteUsed = 0;
			while (byteUsed < data.byteLength) {
				const bytesLeft = data.byteLength - byteUsed;
				const length = Math.min(bytesLeft, BUFFER_MAX_SIZE);
				heap.set(data.subarray(byteUsed, byteUsed + length));
				wasm._Hash_Update(length);
				byteUsed += length;
			}
		},
		digest(method: "hex") {
			if (method !== "hex") {
				throw new Error("Only digest hex is supported");
			}
			wasm._Hash_Final();
			const result = Array.from(heap.slice(0, 32));
			return result.map((b) => b.toString(16).padStart(2, "0")).join("");
		},
	};
}

export function createSHA256WorkerCode(): string {
	return `
		self.addEventListener('message', async (event) => {
      const { file } = event.data;
      const sha256 = await self.createSHA256(true);
      sha256.init();
      const reader = file.stream().getReader();
      const total = file.size;
      let bytesDone = 0;
      while (true) {
        const { done, value } = await reader.read();
        if (done) {
          break;
        }
        sha256.update(value);
        bytesDone += value.length;
        postMessage({ progress: bytesDone / total });
      }
      postMessage({ sha256: sha256.digest('hex') });
    });
    self.SHA256WasmModule = ${WasmModule.toString()};
    self.createSHA256 = ${createSHA256.toString()};
  `;
}