const CHUNK_BYTES = 64 * 1024;
const sha256 = load('sha2.js').sha256;
const hex = bytes => [...bytes].map(value => value.toString(16).padStart(2, '0')).join('');
function stopped(signal) { if (signal?.aborted) throw Object.assign(Error('文件核对已停止，已保存进度保留'), { name: 'AbortError' }); }
async function blob(file, { signal } = {}) {
  if (!(file instanceof Blob)) throw TypeError('需要本机文件');
  const state = sha256.create();
  try {
    for (let offset = 0; offset < file.size; offset += CHUNK_BYTES) {
      stopped(signal);
      const size = Math.min(CHUNK_BYTES, file.size - offset);
      const chunk = new Uint8Array(await file.slice(offset, offset + size).arrayBuffer());
      stopped(signal);
      if (chunk.byteLength !== size) throw Error('本机文件读取长度发生变化，请重新核对');
      state.update(chunk);
    }
    stopped(signal);
    return hex(state.digest());
  } finally { state.destroy(); }
}
globalThis.QxhImportHash = Object.freeze({ blob, chunkBytes: CHUNK_BYTES });
