From cdb05efc1a0b4c8ce85696debd9505e82cbc2138 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Thu, 14 Nov 2024 16:18:58 +0100 Subject: [PATCH] fix: use blakejs instead of blake2b --- package-lock.json | 40 +++++----------------------------------- package.json | 3 +-- src/crypto/blake2b.ts | 15 +++++++++------ 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/package-lock.json b/package-lock.json index fcc60d0..c2eb6c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "SEE LICENSE IN RADIX-SOFTWARE-EULA", "dependencies": { "@radixdlt/radix-connect-schemas": "^0.1.0", - "blake2b": "^2.1.4", + "blakejs": "^1.2.1", "buffer": "^6.0.3", "get-random-values": "^3.0.0", "isomorphic-webcrypto": "^2.3.8", @@ -29,7 +29,6 @@ "@koush/wrtc": "^0.5.3", "@radixdlt/semantic-release-config": "^1.1.0", "@rollup/plugin-typescript": "^11.1.5", - "@types/blake2b": "^2.1.0", "@types/elliptic": "^6.4.14", "@types/jest": "^29.5.4", "@types/lodash.chunk": "^4.2.7", @@ -9070,12 +9069,6 @@ "@babel/types": "^7.20.7" } }, - "node_modules/@types/blake2b": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@types/blake2b/-/blake2b-2.1.0.tgz", - "integrity": "sha512-Mv8N0qtdb72iW1XJR/4Yp01frtibnv8BpFUZDROnzd7hVEX4cEN/xHgU2ZfsnOnTDt+fHJVc0uRIWcNz6uIB2A==", - "dev": true - }, "node_modules/@types/bn.js": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.1.tgz", @@ -9993,11 +9986,6 @@ "node": ">= 4.0.0" } }, - "node_modules/b4a": { - "version": "1.6.4", - "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.4.tgz", - "integrity": "sha512-fpWrvyVHEKyeEvbKZTVOeZF3VSKKWtJxFIxX/jaVPf+cLbGUSitjb49pHLqPV2BUNNZ0LcoeEGfE/YCpyDYHIw==" - }, "node_modules/b64-lite": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/b64-lite/-/b64-lite-1.4.0.tgz", @@ -10491,23 +10479,10 @@ "node": ">= 6" } }, - "node_modules/blake2b": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/blake2b/-/blake2b-2.1.4.tgz", - "integrity": "sha512-AyBuuJNI64gIvwx13qiICz6H6hpmjvYS5DGkG6jbXMOT8Z3WUJ3V1X0FlhIoT1b/5JtHE3ki+xjtMvu1nn+t9A==", - "dependencies": { - "blake2b-wasm": "^2.4.0", - "nanoassert": "^2.0.0" - } - }, - "node_modules/blake2b-wasm": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/blake2b-wasm/-/blake2b-wasm-2.4.0.tgz", - "integrity": "sha512-S1kwmW2ZhZFFFOghcx73+ZajEfKBqhP82JMssxtLVMxlaPea1p9uoLiUZ5WYyHn0KddwbLc+0vh4wR0KBNoT5w==", - "dependencies": { - "b4a": "^1.0.1", - "nanoassert": "^2.0.0" - } + "node_modules/blakejs": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.2.1.tgz", + "integrity": "sha512-QXUSXI3QVc/gJME0dBpXrag1kbzOqCjCX8/b54ntNyW6sjtoqxqRk3LTmXzaJoh71zMsDCjM+47jS7XiwN/+fQ==" }, "node_modules/blueimp-md5": { "version": "2.19.0", @@ -19313,11 +19288,6 @@ "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==", "dev": true }, - "node_modules/nanoassert": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/nanoassert/-/nanoassert-2.0.0.tgz", - "integrity": "sha512-7vO7n28+aYO4J+8w96AzhmU8G+Y/xpPDJz/se19ICsqj/momRbb9mh9ZUtkoJ5X3nTnPdhEJyc0qnM6yAsHBaA==" - }, "node_modules/nanoid": { "version": "3.3.6", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz", diff --git a/package.json b/package.json index a9dba01..c0ed54b 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,6 @@ "@koush/wrtc": "^0.5.3", "@radixdlt/semantic-release-config": "^1.1.0", "@rollup/plugin-typescript": "^11.1.5", - "@types/blake2b": "^2.1.0", "@types/elliptic": "^6.4.14", "@types/jest": "^29.5.4", "@types/lodash.chunk": "^4.2.7", @@ -88,7 +87,7 @@ }, "dependencies": { "@radixdlt/radix-connect-schemas": "^0.1.0", - "blake2b": "^2.1.4", + "blakejs": "^1.2.1", "buffer": "^6.0.3", "get-random-values": "^3.0.0", "isomorphic-webcrypto": "^2.3.8", diff --git a/src/crypto/blake2b.ts b/src/crypto/blake2b.ts index c5cbbc5..67ba1ad 100644 --- a/src/crypto/blake2b.ts +++ b/src/crypto/blake2b.ts @@ -1,5 +1,5 @@ import { Buffer } from 'buffer' -import blake2bHash from 'blake2b' +import blake from 'blakejs' import type { ResultAsync } from 'neverthrow' import { errAsync, okAsync } from 'neverthrow' @@ -7,14 +7,17 @@ export const blake2b = (input: Buffer): ResultAsync => { const output = new Uint8Array(32) try { return okAsync( - blake2bHash(output.length).update(new Uint8Array(input)).digest('hex'), - ).map((hex) => Buffer.from(hex, 'hex')) + blake.blake2b(new Uint8Array(input), undefined, output.length), + ).map((hex) => Buffer.from(hex)) } catch (error) { return errAsync(error as Error) } } export const blakeHashHexSync = (data: string) => - blake2bHash(32) - .update(new Uint8Array(Buffer.from(data, 'hex'))) - .digest('hex') + blakeHashBufferToHex(Buffer.from(data, 'hex')) + +export const blakeHashBufferToHex = (buffer: Buffer) => + Buffer.from(blake.blake2b(new Uint8Array(buffer), undefined, 32)).toString( + 'hex', + ) \ No newline at end of file