From 6e875393d9e3a8ab233533f9c7578a6e30a45711 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Tue, 12 Sep 2017 14:48:56 +0000 Subject: [PATCH] Merged all SHA-2 operations into one with a size argument --- src/core/config/Categories.js | 5 +- src/core/config/OperationConfig.js | 41 ++++++---------- src/core/operations/Hash.js | 75 ++++++++++++------------------ 3 files changed, 45 insertions(+), 76 deletions(-) diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index 6711d294..d49b1545 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -250,10 +250,7 @@ const Categories = [ "MD6", "SHA0", "SHA1", - "SHA224", - "SHA256", - "SHA384", - "SHA512", + "SHA2", "SHA3", "RIPEMD-160", "HMAC", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 3822be60..3ba2544b 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -2883,7 +2883,7 @@ const OperationConfig = { args: [] }, "MD6": { - description: "TODO", + description: "The MD6 (Message-Digest 6) algorithm is a cryptographic hash function. It uses a Merkle tree-like structure to allow for immense parallel computation of hashes for very long inputs.", run: Hash.runMD6, inputType: "string", outputType: "string", @@ -2919,33 +2919,18 @@ const OperationConfig = { outputType: "string", args: [] }, - "SHA224": { - description: "SHA-224 is largely identical to SHA-256 but is truncated to 224 bytes.", - run: Hash.runSHA224, + "SHA2": { + description: "The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2 includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224, SHA256, SHA384, SHA512.
", + run: Hash.runSHA2, inputType: "string", outputType: "string", - args: [] - }, - "SHA256": { - description: "SHA-256 is one of the four variants in the SHA-2 set. It isn't as widely used as SHA-1, though it provides much better security.", - run: Hash.runSHA256, - inputType: "string", - outputType: "string", - args: [] - }, - "SHA384": { - description: "SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes.", - run: Hash.runSHA384, - inputType: "string", - outputType: "string", - args: [] - }, - "SHA512": { - description: "SHA-512 is largely identical to SHA-256 but operates on 64-bit words rather than 32.", - run: Hash.runSHA512, - inputType: "string", - outputType: "string", - args: [] + args: [ + { + name: "Size", + type: "option", + value: Hash.SHA2_SIZE + } + ] }, "SHA3": { description: "This is an implementation of Keccak[c=2d]. SHA3 functions based on different implementations of Keccak will give different results.", @@ -2954,9 +2939,9 @@ const OperationConfig = { outputType: "string", args: [ { - name: "Output length", + name: "Size", type: "option", - value: Hash.SHA3_LENGTH + value: Hash.SHA3_SIZE } ] }, diff --git a/src/core/operations/Hash.js b/src/core/operations/Hash.js index 4bc06c62..119585a6 100755 --- a/src/core/operations/Hash.js +++ b/src/core/operations/Hash.js @@ -111,54 +111,41 @@ const Hash = { /** - * SHA224 operation. - * - * @param {string} input - * @param {Object[]} args - * @returns {string} + * @constant + * @default */ - runSHA224: function (input, args) { - input = CryptoJS.enc.Latin1.parse(input); - return CryptoJS.SHA224(input).toString(CryptoJS.enc.Hex); - }, - + SHA2_SIZE: ["256", "512", "224", "384"], /** - * SHA256 operation. + * SHA2 operation. * * @param {string} input * @param {Object[]} args * @returns {string} */ - runSHA256: function (input, args) { + runSHA2: function (input, args) { + const size = parseInt(args[0], 10); + let algo; + + switch (size) { + case 224: + algo = CryptoJS.SHA224; + break; + case 384: + algo = CryptoJS.SHA384; + break; + case 256: + algo = CryptoJS.SHA256; + break; + case 512: + algo = CryptoJS.SHA512; + break; + default: + return "Invalid size"; + } + input = CryptoJS.enc.Latin1.parse(input); - return CryptoJS.SHA256(input).toString(CryptoJS.enc.Hex); - }, - - - /** - * SHA384 operation. - * - * @param {string} input - * @param {Object[]} args - * @returns {string} - */ - runSHA384: function (input, args) { - input = CryptoJS.enc.Latin1.parse(input); - return CryptoJS.SHA384(input).toString(CryptoJS.enc.Hex); - }, - - - /** - * SHA512 operation. - * - * @param {string} input - * @param {Object[]} args - * @returns {string} - */ - runSHA512: function (input, args) { - input = CryptoJS.enc.Latin1.parse(input); - return CryptoJS.SHA512(input).toString(CryptoJS.enc.Hex); + return algo(input).toString(CryptoJS.enc.Hex); }, @@ -166,7 +153,7 @@ const Hash = { * @constant * @default */ - SHA3_LENGTH: ["512", "384", "256", "224"], + SHA3_SIZE: ["512", "384", "256", "224"], /** * SHA3 operation. @@ -243,10 +230,10 @@ const Hash = { "\nMD6: " + Hash.runMD6(input, []) + "\nSHA0: " + Hash.runSHA0(input, []) + "\nSHA1: " + Hash.runSHA1(input, []) + - "\nSHA2 224: " + Hash.runSHA224(input, []) + - "\nSHA2 256: " + Hash.runSHA256(input, []) + - "\nSHA2 384: " + Hash.runSHA384(input, []) + - "\nSHA2 512: " + Hash.runSHA512(input, []) + + "\nSHA2 224: " + Hash.runSHA2(input, ["224"]) + + "\nSHA2 256: " + Hash.runSHA2(input, ["256"]) + + "\nSHA2 384: " + Hash.runSHA2(input, ["384"]) + + "\nSHA2 512: " + Hash.runSHA2(input, ["512"]) + "\nSHA3 224: " + Hash.runSHA3(input, ["224"]) + "\nSHA3 256: " + Hash.runSHA3(input, ["256"]) + "\nSHA3 384: " + Hash.runSHA3(input, ["384"]) +