Created
December 6, 2022 09:32
-
-
Save thewhodidthis/e74ed815f5cfdd7e66fcc1d0853c3f7a to your computer and use it in GitHub Desktop.
Common hashing algorithms in JS
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
export function crc32c(data, n = data.length, poly = 0xEDB88320) { | |
let crc = 0xFFFFFFFF | |
for (let i = 0; i < n; i += 1) { | |
crc = crc ^ data.charCodeAt(i) | |
for (let j = 0; j < 8; j += 1) { | |
crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1 | |
} | |
} | |
return crc ^ 0xFFFFFFFF | |
} | |
export const CRC_TABLE = Array.from({ length: 256 }, (_, i) => { | |
for (let j = 0; j < 8; j += 1) { | |
i = (i & 0x01 ? 0xEDB88320 ^ (i >>> 1) : (i >>> 1)) | |
} | |
return i | |
}) | |
export function crc32(data = "", n = data.length) { | |
const crc = data.split("").slice(0, n).reduce((a, b) => { | |
const c = b.charCodeAt(0) | |
return CRC_TABLE[(c ^ a) & 0xFF] ^ (a >>> 8) | |
}, -1) | |
return (-1 ^ crc) >>> 0 | |
} | |
export function adler32(data = "", n = data.length) { | |
const MOD_ADLER = 65521 | |
let a = 1 | |
let b = 0 | |
data.slice(0, n).split("").forEach((c) => { | |
a = (a + c.charCodeAt(0)) % MOD_ADLER | |
b = (b + a) % MOD_ADLER | |
}) | |
return (b << 16) | a | |
} | |
export function fletcher16(data, n = data.length) { | |
let a = 0 | |
let b = 0 | |
data.slice(0, n).split("").forEach((c) => { | |
a = (a + c.charCodeAt(0)) % 255 | |
b = (b + a) % 255 | |
}) | |
return (b << 8) | a | |
} | |
export function fletcher32(data = "", l = data.length) { | |
const encoder = new TextEncoder("utf-8") | |
// Round up buffer length if odd to begin with. | |
const m = l % 2 === 0 ? l : l + 1 | |
const buffer = new ArrayBuffer(m) | |
const uint8 = new Uint8Array(buffer) | |
const uint16 = new Uint16Array(uint8.buffer) | |
encoder.encodeInto(data, uint8) | |
let c0 = 0 | |
let c1 = 0 | |
uint16.forEach((v) => { | |
c0 = (c0 + v) % 65535 | |
c1 = (c1 + c0) % 65535 | |
}) | |
return Uint32Array.from([c1 << 16 | c0]).at(-1) | |
} |
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
import { adler32, crc32c, crc32, fletcher16, fletcher32 } from "./main.js" | |
console.assert(crc32c("The quick brown fox jumps over the lazy dog").toString(16) === "414fa339") | |
console.assert(crc32("The quick brown fox jumps over the lazy dog").toString(16) === "414fa339") | |
console.assert(adler32("SheetJS") === 176947863) | |
// https://stackoverflow.com/questions/21496806/fletcher32-is-the-limit-of-360-in-wikipedia-wrong | |
console.assert("fletcher32", fletcher32("abcde") === 4031760169) | |
console.assert("fletcher32", fletcher32("abcdef") === 1448095018) | |
console.assert("fletcher32", fletcher32("abcdefgh") === 3957429649) | |
console.assert("fletcher32", fletcher32("abcdefgh").toString(16) === 0xEBE19591) | |
console.assert("fletcher32", fletcher32("abcdef").toString(16) === 0x56502D2A) | |
console.assert("fletcher32", fletcher32("abcde").toString(16) === "f04fc729") | |
console.assert(fletcher16("abcde") === 51440) | |
console.assert(fletcher16("abcdef") === 8279) | |
console.assert(fletcher16("abcdefgh") === 1575) | |
console.assert(fletcher16("abcdefgh").toString(16) === "627") | |
console.assert(fletcher16("abcdef").toString(16) === "2057") | |
console.assert(fletcher16("abcde").toString(16) === "c8f0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment