Files
clean-architecture-frontend…/src/adapters/browser-transfer/presigned/incremental-sha256.ts
T
DongHyeonkaandClaude Opus 5.5 834308c29b perf(browser-transfer): remove guards that earlier checks already make unreachable (Q-B)
- image-header-metadata: `=== undefined` / `?? 0` / `?? null` guards on bytes
  and boxes whose index was bounds-checked a few lines earlier (PNG IHDR,
  JPEG marker, WebP VP8X/VP8L, AVIF ftyp/meta/iprp/ipma, ascii,
  readUint24LittleEndian). A differential fuzz of the old and new parser over
  720k valid and mutated PNG/JPEG/WebP/AVIF inputs found no difference.
- incremental-sha256: `?? 0` on in-range Uint32Array reads and `>>> 0` on
  Uint32Array stores, which already reduce modulo 2^32. Checked against
  node:crypto for 0..1 MiB inputs, one-shot and streamed.
- p256 verifier: the alphabet index can't be negative after the base64url
  pattern check, and the decoded signature is already an owned, exactly sized
  buffer, so it is no longer copied again.
- browser-image-probe: once the bitmap matches the header dimensions, which
  are positive and inside the decode budget, the repeated positivity and
  budget checks on the bitmap cannot fail.

file-transfer reference runtime: 54894 -> 54754 gzip bytes. Other reference
runtimes unchanged.

Verified: check:architecture, check:types:app, check:types:test, lint,
test:unit, test:component, check:adapter-inventory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 14:49:19 +09:00

202 lines
6.2 KiB
TypeScript

const INITIAL_STATE = new Uint32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
const ROUND_CONSTANTS = 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 type StreamingSha256Verifier = Readonly<{
update(bytes: Uint8Array): void;
verify(): boolean;
}>;
export function createStreamingSha256Verifier(
expectedSha256: string,
): StreamingSha256Verifier {
const accumulator = new Sha256Accumulator();
let verified = false;
return Object.freeze({
update(bytes: Uint8Array) {
if (verified) throw new TypeError("SHA-256 verifier is finalized.");
accumulator.update(bytes);
},
verify() {
if (verified) throw new TypeError("SHA-256 verifier is finalized.");
verified = true;
return constantTimeHexEqual(
accumulator.digestHex(),
expectedSha256.toLowerCase(),
);
},
});
}
export function sha256Hex(bytes: Uint8Array): string {
const accumulator = new Sha256Accumulator();
accumulator.update(bytes);
return accumulator.digestHex();
}
class Sha256Accumulator {
readonly #state = new Uint32Array(INITIAL_STATE);
readonly #buffer = new Uint8Array(64);
readonly #schedule = new Uint32Array(64);
#bufferLength = 0;
#totalBytes = 0;
#finalized = false;
update(bytes: Uint8Array): void {
if (this.#finalized || !(bytes instanceof Uint8Array)) {
throw new TypeError("SHA-256 input is invalid.");
}
const nextTotal = this.#totalBytes + bytes.byteLength;
if (!Number.isSafeInteger(nextTotal)) {
throw new TypeError("SHA-256 input is too large.");
}
this.#totalBytes = nextTotal;
let offset = 0;
if (this.#bufferLength > 0) {
const available = 64 - this.#bufferLength;
const copied = Math.min(available, bytes.byteLength);
this.#buffer.set(bytes.subarray(0, copied), this.#bufferLength);
this.#bufferLength += copied;
offset += copied;
if (this.#bufferLength === 64) {
this.#compress(this.#buffer);
this.#bufferLength = 0;
}
}
while (offset + 64 <= bytes.byteLength) {
this.#compress(bytes.subarray(offset, offset + 64));
offset += 64;
}
if (offset < bytes.byteLength) {
const remainder = bytes.subarray(offset);
this.#buffer.set(remainder, 0);
this.#bufferLength = remainder.byteLength;
}
}
digestHex(): string {
if (this.#finalized) throw new TypeError("SHA-256 is finalized.");
this.#finalized = true;
const finalLength = this.#bufferLength < 56 ? 64 : 128;
const finalBlocks = new Uint8Array(finalLength);
finalBlocks.set(this.#buffer.subarray(0, this.#bufferLength));
finalBlocks[this.#bufferLength] = 0x80;
const bitLength = BigInt(this.#totalBytes) * 8n;
for (let index = 0; index < 8; index += 1) {
finalBlocks[finalLength - 1 - index] = Number(
(bitLength >> BigInt(index * 8)) & 0xffn,
);
}
for (let offset = 0; offset < finalLength; offset += 64) {
this.#compress(finalBlocks.subarray(offset, offset + 64));
}
return Array.from(this.#state, (word) =>
word.toString(16).padStart(8, "0"),
).join("");
}
#compress(block: Uint8Array): void {
const words = this.#schedule;
const view = new DataView(
block.buffer,
block.byteOffset,
block.byteLength,
);
for (let index = 0; index < 16; index += 1) {
words[index] = view.getUint32(index * 4, false);
}
for (let index = 16; index < 64; index += 1) {
const previous15 = words[index - 15];
const previous2 = words[index - 2];
const sigma0 =
rotateRight(previous15, 7) ^
rotateRight(previous15, 18) ^
(previous15 >>> 3);
const sigma1 =
rotateRight(previous2, 17) ^
rotateRight(previous2, 19) ^
(previous2 >>> 10);
// A Uint32Array store already reduces modulo 2^32.
words[index] = words[index - 16] + sigma0 + words[index - 7] + sigma1;
}
let a = this.#state[0];
let b = this.#state[1];
let c = this.#state[2];
let d = this.#state[3];
let e = this.#state[4];
let f = this.#state[5];
let g = this.#state[6];
let h = this.#state[7];
for (let index = 0; index < 64; index += 1) {
const sum1 =
rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
const choice = (e & f) ^ (~e & g);
const temporary1 =
(h +
sum1 +
choice +
ROUND_CONSTANTS[index] +
words[index]) >>>
0;
const sum0 =
rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
const majority = (a & b) ^ (a & c) ^ (b & c);
const temporary2 = (sum0 + majority) >>> 0;
h = g;
g = f;
f = e;
e = (d + temporary1) >>> 0;
d = c;
c = b;
b = a;
a = (temporary1 + temporary2) >>> 0;
}
const state = this.#state;
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
state[5] += f;
state[6] += g;
state[7] += h;
}
}
function rotateRight(value: number, bits: number): number {
return (value >>> bits) | (value << (32 - bits));
}
function constantTimeHexEqual(left: string, right: string): boolean {
let mismatch = left.length ^ right.length;
const length = Math.max(left.length, right.length);
for (let index = 0; index < length; index += 1) {
mismatch |=
(left.charCodeAt(index) || 0) ^ (right.charCodeAt(index) || 0);
}
return mismatch === 0;
}