-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc32c.js
50 lines (43 loc) · 1.07 KB
/
crc32c.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
44
45
46
47
48
49
50
'use strict';
const CRC32Generator = require('./crc32generator.js');
var CRC32C = function(algorithm) {
if (! algorithm) {
algorithm = CRC32C.algorithms[0];
}
if (CRC32C.algorithms.indexOf(algorithm) < 0) {
throw new Error('Unsupported algorithm');
}
this.algorithm = algorithm;
this.defaultEncoding = 'hex-with-prefix';
this.finalized = false;
this.length = 0;
this.crc = new CRC32Generator(-2097792136);
};
CRC32C.algorithms = [ 'crc32c' ];
CRC32C.prototype.update = function(b) {
if (this.finalized) {
throw new Error('Checksum context in finalized state');
}
this.crc.update(b);
this.length = this.crc.length;
return this;
};
CRC32C.prototype.digest = function(encoding) {
var r;
r = this.crc.digest(encoding);
if (! this.finalized) {
this.state = this.crc.state;
this.finalized = true;
}
if (encoding === 'default') {
encoding = this.defaultEncoding;
}
return r;
};
CRC32C.prototype.final = function(encoding) {
if (this.finalized) {
throw new Error('Checksum context in finalized state');
}
return this.digest(encoding);
};
module.exports = CRC32C;