hash improvement

This commit is contained in:
Metazla
2024-04-12 16:28:14 +02:00
parent 6123e10c07
commit 1c65673567
43 changed files with 1545 additions and 171 deletions
+13 -9
View File
@@ -15,13 +15,15 @@ export async function computeCIDs({stream, algorithms}: {
stream: Readable | ReadableStream;
algorithms: CID_ALGORITHM_NAMES[]
}): Promise<string[]> {
const hashers = hasherDefiner(algorithms);
const hashers = await hasherDefiner(algorithms);
// Check if the environment is Node.js or Browser and handle the stream accordingly
if (stream instanceof Readable) {
// Node.js environment
for await (const chunk of stream) {
hashers.forEach(item => item.hasher.update(chunk as Buffer));
for (const item of hashers) {
await item.hasher.update(chunk as Buffer);
}
}
} else {
// Browser environment
@@ -30,28 +32,30 @@ export async function computeCIDs({stream, algorithms}: {
const {done, value} = await reader.read();
if (done) break;
// Assuming the hasher can handle Uint8Array directly
hashers.forEach(item => item.hasher.update(Buffer.from(value)));
for (const item of hashers) {
await item.hasher.update(Buffer.from(value));
}
}
}
return cidFinalize(hashers);
}
function hasherDefiner(algorithms: CID_ALGORITHM_NAMES[]): {
async function hasherDefiner(algorithms: CID_ALGORITHM_NAMES[]): Promise<{
hasher: SimpleHash,
code: CID_ALGORITHM_CODES
}[] {
}[]> {
const hashers = algorithms.filter(algo => Object.values(CID_ALGORITHM_NAMES).includes(algo))
.map(algo => ({
hasher: createHasher(algo),
.map(async algo => ({
hasher: await createHasher(algo),
code: codeTable[algo]
}));
return hashers;
return await Promise.all(hashers);
}
async function cidFinalize(hashers: { hasher: SimpleHash, code: number }[]): Promise<string[]> {
return await Promise.all(hashers.map(async ({code, hasher}) => {
const hashBuffer = hasher.digest();
const hashBuffer = await hasher.digest();
const digest = create(code, hashBuffer);
return CID.createV1(code, digest).toString();
}));
+1 -1
View File
@@ -4,7 +4,7 @@ import {SimpleHash} from "@root/file-id/SimpleHash";
export class Crc32Hash implements SimpleHash {
private _crc: number = undefined;
update(data: Buffer): Crc32Hash {
update(data: Buffer): SimpleHash {
// Update the CRC-32 checksum with the new chunk of data
this._crc = crc32.buf(data, this._crc);
return this;
+1 -3
View File
@@ -1,6 +1,4 @@
import {CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from "../hash-compute/MultiHashData";
import {Readable} from "stream";
import {ReadableStream} from "stream/web";
import {SimpleHash} from "./SimpleHash";
import {createHash} from "crypto";
import {Crc32Hash} from "./Crc32Hash";
@@ -19,7 +17,7 @@ export const codeTable = {
* @returns Array of CIDs (in the order of the algorithms)
* @param algo
*/
export function createHasher(algo: CID_ALGORITHM_NAMES): SimpleHash {
export async function createHasher(algo: CID_ALGORITHM_NAMES): Promise<SimpleHash> {
switch (algo) {
case CID_ALGORITHM_NAMES.sha256:
return createHash("sha256");
+2 -2
View File
@@ -1,5 +1,5 @@
export interface SimpleHash {
update(data: Buffer),
update(data: Buffer): SimpleHash | Promise<SimpleHash>;
digest(): Buffer;
digest(): Buffer | Promise<Buffer>;
}