From ad91099d5edde9f9e85bb98f22f708987f2f5faf Mon Sep 17 00:00:00 2001 From: Jeremy Giudicelli Date: Thu, 17 Apr 2025 11:37:35 +0200 Subject: [PATCH 1/3] feat : add Blake3 hashing import hash-wasm to hash inputs in utf-8 with optional key also in utf-8 and outputs in Hex --- package-lock.json | 6 ++++ package.json | 1 + src/core/operations/BLAKE3.mjs | 58 +++++++++++++++++++++++++++++++ tests/operations/tests/BLAKE3.mjs | 55 +++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 src/core/operations/BLAKE3.mjs create mode 100644 tests/operations/tests/BLAKE3.mjs diff --git a/package-lock.json b/package-lock.json index ef2da3f0..edc15036 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,6 +46,7 @@ "file-saver": "^2.0.5", "flat": "^6.0.1", "geodesy": "1.1.3", + "hash-wasm": "^4.12.0", "highlight.js": "^11.9.0", "ieee754": "^1.2.1", "jimp": "^0.22.12", @@ -10940,6 +10941,11 @@ "node": ">= 0.10" } }, + "node_modules/hash-wasm": { + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/hash-wasm/-/hash-wasm-4.12.0.tgz", + "integrity": "sha512-+/2B2rYLb48I/evdOIhP+K/DD2ca2fgBjp6O+GBEnCDk2e4rpeXIK8GvIyRPjTezgmWn9gmKwkQjjx6BtqDHVQ==" + }, "node_modules/hash.js": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.7.tgz", diff --git a/package.json b/package.json index b3492a8e..adfdcc86 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ "file-saver": "^2.0.5", "flat": "^6.0.1", "geodesy": "1.1.3", + "hash-wasm": "^4.12.0", "highlight.js": "^11.9.0", "ieee754": "^1.2.1", "jimp": "^0.22.12", diff --git a/src/core/operations/BLAKE3.mjs b/src/core/operations/BLAKE3.mjs new file mode 100644 index 00000000..20618acb --- /dev/null +++ b/src/core/operations/BLAKE3.mjs @@ -0,0 +1,58 @@ +/** + * @author xumptex [xumptex@outlook.fr] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import { blake3 } from 'hash-wasm'; +/** + * BLAKE3 operation + */ +class BLAKE3 extends Operation { + + /** + * BLAKE3 constructor + */ + constructor() { + super(); + + this.name = "BLAKE3"; + this.module = "Hashing"; + this.description = "Hashes the input using BLAKE3 (UTF-8 encoded), with an optional key (also UTF-8), and outputs the result in hexadecimal format."; + this.infoURL = "https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Size (bytes)", + "type": "number" + }, { + "name": "Key", + "type": "string", + "value": "" + }, + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const key = args[1]; + const size = args[0]; + // Check if the user want a key hash or not + if (key === "") { + return blake3(input, size*8); + }if (key.length !== 32) { + throw new OperationError("The key must be exactly 32 bytes long"); + } + return blake3(input, size*8, key); + } + +} + +export default BLAKE3; diff --git a/tests/operations/tests/BLAKE3.mjs b/tests/operations/tests/BLAKE3.mjs new file mode 100644 index 00000000..0537e467 --- /dev/null +++ b/tests/operations/tests/BLAKE3.mjs @@ -0,0 +1,55 @@ +/** + * BLAKE3 tests. + * @author xumptex [xumptex@outlook.fr] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "BLAKE3: 8 - Hello world", + input: "Hello world", + expectedOutput: "e7e6fb7d2869d109", + recipeConfig: [ + { "op": "BLAKE3", + "args": [8, ""] } + ] + }, + { + name: "BLAKE3: 16 - Hello world 2", + input: "Hello world 2", + expectedOutput: "2a3df5fe5f0d3fcdd995fc203c7f7c52", + recipeConfig: [ + { "op": "BLAKE3", + "args": [16, ""] } + ] + }, + { + name: "BLAKE3: 32 - Hello world", + input: "Hello world", + expectedOutput: "e7e6fb7d2869d109b62cdb1227208d4016cdaa0af6603d95223c6a698137d945", + recipeConfig: [ + { "op": "BLAKE3", + "args": [32, ""] } + ] + }, + { + name: "BLAKE3: Key Test", + input: "Hello world", + expectedOutput: "59dd23ac9d025690", + recipeConfig: [ + { "op": "BLAKE3", + "args": [8, "ThiskeyisexactlythirtytwoBytesLo"] } + ] + }, + { + name: "BLAKE3: Key Test 2", + input: "Hello world", + expectedOutput: "c8302c9634c1da42", + recipeConfig: [ + { "op": "BLAKE3", + "args": [8, "ThiskeyisexactlythirtytwoByteslo"] } + ] + } +]); \ No newline at end of file From 26d8fa51b06915bb09ad5553b9d8fb5421a80431 Mon Sep 17 00:00:00 2001 From: Jeremy Giudicelli Date: Thu, 17 Apr 2025 13:27:45 +0200 Subject: [PATCH 2/3] fix : delete space and add new line --- src/core/operations/BLAKE3.mjs | 8 ++++---- tests/operations/tests/BLAKE3.mjs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/operations/BLAKE3.mjs b/src/core/operations/BLAKE3.mjs index 20618acb..0f686120 100644 --- a/src/core/operations/BLAKE3.mjs +++ b/src/core/operations/BLAKE3.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation.mjs"; import OperationError from "../errors/OperationError.mjs"; -import { blake3 } from 'hash-wasm'; +import { blake3 } from "hash-wasm"; /** * BLAKE3 operation */ @@ -21,7 +21,7 @@ class BLAKE3 extends Operation { this.name = "BLAKE3"; this.module = "Hashing"; this.description = "Hashes the input using BLAKE3 (UTF-8 encoded), with an optional key (also UTF-8), and outputs the result in hexadecimal format."; - this.infoURL = "https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3"; + this.infoURL = "https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3"; this.inputType = "string"; this.outputType = "string"; this.args = [ @@ -32,7 +32,7 @@ class BLAKE3 extends Operation { "name": "Key", "type": "string", "value": "" - }, + } ]; } @@ -47,7 +47,7 @@ class BLAKE3 extends Operation { // Check if the user want a key hash or not if (key === "") { return blake3(input, size*8); - }if (key.length !== 32) { + } if (key.length !== 32) { throw new OperationError("The key must be exactly 32 bytes long"); } return blake3(input, size*8, key); diff --git a/tests/operations/tests/BLAKE3.mjs b/tests/operations/tests/BLAKE3.mjs index 0537e467..42cb4dc1 100644 --- a/tests/operations/tests/BLAKE3.mjs +++ b/tests/operations/tests/BLAKE3.mjs @@ -52,4 +52,4 @@ TestRegister.addTests([ "args": [8, "ThiskeyisexactlythirtytwoByteslo"] } ] } -]); \ No newline at end of file +]); From bd8906f46b36bd479e1222984c37a534324b2d9d Mon Sep 17 00:00:00 2001 From: Jeremy Giudicelli Date: Thu, 17 Apr 2025 13:40:01 +0200 Subject: [PATCH 3/3] fix : add Categories and index --- src/core/config/Categories.json | 1 + tests/operations/index.mjs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 71b311e6..19e84f76 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -422,6 +422,7 @@ "Snefru", "BLAKE2b", "BLAKE2s", + "BLAKE3", "GOST Hash", "Streebog", "SSDEEP", diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index bb7016bb..a4a27727 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -31,6 +31,7 @@ import "./tests/BCD.mjs"; import "./tests/BitwiseOp.mjs"; import "./tests/BLAKE2b.mjs"; import "./tests/BLAKE2s.mjs"; +import "./tests/BLAKE3.mjs"; import "./tests/Bombe.mjs"; import "./tests/BSON.mjs"; import "./tests/ByteRepr.mjs";