Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/core/operations/Snefru.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import {runHash} from "../lib/Hash.mjs";

/**
Expand All @@ -30,7 +31,7 @@ class Snefru extends Operation {
type: "number",
value: 128,
min: 32,
max: 480,
max: 448,
step: 32
},
{
Expand All @@ -47,8 +48,14 @@ class Snefru extends Operation {
* @returns {string}
*/
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, {
length: args[0],
length: size,
rounds: args[1]
});
}
Expand Down
22 changes: 22 additions & 0 deletions tests/operations/tests/Hash.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
input: "Hello, World!",
Expand Down