From 1e564190fb75c2083dffaaef3576364f13b4d984 Mon Sep 17 00:00:00 2001 From: marko1olo Date: Sat, 6 Jun 2026 05:30:49 +0400 Subject: [PATCH] Fix Whirlpool rounds validation --- src/core/operations/Whirlpool.mjs | 10 ++++++++-- tests/operations/tests/Hash.mjs | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/core/operations/Whirlpool.mjs b/src/core/operations/Whirlpool.mjs index c2364912..d7d69621 100644 --- a/src/core/operations/Whirlpool.mjs +++ b/src/core/operations/Whirlpool.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import {runHash} from "../lib/Hash.mjs"; /** @@ -46,8 +47,13 @@ class Whirlpool extends Operation { * @returns {string} */ run(input, args) { - const variant = args[0].toLowerCase(); - return runHash(variant, input, {rounds: args[1]}); + const variant = args[0].toLowerCase(), + rounds = args[1] ?? 10; + + if (!Number.isInteger(rounds) || rounds < 1 || rounds > 10) + throw new OperationError("Rounds must be an integer between 1 and 10"); + + return runHash(variant, input, {rounds}); } } diff --git a/tests/operations/tests/Hash.mjs b/tests/operations/tests/Hash.mjs index ba502934..536da734 100644 --- a/tests/operations/tests/Hash.mjs +++ b/tests/operations/tests/Hash.mjs @@ -338,6 +338,28 @@ TestRegister.addTests([ } ] }, + { + name: "Whirlpool rejects negative rounds", + input: "Hello, World!", + expectedOutput: "Rounds must be an integer between 1 and 10", + recipeConfig: [ + { + "op": "Whirlpool", + "args": ["Whirlpool", -1] + } + ] + }, + { + name: "Whirlpool rejects fractional rounds", + input: "Hello, World!", + expectedOutput: "Rounds must be an integer between 1 and 10", + recipeConfig: [ + { + "op": "Whirlpool", + "args": ["Whirlpool", 1.2] + } + ] + }, { name: "Snefru 2 128", input: "Hello, World!",