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
8 changes: 7 additions & 1 deletion src/core/operations/MD2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

/**
* MD2 operation
Expand Down Expand Up @@ -40,7 +41,12 @@ class MD2 extends Operation {
* @returns {string}
*/
run(input, args) {
return runHash("md2", input, {rounds: args[0]});
const rounds = args[0] ?? 18;

if (!Number.isInteger(rounds) || rounds < 0)
throw new OperationError("Rounds must be a non-negative integer");

return runHash("md2", input, {rounds});
}

}
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 @@ -19,6 +19,28 @@ TestRegister.addTests([
}
]
},
{
name: "MD2 rejects negative rounds",
input: "Hello, World!",
expectedOutput: "Rounds must be a non-negative integer",
recipeConfig: [
{
"op": "MD2",
"args": [-1]
}
]
},
{
name: "MD2 rejects fractional rounds",
input: "Hello, World!",
expectedOutput: "Rounds must be a non-negative integer",
recipeConfig: [
{
"op": "MD2",
"args": [1.2]
}
]
},
{
name: "MD4",
input: "Hello, World!",
Expand Down