diff --git a/src/core/operations/SHA2.mjs b/src/core/operations/SHA2.mjs index ecdc4cc5..b55b7c28 100644 --- a/src/core/operations/SHA2.mjs +++ b/src/core/operations/SHA2.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import {runHash} from "../lib/Hash.mjs"; /** @@ -83,6 +84,11 @@ class SHA2 extends Operation { */ run(input, args) { const size = args[0]; + + if (!this.args[0].value.some(option => option.name === size)) { + throw new OperationError("Invalid SHA2 size"); + } + const rounds = (size === "256" || size === "224") ? args[1] : args[2]; return runHash("sha" + size, input, {rounds: rounds}); } diff --git a/tests/operations/tests/Hash.mjs b/tests/operations/tests/Hash.mjs index ba502934..4363d956 100644 --- a/tests/operations/tests/Hash.mjs +++ b/tests/operations/tests/Hash.mjs @@ -140,6 +140,17 @@ TestRegister.addTests([ } ] }, + { + name: "SHA2: invalid size", + input: "hi", + expectedOutput: "Invalid SHA2 size", + recipeConfig: [ + { + "op": "SHA2", + "args": ["", 64, 13] + } + ] + }, { name: "SHA3 224", input: "Hello, World!",