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/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/src/core/operations/BLAKE3.mjs b/src/core/operations/BLAKE3.mjs new file mode 100644 index 00000000..0f686120 --- /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/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"; diff --git a/tests/operations/tests/BLAKE3.mjs b/tests/operations/tests/BLAKE3.mjs new file mode 100644 index 00000000..42cb4dc1 --- /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"] } + ] + } +]);