-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { strict as assert } from "node:assert"; | ||
import test, { describe } from "node:test"; | ||
|
||
import { crc32 } from "./crc32.js"; | ||
|
||
describe("crc32", () => { | ||
test("empty string", () => { | ||
const result = crc32(""); | ||
assert.equal(result, 0); | ||
}); | ||
|
||
test('"123456789"', () => { | ||
const result = crc32("123456789"); | ||
const expected = 0xCBF43926; | ||
assert.equal(result, expected); | ||
}); | ||
|
||
test('"Hello, World!"', () => { | ||
const result = crc32("Hello, World!"); | ||
const expected = 0xEC4AC3D0; | ||
assert.equal(result, expected); | ||
}); | ||
|
||
test("a long string", () => { | ||
const longString = "a".repeat(1000); | ||
const result = crc32(longString); | ||
const expected = 0x9A38DA03; | ||
assert.equal(result, expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Precomputed CRC32 lookup table | ||
const LOOKUP_TABLE = new Uint32Array(256); | ||
|
||
for (let i = 0; i < LOOKUP_TABLE.length; i++) { | ||
let crc = i; | ||
for (let j = 0; j < 8; j++) { | ||
crc = (crc >>> 1) ^ (0xEDB88320 * (crc & 1)); | ||
} | ||
LOOKUP_TABLE[i] = crc; | ||
} | ||
|
||
export function crc32(data: string): number { | ||
let crc = 0 ^ (-1); | ||
const encoded = new TextEncoder().encode(data); | ||
|
||
for (let i = 0; i < encoded.length; i++) { | ||
crc = (crc >>> 8) ^ LOOKUP_TABLE[(crc ^ encoded[i]) & 0xFF]; | ||
} | ||
|
||
return (crc ^ (-1)) >>> 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters