Widget:CRCprefix

From RECESSIM, A Reverse Engineering Community
Revision as of 20:07, 21 May 2022 by Hash (talk | contribs)
Jump to navigation Jump to search

<html> <head lang="en"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>CRC Prefix Calculator</title> <script type="text/javascript"> //<![CDATA[ function calculateInit () {

   str = document.crcform.hexstring.value.toString();
   var prefix = crc.calculateInitFunc(str);
   document.getElementById("initcrc").textContent = 
                   prefix.toString(16).toUpperCase().padStart(4, "0");

}

var crc = (function() {

   applyCRC = function(crc, value) {
       crc ^= value << 8;
       crc &= 0xffff;
       for (bit = 0; bit < 8; bit++) {
           var olddata = crc & 0x8000;
           crc <<= 1;
           if (olddata) {
               crc ^= 0x1021;
           }
           crc &= 0xffff;
       }
       return crc;
   }
   calcCRC = function(pkt, crc) {
       return pkt.reduce(applyCRC, crc);
   }
   stringToBytes = function(s){
       return s.split(/([a-fA-F0-9][a-fA-F0-9])/g).filter(p => !!p).map(c => parseInt(c, 16));
   }
   bytesToString = function(b) {
       return b.map(c => c.toString(16).padStart(2,"0")).join("");
   }
   return {
       calculateInitFunc : function (str) {
           str = str.toUpperCase().replace(/\s/g,"");
           var bytes = stringToBytes(str);
           for (testvalue = 0; testvalue < 65536; testvalue+=1) {
               if (calcCRC(bytes,testvalue) === 0) {
                   return testvalue;
               }
           }
       }
   };

})(); //]]> </script> </head> <body>

CRC Prefix Calculator

Enter a string of hex bytes in the box and hit the Find CRC init button searches for the initial CRC value which would make the calculated CRC equal to zero. It uses the 0x1021 polynomial value. If it cannot find such a value, it will leave the value unchanged.

<form name="crcform"> <textarea name="hexstring" rows="5" cols="80">30FFFFFFFFFFFF50CF5DD9E2C0B80065F5D1A483F0FBBC6F01001E6C043B517E90B286 </textarea>
<label>Initial CRC value (hex):</label> <label id="initcrc">5FD6</label>
<input name="submit" type="button" value="Find CRC init" onclick="calculateInit()"> </form>


</body></html>