Pārlūkot izejas kodu

Merged all SHA-2 operations into one with a size argument

n1474335 7 gadi atpakaļ
vecāks
revīzija
6e875393d9

+ 1 - 4
src/core/config/Categories.js

@@ -250,10 +250,7 @@ const Categories = [
             "MD6",
             "SHA0",
             "SHA1",
-            "SHA224",
-            "SHA256",
-            "SHA384",
-            "SHA512",
+            "SHA2",
             "SHA3",
             "RIPEMD-160",
             "HMAC",

+ 13 - 28
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.<br><ul><li>SHA-256 operates on 32-bit words.</li><li>SHA-512 operates on 64-bit words.</li><li>SHA-224 is largely identical to SHA-256 but is truncated to 224 bytes.</li><li>SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes.</li></ul>",
+        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
             }
         ]
     },

+ 29 - 42
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) {
-        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);
-    },
+    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";
+        }
 
-    /**
-     * 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"]) +