From 3950dba2c5278451a8ead362ddfa406c147c871f Mon Sep 17 00:00:00 2001 From: h345983745 Date: Sun, 31 Mar 2019 18:32:53 +0100 Subject: [PATCH] Added Key Option --- src/core/operations/BLAKE2b.mjs | 18 ++++++++++++++---- src/core/operations/BLAKE2s.mjs | 18 +++++++++++++++--- src/core/operations/GenerateAllHashes.mjs | 16 ++++++++-------- tests/operations/tests/BLAKE2b.mjs | 17 +++++++++++++---- tests/operations/tests/BLAKE2s.mjs | 15 ++++++++++++--- 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/core/operations/BLAKE2b.mjs b/src/core/operations/BLAKE2b.mjs index 7124cd52..62709a93 100644 --- a/src/core/operations/BLAKE2b.mjs +++ b/src/core/operations/BLAKE2b.mjs @@ -36,6 +36,11 @@ class BLAKE2b extends Operation { "name": "Output Encoding", "type": "option", "value": ["Hex", "Base64", "Raw"] + }, { + "name": "Key", + "type": "toggleString", + "value": "", + "toggleValues": ["UTF8", "Decimal", "Base64", "Hex", "Latin1"] } ]; } @@ -47,14 +52,19 @@ class BLAKE2b extends Operation { */ run(input, args) { const [outSize, outFormat] = args; - + let key = Utils.convertToByteArray(args[2].string || "", args[2].option); + if (key.length === 0){ + key = null; + } else if (key.length > 64){ + throw new OperationError(["Key cannot be greater than 64 bytes", "It is currently " + key.length +" bytes."].join("\n")); + } switch (outFormat) { case "Hex": - return blakejs.blake2bHex(input, null, outSize / 8); + return blakejs.blake2bHex(input, key, outSize / 8); case "Base64": - return toBase64(blakejs.blake2b(input, null, outSize / 8)); + return toBase64(blakejs.blake2b(input, key, outSize / 8)); case "Raw": - return Utils.arrayBufferToStr(blakejs.blake2b(input, null, outSize / 8).buffer); + return Utils.arrayBufferToStr(blakejs.blake2b(input, key, outSize / 8).buffer); default: return new OperationError("Unsupported Output Type"); } diff --git a/src/core/operations/BLAKE2s.mjs b/src/core/operations/BLAKE2s.mjs index 2e7f5dd9..0ded5eac 100644 --- a/src/core/operations/BLAKE2s.mjs +++ b/src/core/operations/BLAKE2s.mjs @@ -35,6 +35,12 @@ class BLAKE2s extends Operation { "name": "Output Encoding", "type": "option", "value": ["Hex", "Base64", "Raw"] + }, + { + "name": "Key", + "type": "toggleString", + "value": "", + "toggleValues": ["UTF8", "Decimal", "Base64", "Hex", "Latin1"] } ]; } @@ -46,13 +52,19 @@ class BLAKE2s extends Operation { */ run(input, args) { const [outSize, outFormat] = args; + let key = Utils.convertToByteArray(args[2].string || "", args[2].option); + if (key.length === 0){ + key = null; + } else if (key.length > 32){ + throw new OperationError(["Key cannot be greater than 32 bytes", "It is currently " + key.length +" bytes."].join("\n")); + } switch (outFormat) { case "Hex": - return blakejs.blake2sHex(input, null, outSize / 8); + return blakejs.blake2sHex(input, key, outSize / 8); case "Base64": - return toBase64(blakejs.blake2s(input, null, outSize / 8)); + return toBase64(blakejs.blake2s(input, key, outSize / 8)); case "Raw": - return Utils.arrayBufferToStr(blakejs.blake2s(input, null, outSize / 8).buffer); + return Utils.arrayBufferToStr(blakejs.blake2s(input, key, outSize / 8).buffer); default: return new OperationError("Unsupported Output Type"); } diff --git a/src/core/operations/GenerateAllHashes.mjs b/src/core/operations/GenerateAllHashes.mjs index 3453302f..53a6fcf9 100644 --- a/src/core/operations/GenerateAllHashes.mjs +++ b/src/core/operations/GenerateAllHashes.mjs @@ -90,14 +90,14 @@ class GenerateAllHashes extends Operation { "\nWhirlpool: " + (new Whirlpool()).run(arrayBuffer, ["Whirlpool"]) + "\nSSDEEP: " + (new SSDEEP()).run(str) + "\nCTPH: " + (new CTPH()).run(str) + - "\nBLAKE2b-512: " + (new BLAKE2b).run(str, ["512", "Hex"]) + - "\nBLAKE2b-384: " + (new BLAKE2b).run(str, ["384", "Hex"]) + - "\nBLAKE2b-256: " + (new BLAKE2b).run(str, ["256", "Hex"]) + - "\nBLAKE2b-160: " + (new BLAKE2b).run(str, ["160", "Hex"]) + - "\nBLAKE2b-128: " + (new BLAKE2b).run(str, ["128", "Hex"]) + - "\nBLAKE2s-256: " + (new BLAKE2s).run(str, ["256", "Hex"]) + - "\nBLAKE2s-160: " + (new BLAKE2s).run(str, ["160", "Hex"]) + - "\nBLAKE2s-128: " + (new BLAKE2s).run(str, ["128", "Hex"]) + + "\nBLAKE2b-512: " + (new BLAKE2b).run(str, ["512", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2b-384: " + (new BLAKE2b).run(str, ["384", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2b-256: " + (new BLAKE2b).run(str, ["256", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2b-160: " + (new BLAKE2b).run(str, ["160", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2b-128: " + (new BLAKE2b).run(str, ["128", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2s-256: " + (new BLAKE2s).run(str, ["256", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2s-160: " + (new BLAKE2s).run(str, ["160", "Hex", {string: "", option: "UTF8"}]) + + "\nBLAKE2s-128: " + (new BLAKE2s).run(str, ["128", "Hex", {string: "", option: "UTF8"}]) + "\n\nChecksums:" + "\nFletcher-8: " + (new Fletcher8Checksum).run(byteArray, []) + "\nFletcher-16: " + (new Fletcher16Checksum).run(byteArray, []) + diff --git a/tests/operations/tests/BLAKE2b.mjs b/tests/operations/tests/BLAKE2b.mjs index 0c05cd76..94c4fab9 100644 --- a/tests/operations/tests/BLAKE2b.mjs +++ b/tests/operations/tests/BLAKE2b.mjs @@ -14,7 +14,7 @@ TestRegister.addTests([ expectedOutput: "4386a08a265111c9896f56456e2cb61a64239115c4784cf438e36cc851221972da3fb0115f73cd02486254001f878ab1fd126aac69844ef1c1ca152379d0a9bd", recipeConfig: [ { "op": "BLAKE2b", - "args": ["512", "Hex"] } + "args": ["512", "Hex", {string: "", option: "UTF8"}] } ] }, { @@ -23,7 +23,7 @@ TestRegister.addTests([ expectedOutput: "4d388e82ca8f866e606b6f6f0be910abd62ad6e98c0adfc27cf35acf948986d5c5b9c18b6f47261e1e679eb98edf8e2d", recipeConfig: [ { "op": "BLAKE2b", - "args": ["384", "Hex"] } + "args": ["384", "Hex", {string: "", option: "UTF8"}] } ] }, { @@ -32,7 +32,7 @@ TestRegister.addTests([ expectedOutput: "1dc01772ee0171f5f614c673e3c7fa1107a8cf727bdf5a6dadb379e93c0d1d00", recipeConfig: [ { "op": "BLAKE2b", - "args": ["256", "Hex"] } + "args": ["256", "Hex", {string: "", option: "UTF8"}] } ] }, { @@ -41,7 +41,16 @@ TestRegister.addTests([ expectedOutput: "6a8489e6fd6e51fae12ab271ec7fc8134dd5d737", recipeConfig: [ { "op": "BLAKE2b", - "args": ["160", "Hex"] } + "args": ["160", "Hex", {string: "", option: "UTF8"}] } + ] + }, + { + name: "BLAKE2b: Key Test", + input: "message data", + expectedOutput: "3d363ff7401e02026f4a4687d4863ced", + recipeConfig: [ + { "op": "BLAKE2b", + "args": ["128", "Hex", {string: "pseudorandom key", option: "UTF8"}] } ] } ]); diff --git a/tests/operations/tests/BLAKE2s.mjs b/tests/operations/tests/BLAKE2s.mjs index 80771430..09d53b6a 100755 --- a/tests/operations/tests/BLAKE2s.mjs +++ b/tests/operations/tests/BLAKE2s.mjs @@ -14,7 +14,7 @@ TestRegister.addTests([ expectedOutput: "7706af019148849e516f95ba630307a2018bb7bf03803eca5ed7ed2c3c013513", recipeConfig: [ { "op": "BLAKE2s", - "args": ["256", "Hex"] } + "args": ["256", "Hex", {string: "", option: "UTF8"}] } ] }, { @@ -23,7 +23,7 @@ TestRegister.addTests([ expectedOutput: "0e4fcfc2ee0097ac1d72d70b595a39e09a3c7c7e", recipeConfig: [ { "op": "BLAKE2s", - "args": ["160", "Hex"] } + "args": ["160", "Hex", {string: "", option: "UTF8"}] } ] }, { @@ -32,7 +32,16 @@ TestRegister.addTests([ expectedOutput: "9964ee6f36126626bf864363edfa96f6", recipeConfig: [ { "op": "BLAKE2s", - "args": ["128", "Hex"] } + "args": ["128", "Hex", {string: "", option: "UTF8"}] } + ] + }, + { + name: "BLAKE2s: Key Test", + input: "Hello World", + expectedOutput: "9964ee6f36126626bf864363edfa96f6", + recipeConfig: [ + { "op": "BLAKE2s", + "args": ["128", "Hex", {string: "", option: "UTF8"}] } ] } ]);