Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom digest size in BLAKE2 operations #1867

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/core/operations/BLAKE2b.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class BLAKE2b extends Operation {
this.args = [
{
"name": "Size",
"type": "option",
"value": ["512", "384", "256", "160", "128"]
"type": "number",
"value": 256,
"min": 8,
"max": 512
}, {
"name": "Output Encoding",
"type": "option",
Expand Down Expand Up @@ -61,6 +63,10 @@ class BLAKE2b extends Operation {
throw new OperationError(["Key cannot be greater than 64 bytes", "It is currently " + key.length + " bytes."].join("\n"));
}

if (outSize % 8 !== 0 || outSize < 8 || outSize > 512) {
throw new OperationError("Size has to be between 8 to 512 and multiple of 8.");
}

input = new Uint8Array(input);
switch (outFormat) {
case "Hex":
Expand Down
10 changes: 8 additions & 2 deletions src/core/operations/BLAKE2s.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class BLAKE2s extends Operation {
this.args = [
{
"name": "Size",
"type": "option",
"value": ["256", "160", "128"]
"type": "number",
"value": 256,
"min": 8,
"max": 256
}, {
"name": "Output Encoding",
"type": "option",
Expand Down Expand Up @@ -62,6 +64,10 @@ class BLAKE2s extends Operation {
throw new OperationError(["Key cannot be greater than 32 bytes", "It is currently " + key.length + " bytes."].join("\n"));
}

if (outSize % 8 !== 0 || outSize < 8 || outSize > 256) {
throw new OperationError("Size has to be between 8 to 256 and multiple of 8.");
}

input = new Uint8Array(input);
switch (outFormat) {
case "Hex":
Expand Down
18 changes: 18 additions & 0 deletions tests/operations/tests/BLAKE2b.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,23 @@ TestRegister.addTests([
{ "op": "BLAKE2b",
"args": ["128", "Hex", {string: "pseudorandom key", option: "UTF8"}] }
]
},
{
name: "BLAKE2b: 224 - Hello World",
input: "Hello World",
expectedOutput: "19790463ef4ad09bdb724e3a6550c640593d4870f6e192ac8147f35d",
recipeConfig: [
{ "op": "BLAKE2b",
"args": ["224", "Hex", {string: "", option: "UTF8"}] }
]
},
{
name: "BLAKE2b: 192 - Hello World",
input: "Hello World",
expectedOutput: "317d59b18418b8a12623faf5ebc2072e5e70ac4b8ba6d33a",
recipeConfig: [
{ "op": "BLAKE2b",
"args": ["192", "Hex", {string: "", option: "UTF8"}] }
]
}
]);
18 changes: 18 additions & 0 deletions tests/operations/tests/BLAKE2s.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,23 @@ TestRegister.addTests([
{ "op": "BLAKE2s",
"args": ["128", "Hex", {string: "", option: "UTF8"}] }
]
},
{
name: "BLAKE2s: 224 - Hello World",
input: "Hello World",
expectedOutput: "8a74d9e5c5de9c8a0cb1c292f65808831717e422302f3d58853d58ed",
recipeConfig: [
{ "op": "BLAKE2s",
"args": ["224", "Hex", {string: "", option: "UTF8"}] }
]
},
{
name: "BLAKE2s: 192 - Hello World",
input: "Hello World",
expectedOutput: "cc181c0f167a1727308d894c6182bbe6c4865698e7f7bfac",
recipeConfig: [
{ "op": "BLAKE2s",
"args": ["192", "Hex", {string: "", option: "UTF8"}] }
]
}
]);
Loading