CRC32 Calculator (Javascript)


CRC (Cyclic Redundancy Checksum)  is a quick method to compute a value (or sometimes known as hash), that can be used to represent the data integrity. CRC32 computes a 32-bit integer (signed or unsigned) from a given source (text, string).

CRC32 is easy to implement. First, define a table of 256 values. The table can be found [here]. Then for each element from source, do the following operation

r = (r >> 8) ^ CRCTable[str[i] ^ (r & 0x000000FF)];

where r stores the intermediate result.

A Javascript implementation can be found online at https://rot47.net/crc32.html

However, since a 32-bit integer can only store tex_b86dd186dd89d62998268e510619b63c CRC32 Calculator (Javascript) algorithms hash implementation javascript programming languages values. The CRC32 algorithm of course can not avoid conflicts (two different sources have the same CRC32 hash value). However, the major purpose of the CRC32 algorithm is to ensure the data integrity (changing minors of source will yield a totally different hash value).

CRC32 is NOT used in security areas (storing passwords)  such as MD5 where data conflicting is of importance.

The following is a Javascript source code (only a partial table is given) that gives the CRC32 in a function, which can be used easily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
    CRC32.js
    compute Cyclic redundancy check
    Dr justyy
    https://rot47.net/crc32.html
*/
 
var CRC32 = function(str){
    var CRCTable = [
        0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
        0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
        // lots of data leading up to 256 elements ...
    ];
    var len = str.length;
    var r = 0xffffffff;
    for (var i = 0; i < len; i ++) {        
        r = (r >> 8) ^ CRCTable[str[i] ^ (r & 0x000000FF)];
    }
    return ~r;
}
/*
	CRC32.js
	compute Cyclic redundancy check
	Dr justyy
	https://rot47.net/crc32.html
*/

var CRC32 = function(str){
	var CRCTable = [
		0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
		0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
		// lots of data leading up to 256 elements ...
	];
	var len = str.length;
	var r = 0xffffffff;
	for (var i = 0; i < len; i ++) { 		
		r = (r >> 8) ^ CRCTable[str[i] ^ (r & 0x000000FF)];
	}
	return ~r;
}

The Javascript CRC32 implementation together with HTML can be found at [here]

References:

[1] http://en.wikipedia.org/wiki/Cryptographic_hash_function

[2] http://en.wikipedia.org/wiki/MD5

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
468 words
Last Post: Codeforces: 347 B. Fixed Points
Next Post: Picture Explorer, Sort Pictures by Facebook Likes

The Permanent URL is: CRC32 Calculator (Javascript)

Leave a Reply