Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Web Development Languages (http://www.programmingforums.org/forum40.html)
-   -   Implementation of CRC32 (http://www.programmingforums.org/showthread.php?t=12698)

grimpirate Feb 28th, 2007 3:52 PM

Implementation of CRC32
 
ok now I realize this isn't the best or most efficient or even a way that I should implement CRC32, BUT I figure if I can do it this way first then the other better ways will be easier to understand (or at the very least please afford me that discretion). I'm basically trying to compute the crc32 hash of the letter 'a' using ActionScript. Now irrelevant of the actual language, this actionscript code doesn't use any fancy Flash stuff other than the function trace() which outputs results in a console-like fashion for debugging. So here's the code:
:

trace(crc32('a'));

function crc32(input:String):String {
        var hexInput:String = '';
        for(var i:Number = 0; i < input.length; i++) {
                hexInput += dechex(input.charCodeAt(i));
        }
        var binaryInput:String = '';
        for(var i:Number = 0; i < hexInput.length; i++) {
                switch(hexInput.charCodeAt(i)) {
                        case 48:
                        binaryInput += '0000';
                        break;
                        case 49:
                        binaryInput += '0001';
                        break;
                        case 50:
                        binaryInput += '0010';
                        break;
                        case 51:
                        binaryInput += '0011';
                        break;
                        case 52:
                        binaryInput += '0100';
                        break;
                        case 53:
                        binaryInput += '0101';
                        break;
                        case 54:
                        binaryInput += '0110';
                        break;
                        case 55:
                        binaryInput += '0111';
                        break;
                        case 56:
                        binaryInput += '1000';
                        break;
                        case 57:
                        binaryInput += '1001';
                        break;
                        case 97:
                        binaryInput += '1010';
                        break;
                        case 98:
                        binaryInput += '1011';
                        break;
                        case 99:
                        binaryInput += '1100';
                        break;
                        case 100:
                        binaryInput += '1101';
                        break;
                        case 101:
                        binaryInput += '1110';
                        break;
                        case 102:
                        binaryInput += '1111';
                        break;
                }
        }
        binaryInput += '00000000000000000000000000000000';
        var binaryGenerator:String = '100000100110000010001110110110111';
        for(var i:Number = 0; i < binaryInput.length - binaryGenerator.length; i++) {
                if(binaryInput.charAt(i) == '1') {
                        for(var j:Number = 0; j < binaryGenerator.length; j++) {
                                if(binaryInput.charAt(i + j) != binaryGenerator.charAt(j)){
                                        binaryInput = replace(binaryInput, i + j, '1');
                                }else{
                                        binaryInput = replace(binaryInput, i + j, '0');
                                }
                        }
                }
        }
        return binaryInput.substr(binaryInput.length - (binaryGenerator.length - 1));
}

function replace(original:String, index:Number, replacement:String):String {
        if(index == 0){
                return replacement.concat(original.substr(1));
        }
        if(index == original.length - 1){
                return original.substr(0, original.length - 1) + replacement;
        }
        return original.substr(0, index) + replacement + original.substr(index + 1);
}

function dechex(val:Number):String {
        if(val < 0){
                val += Math.pow(2, 32);
        }
        var output:String = '';
        for(var i:Number = 0; i < 8; i++){
                var remainder:Number = val % 16;
                val = (val - remainder) / 16;
                switch(remainder){
                        case 10:
                        output = 'a' + output;
                        break;
                        case 11:
                        output = 'b' + output;
                        break;
                        case 12:
                        output = 'c' + output;
                        break;
                        case 13:
                        output = 'd' + output;
                        break;
                        case 14:
                        output = 'e' + output;
                        break;
                        case 15:
                        output = 'f' + output;
                        break;
                        default:
                        output = remainder + output;
                        break;
                }
        }
        do{
                if(output.charCodeAt(0) == 48){
                        output = output.substr(1, output.length - 1);
                }
        }while(output.charCodeAt(0) == 48);
        return output;
}

This results in 1010 1000 0110 0100 1101 1011 0010 0000. Now I performed this calculation by hand and that's the actual result of the calculation I performed by hand. SO somewhere along the line I'm missing something I just don't know what. It may have something to do with big-endian or little-endian format. I'm hoping someone can point me in the right direction in terms of what I'm doing wrong.

Here's an explanation on crc32

The actual value I'm trying to achieve is 0xe8b7be43

grimpirate Mar 2nd, 2007 12:41 AM

As per this page I just discovered that you can append the crc32 checksum to the original message and that if you compute the crc32 value of that then the result should be 0. I did this with my own program and sure enough the result was 0. Soooooooo now I'm even more confused 'cause it seemingly works correctly. What am I doing wrong? :confused: This is so discouraging :(

grimpirate Mar 3rd, 2007 3:13 PM

I changed the script a bit and now it will actually verify the checksum by appending it to the original message and computing the remainder.
:

trace('//////////////////////');
trace('// Compute CheckSum //');
trace('//////////////////////');
var mess:String = 'a';
var myArray:Array = new Array(mess);
myArray.push(crc32(myArray));
trace('/////////////////////');
trace('// Verify CheckSum //');
trace('/////////////////////');
crc32(myArray);

function crc32(input:Array):String {
  var hexInput:String = '';
  for(var i:Number = 0; i < input[0].length; i++) {
      hexInput += dechex(input[0].charCodeAt(i));
  }
  var binaryInput:String = '';
  for(var i:Number = 0; i < hexInput.length; i++) {
      switch(hexInput.charCodeAt(i)) {
        case 48:
        binaryInput += '0000';
        break;
        case 49:
        binaryInput += '0001';
        break;
        case 50:
        binaryInput += '0010';
        break;
        case 51:
        binaryInput += '0011';
        break;
        case 52:
        binaryInput += '0100';
        break;
        case 53:
        binaryInput += '0101';
        break;
        case 54:
        binaryInput += '0110';
        break;
        case 55:
        binaryInput += '0111';
        break;
        case 56:
        binaryInput += '1000';
        break;
        case 57:
        binaryInput += '1001';
        break;
        case 97:
        binaryInput += '1010';
        break;
        case 98:
        binaryInput += '1011';
        break;
        case 99:
        binaryInput += '1100';
        break;
        case 100:
        binaryInput += '1101';
        break;
        case 101:
        binaryInput += '1110';
        break;
        case 102:
        binaryInput += '1111';
        break;
      }
  }
  var iterations:Number = binaryInput.length;
  var binaryGenerator:String = '100000100110000010001110110110111'; //Big-endian
  switch(input.length){
      case 1:
      for(var i:Number = 0; i < binaryGenerator.length - 1; i++){
        binaryInput += '0';
      }
      break;
      case 2:
      binaryInput += input[1];
      break;
      default:
      return 'Error';
      break;
  }
  for(var i:Number = 0; i < iterations; i++) {
      trace('  ' + binaryInput);
      hexInput = '';
      for(var j:Number = 0; j < i; j++){
        hexInput += ' ';
      }
      if(binaryInput.charAt(i) == '1') {
        trace('- ' + hexInput + binaryGenerator);
      }else{
        for(var j:Number = 0; j < binaryGenerator.length; j++){
            hexInput += '0';
        }
        trace('- ' + hexInput);
      }
      if(binaryInput.charAt(i) == '1') {
        for(var j:Number = 0; j < binaryGenerator.length; j++) {
            if(binaryInput.charAt(i + j) != binaryGenerator.charAt(j)){
              binaryInput = replace(binaryInput, i + j, '1');
            }else{
              binaryInput = replace(binaryInput, i + j, '0');
            }
        }
      }
      hexInput = '';
      for(var j:Number = 0; j < binaryInput.length; j++){
        hexInput += '-';
      }
      trace('  ' + hexInput);
  }
  trace('  ' + binaryInput);
  return binaryInput.substr(binaryInput.length - (binaryGenerator.length - 1));
}

function replace(original:String, index:Number, replacement:String):String {
  if(index == 0){
      return replacement + original.substr(1);
  }
  if(index == original.length - 1){
      return original.substr(0, original.length - 1) + replacement;
  }
  return original.substr(0, index) + replacement + original.substr(index + 1);
}

function dechex(val:Number):String {
  if(val < 0){
      val += Math.pow(2, 32);
  }
  var output:String = '';
  for(var i:Number = 0; i < 8; i++){
      var remainder:Number = val % 16;
      val = (val - remainder) / 16;
      switch(remainder){
        case 10:
        output = 'a' + output;
        break;
        case 11:
        output = 'b' + output;
        break;
        case 12:
        output = 'c' + output;
        break;
        case 13:
        output = 'd' + output;
        break;
        case 14:
        output = 'e' + output;
        break;
        case 15:
        output = 'f' + output;
        break;
        default:
        output = remainder + output;
        break;
      }
  }
  do{
      if(output.charCodeAt(0) == 48){
        output = output.substr(1, output.length - 1);
      }
  }while(output.charCodeAt(0) == 48);
  return output;
}

The output is:
:

//////////////////////
// Compute CheckSum //
//////////////////////
  0110000100000000000000000000000000000000
- 000000000000000000000000000000000
  ----------------------------------------
  0110000100000000000000000000000000000000
-  100000100110000010001110110110111
  ----------------------------------------
  0010000000110000010001110110110111000000
-  100000100110000010001110110110111
  ----------------------------------------
  0000000010101000011001001101101100100000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-        000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
/////////////////////
// Verify CheckSum //
/////////////////////
  0110000110101000011001001101101100100000
- 000000000000000000000000000000000
  ----------------------------------------
  0110000110101000011001001101101100100000
-  100000100110000010001110110110111
  ----------------------------------------
  0010000010011000001000111011011011100000
-  100000100110000010001110110110111
  ----------------------------------------
  0000000000000000000000000000000000000000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-        000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000

Why am I right? And yet SO wrong? :(


All times are GMT -5. The time now is 1:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC