Fix Snefru size validation

This commit is contained in:
marko1olo 2026-06-06 02:27:55 +04:00
parent d735496641
commit a08f57cc15
2 changed files with 31 additions and 2 deletions

View File

@ -5,6 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import {runHash} from "../lib/Hash.mjs"; import {runHash} from "../lib/Hash.mjs";
/** /**
@ -30,7 +31,7 @@ class Snefru extends Operation {
type: "number", type: "number",
value: 128, value: 128,
min: 32, min: 32,
max: 480, max: 448,
step: 32 step: 32
}, },
{ {
@ -47,8 +48,14 @@ class Snefru extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const size = args[0];
if (!Number.isInteger(size) || size < 32 || size > 448 || size % 32 !== 0) {
throw new OperationError("Size must be a multiple of 32 between 32 and 448");
}
return runHash("snefru", input, { return runHash("snefru", input, {
length: args[0], length: size,
rounds: args[1] rounds: args[1]
}); });
} }

View File

@ -404,6 +404,28 @@ TestRegister.addTests([
} }
] ]
}, },
{
name: "Snefru rejects negative size",
input: "hello",
expectedOutput: "Size must be a multiple of 32 between 32 and 448",
recipeConfig: [
{
"op": "Snefru",
"args": [-32, "8"]
}
]
},
{
name: "Snefru rejects out-of-range size",
input: "hello",
expectedOutput: "Size must be a multiple of 32 between 32 and 448",
recipeConfig: [
{
"op": "Snefru",
"args": [480, "8"]
}
]
},
{ {
name: "SM3 256 64", name: "SM3 256 64",
input: "Hello, World!", input: "Hello, World!",