fix: wrong size oshash

This commit is contained in:
monoid 2024-10-28 23:54:54 +09:00
parent a99b62a229
commit e00c888d7b

View File

@ -20,7 +20,18 @@ export async function oshash(
let hash = BigInt(st.size); let hash = BigInt(st.size);
if (st.size < minFileSize){ if (st.size < minFileSize){
throw new Error("File is too small to hash"); // Although the original oshash algorithm should throw an exception,
// just applying the hash function.
const chunk = new Uint8Array(st.size);
await fd.read(chunk, 0, st.size, 0);
const chunkView = new DataView(chunk.buffer);
for (let i = 0; i < Math.floor(st.size / 8) * 8 ; i += 8) {
hash += chunkView.getBigUint64(i, true);
hash *= 65537n; // 2^16 + 1
hash = (hash & 0xFFFFFFFFFFFFFFFFn);
}
fd.close();
return hash;
} }
// read first and last chunk // read first and last chunk