-
Notifications
You must be signed in to change notification settings - Fork 34
/
crc32.js
43 lines (38 loc) · 917 Bytes
/
crc32.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// //////////////////////////////////////////////////////////
// crc32.js
// Copyright (c) 2014 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//
function hex(what)
{
// adjust negative numbers
if (what < 0)
what = 0xFFFFFFFF + what + 1;
// convert to hexadecimal string
var result = what.toString(16);
// add leading zeros
return ('0000000' + result).slice(-8);
}
function crc32_bitwise(text)
{
// CRC32b polynomial
var Polynomial = 0xEDB88320;
// start value
var crc = 0xFFFFFFFF;
for (var i = 0; i < text.length; i++)
{
// XOR next byte into state
crc ^= text.charCodeAt(i);
// process 8 bits
for (var bit = 0; bit < 8; bit++)
{
// look at lowest bit
if ((crc & 1) != 0)
crc = (crc >>> 1) ^ Polynomial;
else
crc = crc >>> 1;
}
}
// return hex string
return hex(~crc);
}