Prechádzať zdrojové kódy

Added 'CRC-16 Checksum' operation

n1474335 7 rokov pred
rodič
commit
174cabdc74

+ 5 - 0
package-lock.json

@@ -4198,6 +4198,11 @@
       "integrity": "sha1-8OgK4DmkvWVLXygfyT8EqRSn/M4=",
       "dev": true
     },
+    "js-crc": {
+      "version": "0.2.0",
+      "resolved": "https://registry.npmjs.org/js-crc/-/js-crc-0.2.0.tgz",
+      "integrity": "sha1-9yxcdhgXa/91zIEqHO2949jraDk="
+    },
     "js-sha3": {
       "version": "0.6.1",
       "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.6.1.tgz",

+ 1 - 0
package.json

@@ -76,6 +76,7 @@
     "exif-parser": "^0.1.12",
     "google-code-prettify": "^1.0.5",
     "jquery": "^3.1.1",
+    "js-crc": "^0.2.0",
     "js-sha3": "^0.6.1",
     "jsbn": "^1.1.0",
     "jsonpath": "^0.2.12",

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

@@ -261,6 +261,7 @@ const Categories = [
             "Fletcher-32 Checksum",
             "Fletcher-64 Checksum",
             "Adler-32 Checksum",
+            "CRC-16 Checksum",
             "CRC-32 Checksum",
             "TCP/IP Checksum",
         ]

+ 8 - 1
src/core/config/OperationConfig.js

@@ -3040,7 +3040,14 @@ const OperationConfig = {
     "CRC-32 Checksum": {
         description: "A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data.<br><br>The CRC was invented by W. Wesley Peterson in 1961; the 32-bit CRC function of Ethernet and many other standards is the work of several researchers and was published in 1975.",
         run: Checksum.runCRC32,
-        inputType: "byteArray",
+        inputType: "string",
+        outputType: "string",
+        args: []
+    },
+    "CRC-16 Checksum": {
+        description: "A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data.<br><br>The CRC was invented by W. Wesley Peterson in 1961.",
+        run: Checksum.runCRC16,
+        inputType: "string",
         outputType: "string",
         args: []
     },

+ 13 - 29
src/core/operations/Checksum.js

@@ -1,3 +1,4 @@
+import * as CRC from "js-crc";
 import Utils from "../Utils.js";
 
 
@@ -119,19 +120,24 @@ const Checksum = {
     /**
      * CRC-32 Checksum operation.
      *
-     * @param {byteArray} input
+     * @param {string} input
      * @param {Object[]} args
      * @returns {string}
      */
     runCRC32: function(input, args) {
-        let crcTable = global.crcTable || (global.crcTable = Checksum._genCRCTable()),
-            crc = 0 ^ (-1);
+        return CRC.crc32(input);
+    },
 
-        for (let i = 0; i < input.length; i++) {
-            crc = (crc >>> 8) ^ crcTable[(crc ^ input[i]) & 0xff];
-        }
 
-        return Utils.hex((crc ^ (-1)) >>> 0);
+    /**
+     * CRC-16 Checksum operation.
+     *
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    runCRC16: function(input, args) {
+        return CRC.crc16(input);
     },
 
 
@@ -168,28 +174,6 @@ const Checksum = {
         return Utils.hex(0xffff - csum);
     },
 
-
-    /**
-     * Generates a CRC table for use with CRC checksums.
-     *
-     * @private
-     * @returns {array}
-     */
-    _genCRCTable: function() {
-        let c,
-            crcTable = [];
-
-        for (let n = 0; n < 256; n++) {
-            c = n;
-            for (let k = 0; k < 8; k++) {
-                c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
-            }
-            crcTable[n] = c;
-        }
-
-        return crcTable;
-    },
-
 };
 
 export default Checksum;

+ 2 - 1
src/core/operations/Hash.js

@@ -346,7 +346,8 @@ const Hash = {
                 "\nFletcher-32: " + Checksum.runFletcher32(byteArray, []) +
                 "\nFletcher-64: " + Checksum.runFletcher64(byteArray, []) +
                 "\nAdler-32:    " + Checksum.runAdler32(byteArray, []) +
-                "\nCRC-32:      " + Checksum.runCRC32(byteArray, []);
+                "\nCRC-16:      " + Checksum.runCRC16(input, []) +
+                "\nCRC-32:      " + Checksum.runCRC32(input, []);
 
         return output;
     },