From 59689113d9060ab752d2f279c8bff1ef913e844a Mon Sep 17 00:00:00 2001 From: JrMasterModelBuilder Date: Thu, 2 May 2024 23:35:27 -0400 Subject: [PATCH] Added CRC64 hash class --- .eslintrc.yaml | 3 ++- util/hash.mjs | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index fe7fd6ca..b98f0b75 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -2,7 +2,7 @@ plugins: - eslint-plugin-import parserOptions: - ecmaVersion: 13 + ecmaVersion: latest sourceType: module extends: @@ -14,6 +14,7 @@ env: es6: true commonjs: true node: true + es2020: true rules: # Possible Errors: diff --git a/util/hash.mjs b/util/hash.mjs index 3a49d849..334469a5 100644 --- a/util/hash.mjs +++ b/util/hash.mjs @@ -5,11 +5,25 @@ import {createHash} from 'node:crypto'; import {Transform, Writable} from 'node:stream'; import {pipeline} from 'node:stream/promises'; +import {Crc64} from './crc64.mjs'; + +function hasher(algo) { + switch (algo) { + case 'crc64': { + return new Crc64(); + } + default: { + // Do nothing. + } + } + return createHash(algo); +} + export class HashWritable extends Writable { constructor(algos) { super(); - this._hashers = algos.map(algo => createHash(algo)); + this._hashers = algos.map(hasher); } digests(encoding = null) { @@ -21,7 +35,7 @@ export class HashWritable extends Writable { _write(chunk, encoding, callback) { for (const hasher of this._hashers) { - hasher.update(chunk, encoding); + hasher.update(Buffer.from(chunk, encoding)); } callback(); } @@ -31,7 +45,7 @@ export class HashTransform extends Transform { constructor(algos) { super(); - this._hashers = algos.map(algo => createHash(algo)); + this._hashers = algos.map(hasher); } digests(encoding = null) { @@ -43,7 +57,7 @@ export class HashTransform extends Transform { _transform(chunk, encoding, callback) { for (const hasher of this._hashers) { - hasher.update(chunk, encoding); + hasher.update(Buffer.from(chunk, encoding)); } this.push(chunk, encoding); callback();