JavaScript CRC32
I'm looking for a modern JavaScript implementation of CRC32.
This implementation, which may have originated from here, and is now here, there and everywhere, is unacceptable because it's slow (500ms/MB), and depends on over 2KB of space delimited table, accessed using substr. Yuck!
There appears to be a few variations of CRC32, so I need to match this output:
mysql> SELECT CRC32('abcde');
> 2240272485
Function doesn't actually need to accept a string however, since I'm working with byte arrays.
javascript checksum crc32
add a comment |
I'm looking for a modern JavaScript implementation of CRC32.
This implementation, which may have originated from here, and is now here, there and everywhere, is unacceptable because it's slow (500ms/MB), and depends on over 2KB of space delimited table, accessed using substr. Yuck!
There appears to be a few variations of CRC32, so I need to match this output:
mysql> SELECT CRC32('abcde');
> 2240272485
Function doesn't actually need to accept a string however, since I'm working with byte arrays.
javascript checksum crc32
5
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38
add a comment |
I'm looking for a modern JavaScript implementation of CRC32.
This implementation, which may have originated from here, and is now here, there and everywhere, is unacceptable because it's slow (500ms/MB), and depends on over 2KB of space delimited table, accessed using substr. Yuck!
There appears to be a few variations of CRC32, so I need to match this output:
mysql> SELECT CRC32('abcde');
> 2240272485
Function doesn't actually need to accept a string however, since I'm working with byte arrays.
javascript checksum crc32
I'm looking for a modern JavaScript implementation of CRC32.
This implementation, which may have originated from here, and is now here, there and everywhere, is unacceptable because it's slow (500ms/MB), and depends on over 2KB of space delimited table, accessed using substr. Yuck!
There appears to be a few variations of CRC32, so I need to match this output:
mysql> SELECT CRC32('abcde');
> 2240272485
Function doesn't actually need to accept a string however, since I'm working with byte arrays.
javascript checksum crc32
javascript checksum crc32
edited May 23 '17 at 11:33
Community♦
11
11
asked Sep 5 '13 at 14:21
Adria
4,60032523
4,60032523
5
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38
add a comment |
5
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38
5
5
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38
add a comment |
6 Answers
6
active
oldest
votes
Update
I added a helper function to create the CRCTable instead of having this enormous literal in the code. It could also be used to create the table once and save it in an object or variable and have the crc32 function use that (or as W3C's example, check for the existence and create if necessary). I also updated the jsPerf to compare using a CRCtable with the literal string, literal array, saved window variable and dynamic pointer (the example shown here).
var makeCRCTable = function(){
var c;
var crcTable = ;
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
}
var crc32 = function(str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Here's a link to the performance difference: http://jsperf.com/js-crc32
Well here's my ameatur shot at this. I figured reading off an array is faster than substringing it.
Warning though, I forwent the use of the Utf8Encode function in these examples to simplify the tests. After all, these are just examples and pretty rough ones at that.
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
@Alex Is there a reason for this code?:var crc = 0 ^ (-1);
Why is this diferrent from just-1
?
– ColBeseder
Nov 30 '15 at 8:00
|
show 7 more comments
The code you link to do a substring
and parses the result as number for each char. That's very inefficient.
I was able to make it 20 x faster with a simple optimization.
var a_table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
var b_table = a_table.split(' ').map(function(s){ return parseInt(s,16) });
function b_crc32 (str) {
var crc = -1;
for(var i=0, iTop=str.length; i<iTop; i++) {
crc = ( crc >>> 8 ) ^ b_table[( crc ^ str.charCodeAt( i ) ) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Performances comparison
JsBin to check it gives the same result
Small typo:var crc=crc ^ (-1)
should bevar crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
add a comment |
I needed this ASAP so I wrote my own. Hope someone finds it useful.
196 bytes (After closure compiler). 16ms/MB
Edit: With improvements from everyone’s input.
var crc32 = (function()
{
var table = new Uint32Array(256);
// Pre-generate crc32 polynomial lookup table
// http://wiki.osdev.org/CRC32#Building_the_Lookup_Table
// ... Actually use Alex's because it generates the correct bit order
// so no need for the reversal function
for(var i=256; i--;)
{
var tmp = i;
for(var k=8; k--;)
{
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
// crc32b
// Example input : [97, 98, 99, 100, 101] (Uint8Array)
// Example output : 2240272485 (Uint32)
return function( data )
{
var crc = -1; // Begin with all bits set ( 0xffffffff )
for(var i=0, l=data.length; i<l; i++)
{
crc = crc >>> 8 ^ table[ crc & 255 ^ data[i] ];
}
return (crc ^ -1) >>> 0; // Apply binary NOT
};
})();
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not usecharCodeAt()
? However, I see that you usedata.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)
– StanE
Mar 21 '16 at 12:10
What is the correct way to convert outputUint32
toUint8
? I useparseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
add a comment |
Based on @Denys Séguret's answer, I threw this together for a Node.js project:
(crc32-adler)
crc.js
:
/* jslint node: true */
'use strict';
const CRC32_TABLE =
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'.split(' ').map(s => parseInt(s, 16));
exports.CRC32 = class CRC32 {
constructor() {
this.crc = -1;
}
update(input) {
input = Buffer.isBuffer(input) ? input : Buffer.from(input, 'binary');
input.forEach(c => {
this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ c) & 0xff];
});
}
finalize() {
return (this.crc ^ (-1)) >>> 0;
}
};
You can then use this for streams and the like where data may come in chunks. Perform update
on each chunk then finalize
to get the result.
BTW: I found this to be a good source for checksum validation in addition to the built in *nix crc32
command.
Update:
An updated and much faster version can be found on my GitHub (too long to post here)
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
add a comment |
For ready-to-useness, here is a minified version of Alex's answer:
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
add a comment |
Dystroy optimized, but still Alex is faster. Note that making the string from the document makes an unfair benchmark as the document grows as the testing progresses, since Alex is last he was the most data to process.
Table setup optimization is great but has debatable effect on the use time execution.
http://jsperf.com/alex-variations
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18638900%2fjavascript-crc32%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Update
I added a helper function to create the CRCTable instead of having this enormous literal in the code. It could also be used to create the table once and save it in an object or variable and have the crc32 function use that (or as W3C's example, check for the existence and create if necessary). I also updated the jsPerf to compare using a CRCtable with the literal string, literal array, saved window variable and dynamic pointer (the example shown here).
var makeCRCTable = function(){
var c;
var crcTable = ;
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
}
var crc32 = function(str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Here's a link to the performance difference: http://jsperf.com/js-crc32
Well here's my ameatur shot at this. I figured reading off an array is faster than substringing it.
Warning though, I forwent the use of the Utf8Encode function in these examples to simplify the tests. After all, these are just examples and pretty rough ones at that.
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
@Alex Is there a reason for this code?:var crc = 0 ^ (-1);
Why is this diferrent from just-1
?
– ColBeseder
Nov 30 '15 at 8:00
|
show 7 more comments
Update
I added a helper function to create the CRCTable instead of having this enormous literal in the code. It could also be used to create the table once and save it in an object or variable and have the crc32 function use that (or as W3C's example, check for the existence and create if necessary). I also updated the jsPerf to compare using a CRCtable with the literal string, literal array, saved window variable and dynamic pointer (the example shown here).
var makeCRCTable = function(){
var c;
var crcTable = ;
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
}
var crc32 = function(str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Here's a link to the performance difference: http://jsperf.com/js-crc32
Well here's my ameatur shot at this. I figured reading off an array is faster than substringing it.
Warning though, I forwent the use of the Utf8Encode function in these examples to simplify the tests. After all, these are just examples and pretty rough ones at that.
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
@Alex Is there a reason for this code?:var crc = 0 ^ (-1);
Why is this diferrent from just-1
?
– ColBeseder
Nov 30 '15 at 8:00
|
show 7 more comments
Update
I added a helper function to create the CRCTable instead of having this enormous literal in the code. It could also be used to create the table once and save it in an object or variable and have the crc32 function use that (or as W3C's example, check for the existence and create if necessary). I also updated the jsPerf to compare using a CRCtable with the literal string, literal array, saved window variable and dynamic pointer (the example shown here).
var makeCRCTable = function(){
var c;
var crcTable = ;
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
}
var crc32 = function(str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Here's a link to the performance difference: http://jsperf.com/js-crc32
Well here's my ameatur shot at this. I figured reading off an array is faster than substringing it.
Warning though, I forwent the use of the Utf8Encode function in these examples to simplify the tests. After all, these are just examples and pretty rough ones at that.
Update
I added a helper function to create the CRCTable instead of having this enormous literal in the code. It could also be used to create the table once and save it in an object or variable and have the crc32 function use that (or as W3C's example, check for the existence and create if necessary). I also updated the jsPerf to compare using a CRCtable with the literal string, literal array, saved window variable and dynamic pointer (the example shown here).
var makeCRCTable = function(){
var c;
var crcTable = ;
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
}
var crc32 = function(str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Here's a link to the performance difference: http://jsperf.com/js-crc32
Well here's my ameatur shot at this. I figured reading off an array is faster than substringing it.
Warning though, I forwent the use of the Utf8Encode function in these examples to simplify the tests. After all, these are just examples and pretty rough ones at that.
edited Sep 5 '13 at 16:16
answered Sep 5 '13 at 15:11
Alex
1,0191020
1,0191020
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
@Alex Is there a reason for this code?:var crc = 0 ^ (-1);
Why is this diferrent from just-1
?
– ColBeseder
Nov 30 '15 at 8:00
|
show 7 more comments
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
@Alex Is there a reason for this code?:var crc = 0 ^ (-1);
Why is this diferrent from just-1
?
– ColBeseder
Nov 30 '15 at 8:00
1
1
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
Once it's fixed, it's the fastest in this page (same idea than mine but the literal table is probably optimized by js engines).
– Denys Séguret
Sep 5 '13 at 15:40
2
2
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
I'd upvote you but apparently I don't have the reputation :|
– Adria
Sep 5 '13 at 16:17
1
1
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
This is great! It also does work in IE8, as far as I can see - FYI.
– Lukx
Feb 21 '14 at 7:21
2
2
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
As code with long tables gets passed along on the net, somewhere along the way the table may get (intentionally?) corrupted. Initally my crc32 worked, until... it made a maistake. I replaced my copy/pasted table with your generator, and suddenly no more errors :-) Thanks
– Mike Tommasi
May 8 '14 at 7:03
2
2
@Alex Is there a reason for this code?:
var crc = 0 ^ (-1);
Why is this diferrent from just -1
?– ColBeseder
Nov 30 '15 at 8:00
@Alex Is there a reason for this code?:
var crc = 0 ^ (-1);
Why is this diferrent from just -1
?– ColBeseder
Nov 30 '15 at 8:00
|
show 7 more comments
The code you link to do a substring
and parses the result as number for each char. That's very inefficient.
I was able to make it 20 x faster with a simple optimization.
var a_table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
var b_table = a_table.split(' ').map(function(s){ return parseInt(s,16) });
function b_crc32 (str) {
var crc = -1;
for(var i=0, iTop=str.length; i<iTop; i++) {
crc = ( crc >>> 8 ) ^ b_table[( crc ^ str.charCodeAt( i ) ) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Performances comparison
JsBin to check it gives the same result
Small typo:var crc=crc ^ (-1)
should bevar crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
add a comment |
The code you link to do a substring
and parses the result as number for each char. That's very inefficient.
I was able to make it 20 x faster with a simple optimization.
var a_table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
var b_table = a_table.split(' ').map(function(s){ return parseInt(s,16) });
function b_crc32 (str) {
var crc = -1;
for(var i=0, iTop=str.length; i<iTop; i++) {
crc = ( crc >>> 8 ) ^ b_table[( crc ^ str.charCodeAt( i ) ) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Performances comparison
JsBin to check it gives the same result
Small typo:var crc=crc ^ (-1)
should bevar crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
add a comment |
The code you link to do a substring
and parses the result as number for each char. That's very inefficient.
I was able to make it 20 x faster with a simple optimization.
var a_table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
var b_table = a_table.split(' ').map(function(s){ return parseInt(s,16) });
function b_crc32 (str) {
var crc = -1;
for(var i=0, iTop=str.length; i<iTop; i++) {
crc = ( crc >>> 8 ) ^ b_table[( crc ^ str.charCodeAt( i ) ) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Performances comparison
JsBin to check it gives the same result
The code you link to do a substring
and parses the result as number for each char. That's very inefficient.
I was able to make it 20 x faster with a simple optimization.
var a_table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
var b_table = a_table.split(' ').map(function(s){ return parseInt(s,16) });
function b_crc32 (str) {
var crc = -1;
for(var i=0, iTop=str.length; i<iTop; i++) {
crc = ( crc >>> 8 ) ^ b_table[( crc ^ str.charCodeAt( i ) ) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
Performances comparison
JsBin to check it gives the same result
edited Apr 8 '16 at 6:03
answered Sep 5 '13 at 15:10
Denys Séguret
274k52576593
274k52576593
Small typo:var crc=crc ^ (-1)
should bevar crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
add a comment |
Small typo:var crc=crc ^ (-1)
should bevar crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
Small typo:
var crc=crc ^ (-1)
should be var crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
Small typo:
var crc=crc ^ (-1)
should be var crc=0 ^ (-1)
– Chris Hutchinson
Apr 7 '16 at 23:48
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
@ChrisHutchinson Yes. It doesn't change the result, but it's certainly cleaner!
– Denys Séguret
Apr 8 '16 at 6:04
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
thanks, btw made a version that only makes 1 global variable, b_crc32: gist.github.com/anonymous/028635b9dc2839b260a196d19405a1e1
– hanshenrik
Nov 30 '17 at 21:40
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 13:44
add a comment |
I needed this ASAP so I wrote my own. Hope someone finds it useful.
196 bytes (After closure compiler). 16ms/MB
Edit: With improvements from everyone’s input.
var crc32 = (function()
{
var table = new Uint32Array(256);
// Pre-generate crc32 polynomial lookup table
// http://wiki.osdev.org/CRC32#Building_the_Lookup_Table
// ... Actually use Alex's because it generates the correct bit order
// so no need for the reversal function
for(var i=256; i--;)
{
var tmp = i;
for(var k=8; k--;)
{
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
// crc32b
// Example input : [97, 98, 99, 100, 101] (Uint8Array)
// Example output : 2240272485 (Uint32)
return function( data )
{
var crc = -1; // Begin with all bits set ( 0xffffffff )
for(var i=0, l=data.length; i<l; i++)
{
crc = crc >>> 8 ^ table[ crc & 255 ^ data[i] ];
}
return (crc ^ -1) >>> 0; // Apply binary NOT
};
})();
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not usecharCodeAt()
? However, I see that you usedata.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)
– StanE
Mar 21 '16 at 12:10
What is the correct way to convert outputUint32
toUint8
? I useparseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
add a comment |
I needed this ASAP so I wrote my own. Hope someone finds it useful.
196 bytes (After closure compiler). 16ms/MB
Edit: With improvements from everyone’s input.
var crc32 = (function()
{
var table = new Uint32Array(256);
// Pre-generate crc32 polynomial lookup table
// http://wiki.osdev.org/CRC32#Building_the_Lookup_Table
// ... Actually use Alex's because it generates the correct bit order
// so no need for the reversal function
for(var i=256; i--;)
{
var tmp = i;
for(var k=8; k--;)
{
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
// crc32b
// Example input : [97, 98, 99, 100, 101] (Uint8Array)
// Example output : 2240272485 (Uint32)
return function( data )
{
var crc = -1; // Begin with all bits set ( 0xffffffff )
for(var i=0, l=data.length; i<l; i++)
{
crc = crc >>> 8 ^ table[ crc & 255 ^ data[i] ];
}
return (crc ^ -1) >>> 0; // Apply binary NOT
};
})();
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not usecharCodeAt()
? However, I see that you usedata.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)
– StanE
Mar 21 '16 at 12:10
What is the correct way to convert outputUint32
toUint8
? I useparseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
add a comment |
I needed this ASAP so I wrote my own. Hope someone finds it useful.
196 bytes (After closure compiler). 16ms/MB
Edit: With improvements from everyone’s input.
var crc32 = (function()
{
var table = new Uint32Array(256);
// Pre-generate crc32 polynomial lookup table
// http://wiki.osdev.org/CRC32#Building_the_Lookup_Table
// ... Actually use Alex's because it generates the correct bit order
// so no need for the reversal function
for(var i=256; i--;)
{
var tmp = i;
for(var k=8; k--;)
{
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
// crc32b
// Example input : [97, 98, 99, 100, 101] (Uint8Array)
// Example output : 2240272485 (Uint32)
return function( data )
{
var crc = -1; // Begin with all bits set ( 0xffffffff )
for(var i=0, l=data.length; i<l; i++)
{
crc = crc >>> 8 ^ table[ crc & 255 ^ data[i] ];
}
return (crc ^ -1) >>> 0; // Apply binary NOT
};
})();
I needed this ASAP so I wrote my own. Hope someone finds it useful.
196 bytes (After closure compiler). 16ms/MB
Edit: With improvements from everyone’s input.
var crc32 = (function()
{
var table = new Uint32Array(256);
// Pre-generate crc32 polynomial lookup table
// http://wiki.osdev.org/CRC32#Building_the_Lookup_Table
// ... Actually use Alex's because it generates the correct bit order
// so no need for the reversal function
for(var i=256; i--;)
{
var tmp = i;
for(var k=8; k--;)
{
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
// crc32b
// Example input : [97, 98, 99, 100, 101] (Uint8Array)
// Example output : 2240272485 (Uint32)
return function( data )
{
var crc = -1; // Begin with all bits set ( 0xffffffff )
for(var i=0, l=data.length; i<l; i++)
{
crc = crc >>> 8 ^ table[ crc & 255 ^ data[i] ];
}
return (crc ^ -1) >>> 0; // Apply binary NOT
};
})();
edited Oct 4 '14 at 1:34
answered Sep 5 '13 at 15:07
Adria
4,60032523
4,60032523
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not usecharCodeAt()
? However, I see that you usedata.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)
– StanE
Mar 21 '16 at 12:10
What is the correct way to convert outputUint32
toUint8
? I useparseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
add a comment |
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not usecharCodeAt()
? However, I see that you usedata.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)
– StanE
Mar 21 '16 at 12:10
What is the correct way to convert outputUint32
toUint8
? I useparseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
No it wouldn't, because it doesn't take a string
– Adria
Sep 5 '13 at 15:34
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
They can't be since there is no Array.charCodeAt
– Adria
Sep 5 '13 at 15:52
Does this work with unicode too, since it does not use
charCodeAt()
? However, I see that you use data.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)– StanE
Mar 21 '16 at 12:10
Does this work with unicode too, since it does not use
charCodeAt()
? However, I see that you use data.length
- does it return amount of bytes are chars (since there would be a difference with unicode chars)? Edit: Ah, ok I see, it expects an array! :-)– StanE
Mar 21 '16 at 12:10
What is the correct way to convert output
Uint32
to Uint8
? I use parseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
What is the correct way to convert output
Uint32
to Uint8
? I use parseInt(output.toString(16), 16)
– Vitaly Zdanevich
Nov 22 '18 at 13:01
add a comment |
Based on @Denys Séguret's answer, I threw this together for a Node.js project:
(crc32-adler)
crc.js
:
/* jslint node: true */
'use strict';
const CRC32_TABLE =
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'.split(' ').map(s => parseInt(s, 16));
exports.CRC32 = class CRC32 {
constructor() {
this.crc = -1;
}
update(input) {
input = Buffer.isBuffer(input) ? input : Buffer.from(input, 'binary');
input.forEach(c => {
this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ c) & 0xff];
});
}
finalize() {
return (this.crc ^ (-1)) >>> 0;
}
};
You can then use this for streams and the like where data may come in chunks. Perform update
on each chunk then finalize
to get the result.
BTW: I found this to be a good source for checksum validation in addition to the built in *nix crc32
command.
Update:
An updated and much faster version can be found on my GitHub (too long to post here)
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
add a comment |
Based on @Denys Séguret's answer, I threw this together for a Node.js project:
(crc32-adler)
crc.js
:
/* jslint node: true */
'use strict';
const CRC32_TABLE =
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'.split(' ').map(s => parseInt(s, 16));
exports.CRC32 = class CRC32 {
constructor() {
this.crc = -1;
}
update(input) {
input = Buffer.isBuffer(input) ? input : Buffer.from(input, 'binary');
input.forEach(c => {
this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ c) & 0xff];
});
}
finalize() {
return (this.crc ^ (-1)) >>> 0;
}
};
You can then use this for streams and the like where data may come in chunks. Perform update
on each chunk then finalize
to get the result.
BTW: I found this to be a good source for checksum validation in addition to the built in *nix crc32
command.
Update:
An updated and much faster version can be found on my GitHub (too long to post here)
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
add a comment |
Based on @Denys Séguret's answer, I threw this together for a Node.js project:
(crc32-adler)
crc.js
:
/* jslint node: true */
'use strict';
const CRC32_TABLE =
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'.split(' ').map(s => parseInt(s, 16));
exports.CRC32 = class CRC32 {
constructor() {
this.crc = -1;
}
update(input) {
input = Buffer.isBuffer(input) ? input : Buffer.from(input, 'binary');
input.forEach(c => {
this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ c) & 0xff];
});
}
finalize() {
return (this.crc ^ (-1)) >>> 0;
}
};
You can then use this for streams and the like where data may come in chunks. Perform update
on each chunk then finalize
to get the result.
BTW: I found this to be a good source for checksum validation in addition to the built in *nix crc32
command.
Update:
An updated and much faster version can be found on my GitHub (too long to post here)
Based on @Denys Séguret's answer, I threw this together for a Node.js project:
(crc32-adler)
crc.js
:
/* jslint node: true */
'use strict';
const CRC32_TABLE =
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'.split(' ').map(s => parseInt(s, 16));
exports.CRC32 = class CRC32 {
constructor() {
this.crc = -1;
}
update(input) {
input = Buffer.isBuffer(input) ? input : Buffer.from(input, 'binary');
input.forEach(c => {
this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ c) & 0xff];
});
}
finalize() {
return (this.crc ^ (-1)) >>> 0;
}
};
You can then use this for streams and the like where data may come in chunks. Perform update
on each chunk then finalize
to get the result.
BTW: I found this to be a good source for checksum validation in addition to the built in *nix crc32
command.
Update:
An updated and much faster version can be found on my GitHub (too long to post here)
edited Nov 23 '18 at 1:12
answered Oct 14 '16 at 3:00
NuSkooler
4,3332547
4,3332547
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
add a comment |
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
Why your table is not the same as here and here?
– Vitaly Zdanevich
Nov 22 '18 at 19:32
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
@Zdanevich there are variations of crc-32 algorithms. See mrob.com/pub/comp/crc-all.html for some examples.
– NuSkooler
Nov 22 '18 at 20:50
add a comment |
For ready-to-useness, here is a minified version of Alex's answer:
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
add a comment |
For ready-to-useness, here is a minified version of Alex's answer:
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
add a comment |
For ready-to-useness, here is a minified version of Alex's answer:
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
For ready-to-useness, here is a minified version of Alex's answer:
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
var crc32=function(r){for(var a,o=,c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
edited May 29 '18 at 10:31
answered May 29 '18 at 8:28
Basj
5,50429103226
5,50429103226
add a comment |
add a comment |
Dystroy optimized, but still Alex is faster. Note that making the string from the document makes an unfair benchmark as the document grows as the testing progresses, since Alex is last he was the most data to process.
Table setup optimization is great but has debatable effect on the use time execution.
http://jsperf.com/alex-variations
add a comment |
Dystroy optimized, but still Alex is faster. Note that making the string from the document makes an unfair benchmark as the document grows as the testing progresses, since Alex is last he was the most data to process.
Table setup optimization is great but has debatable effect on the use time execution.
http://jsperf.com/alex-variations
add a comment |
Dystroy optimized, but still Alex is faster. Note that making the string from the document makes an unfair benchmark as the document grows as the testing progresses, since Alex is last he was the most data to process.
Table setup optimization is great but has debatable effect on the use time execution.
http://jsperf.com/alex-variations
Dystroy optimized, but still Alex is faster. Note that making the string from the document makes an unfair benchmark as the document grows as the testing progresses, since Alex is last he was the most data to process.
Table setup optimization is great but has debatable effect on the use time execution.
http://jsperf.com/alex-variations
answered Dec 9 '13 at 4:05
Erich Horn
576
576
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18638900%2fjavascript-crc32%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
I made a speed comparison : Alex's function is the fastest one on V8 (probably because the literal array is optimized) : jsperf.com/dyscrc32
– Denys Séguret
Sep 5 '13 at 15:38