Skip to content

Instantly share code, notes, and snippets.

@svdamani
Created June 13, 2015 10:23
Show Gist options
  • Save svdamani/06b45ca1fafd85f73eab to your computer and use it in GitHub Desktop.
Save svdamani/06b45ca1fafd85f73eab to your computer and use it in GitHub Desktop.
CRC Implementation in JavaScript
function CRC() {
var table = new Array(256), /// LookUp Table for CRC
initial = 0xFFFFFFFF; /// Initial CRC
return {
/// Reverse a polynomial
reverse: function(poly) {
var rev = 0;
for(var i = 0; i < 32; i++) rev = rev << 1 | (poly >>> i) & 1;
return rev;
},
/// Populate lookup table using the polynomial
generate: function(poly) {
for(var i = 0; i < 256; i++) {
var n = i;
for(var j = 8; j > 0; j--) {
n >>>= 1;
if((n & 1) == 1) n ^= poly;
}
table[i] = n;
}
},
/// Compute the CRC of the given string using the polynomial
compute: function(poly, data) {
this.generate(poly);
var crc = initial;
for(var i = 0; i < data.length; i++)
crc = (crc >>> 8) ^ table[data.charCodeAt(i) ^ (crc & 0x000000FF)];
crc = ~crc;
return crc < 0 ? initial + crc + 1 : crc;
},
};
};
<!DOCTYPE html>
<html>
<head>
<script src="crc32.js"></script>
<title>CRC - 32</title>
</head>
<body>
<h2>CRC-32 Implementation in JavaScript</h2>
<p>This is a simple crc generator using data and polynomial.</p>
<p><b>Usage</b></p>
<ul>
<li>Enter data into the data field to get the 32 bit CRC</li>
<li>To change the polynomial, just edit the polynomial field (either normal or reversed)</li>
<li>To see the source code, <a href="crc32.js" target="_blank">click here</a></li>
</ul>
<form name="form1" action="javascript:void" autocomplete="off">
<table>
<tr>
<td>Polynomial (normal)</td>
<td>
<input type="text" name="polynomial" value="04C11DB7" onkeyup="onChangeNormal()" />
<button onclick="onResetClick()">Reset</button>
</td>
</tr>
<tr>
<td>Polynomial (reversed)</td>
<td><input type="text" name="polyReversed" value="EDB88320" onkeyup="onChangeReversed()" /></td>
</tr>
<tr>
<td>Input String</td>
<td><textarea name="data" rows="5" cols="80" onkeyup="onChange()" autofocus></textarea></td>
</tr>
<tr>
<td>Result (decimal)</td>
<td><input type="text" name="decResult" readonly /></td>
</tr>
<tr>
<td>Result (hexadecimal)</td>
<td><input type="text" name="hexResult" readonly /></td>
</tr>
</table>
</form>
<script>
String.prototype.repeat = function(times) {
return (new Array(times + 1)).join(this);
}
Number.prototype.toHex = function(len) {
len = len || 8;
var num = this < 0 ? (0xFFFFFFFF + this + 1) : this;
var hex = num.toString(16).toUpperCase();
var pad = hex.length < len ? len - hex.length : 0;
return "0".repeat(pad) + hex;
}
var form = document.form1,
crc = new CRC();
function onChangeNormal() {
var polynomial = parseInt(form.polynomial.value, 16);
form.polyReversed.value = polynomial ? crc.reverse(polynomial).toHex() : "";
onChange();
}
function onChangeReversed() {
var reversed = parseInt(form.polyReversed.value, 16);
form.polynomial.value = reversed ? crc.reverse(reversed).toHex() : "";
onChange();
}
function onChange() {
var reversed = parseInt(form.polyReversed.value, 16);
var data = form.data.value;
var c = crc.compute(reversed, data);
form.decResult.value = data ? c : "";
form.hexResult.value = data ? c.toHex() : "";
}
function onResetClick() {
form.polynomial.value = "04C11DB7";
onChangeNormal();
form.data.focus();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment