diff --git a/package-lock.json b/package-lock.json index 985391a9..87615219 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2249,6 +2249,11 @@ "integrity": "sha512-Un7MIEDdUC5gNpcGDV97op1Ywk748MpHcFTHoYs6qnj1Z3j7I53VG3nwZhKzoBZmbdRNnb6WRdFlwl7tSDuZGw==", "dev": true }, + "blakejs": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.1.0.tgz", + "integrity": "sha1-ad+S75U6qIylGjLfarHFShVfx6U=" + }, "block-stream": { "version": "0.0.9", "resolved": "https://registry.npmjs.org/block-stream/-/block-stream-0.0.9.tgz", diff --git a/package.json b/package.json index 2bd5b269..320908b8 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "babel-plugin-transform-builtin-extend": "1.1.2", "bcryptjs": "^2.4.3", "bignumber.js": "^8.1.1", + "blakejs": "^1.1.0", "bootstrap": "4.2.1", "bootstrap-colorpicker": "^2.5.3", "bootstrap-material-design": "^4.1.1", diff --git a/src/core/operations/BLAKE2b.mjs b/src/core/operations/BLAKE2b.mjs new file mode 100644 index 00000000..e5bda30e --- /dev/null +++ b/src/core/operations/BLAKE2b.mjs @@ -0,0 +1,48 @@ +/** + * @author h [h] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; +import blakejs from "blakejs"; + +/** + * BLAKE2b operation + */ +class BLAKE2b extends Operation { + + /** + * BLAKE2b constructor + */ + constructor() { + super(); + + this.name = "BLAKE2b"; + this.module = "Hashing"; + this.description = "Performs BLAKE2b hashing on the input. Returns the output HEX encoded."; + this.infoURL = "https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Size", + "type": "option", + "value": ["512", "384", "256", "160", "128"] + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} The input having been hashed with BLAKE2b, HEX encoded. + */ + run(input, args) { + const [outSize] = args; + return blakejs.blake2bHex(input, null, outSize / 8); + } + +} + +export default BLAKE2b; diff --git a/src/core/operations/BLAKE2s.mjs b/src/core/operations/BLAKE2s.mjs new file mode 100644 index 00000000..b5b8ed55 --- /dev/null +++ b/src/core/operations/BLAKE2s.mjs @@ -0,0 +1,48 @@ +/** + * @author h [h] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; +import blakejs from "blakejs"; + +/** + * BLAKE2s Operation + */ +class BLAKE2s extends Operation { + + /** + * BLAKE2s constructor + */ + constructor() { + super(); + + this.name = "BLAKE2s"; + this.module = "Hashing"; + this.description = "Performs BLAKE2s hashing on the input. Returns the output HEX encoded."; + this.infoURL = "https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Size", + "type": "option", + "value": ["256", "160", "128"] + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} The input having been hashed with BLAKE2s, HEX encoded. + */ + run(input, args) { + const [outSize] = args; + return blakejs.blake2sHex(input, null, outSize / 8); + } + +} + +export default BLAKE2s; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index c54f15e8..07716fe2 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -86,6 +86,8 @@ import "./tests/Enigma"; import "./tests/Bombe"; import "./tests/MultipleBombe"; import "./tests/Typex"; +import "./tests/BLAKE2b"; +import "./tests/BLAKE2s"; // Cannot test operations that use the File type yet //import "./tests/SplitColourChannels"; diff --git a/tests/operations/tests/BLAKE2b.mjs b/tests/operations/tests/BLAKE2b.mjs new file mode 100644 index 00000000..9a4c211e --- /dev/null +++ b/tests/operations/tests/BLAKE2b.mjs @@ -0,0 +1,47 @@ +/** + * BitwiseOp tests + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../TestRegister"; + +TestRegister.addTests([ + { + name: "BLAKE2b: 512 - Hello World", + input: "Hello World", + expectedOutput: "4386a08a265111c9896f56456e2cb61a64239115c4784cf438e36cc851221972da3fb0115f73cd02486254001f878ab1fd126aac69844ef1c1ca152379d0a9bd", + recipeConfig: [ + { "op": "BLAKE2b", + "args": ["512"] } + ] + }, + { + name: "BLAKE2b: 384 - Hello World", + input: "Hello World", + expectedOutput: "4d388e82ca8f866e606b6f6f0be910abd62ad6e98c0adfc27cf35acf948986d5c5b9c18b6f47261e1e679eb98edf8e2d", + recipeConfig: [ + { "op": "BLAKE2b", + "args": ["384"] } + ] + }, + { + name: "BLAKE2b: 256 - Hello World", + input: "Hello World", + expectedOutput: "1dc01772ee0171f5f614c673e3c7fa1107a8cf727bdf5a6dadb379e93c0d1d00", + recipeConfig: [ + { "op": "BLAKE2b", + "args": ["256"] } + ] + }, + { + name: "BLAKE2b: 160 - Hello World", + input: "Hello World", + expectedOutput: "6a8489e6fd6e51fae12ab271ec7fc8134dd5d737", + recipeConfig: [ + { "op": "BLAKE2b", + "args": ["160"] } + ] + } +]); diff --git a/tests/operations/tests/BLAKE2s.mjs b/tests/operations/tests/BLAKE2s.mjs new file mode 100755 index 00000000..e08996ad --- /dev/null +++ b/tests/operations/tests/BLAKE2s.mjs @@ -0,0 +1,38 @@ +/** + * BitwiseOp tests + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../TestRegister"; + +TestRegister.addTests([ + { + name: "BLAKE2s: 256 - Hello World", + input: "Hello World", + expectedOutput: "7706af019148849e516f95ba630307a2018bb7bf03803eca5ed7ed2c3c013513", + recipeConfig: [ + { "op": "BLAKE2s", + "args": ["256"] } + ] + }, + { + name: "BLAKE2s: 160 - Hello World", + input: "Hello World", + expectedOutput: "0e4fcfc2ee0097ac1d72d70b595a39e09a3c7c7e", + recipeConfig: [ + { "op": "BLAKE2s", + "args": ["160"] } + ] + }, + { + name: "BLAKE2s: 128 - Hello World", + input: "Hello World", + expectedOutput: "9964ee6f36126626bf864363edfa96f6", + recipeConfig: [ + { "op": "BLAKE2s", + "args": ["128"] } + ] + } +]);