Last active
October 4, 2023 05:29
-
-
Save SakuraHentai/812dfb32585e6dce92a7e4717371d572 to your computer and use it in GitHub Desktop.
crc64
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
// ref: https://github.com/torvalds/linux/blob/master/lib/crc64.c#L51 | |
function crc64(str) { | |
const CRC64_ECMA182_POLY = 0x42f0e1eba9ea3693n | |
// CRC64 lookup table | |
const crc64Table = [] | |
for (let i = 0n; i < 256n; i++) { | |
let crc = 0n | |
let c = i << 56n | |
for (let j = 0n; j < 8n; j++) { | |
if (BigInt(crc ^ c) & 0x8000000000000000n) | |
crc = (crc << 1n) ^ CRC64_ECMA182_POLY | |
else crc <<= 1n | |
c <<= 1n | |
} | |
crc64Table[i] = crc & 0xffffffffffffffffn | |
} | |
// console.log(crc64Table.map((crc) => crc.toString(16))) | |
let crc = BigInt(0xffffffffffffffff) | |
for (let i = 0; i < str.length; i++) { | |
const byte = str.charCodeAt(i) | |
const crcIdx = ((crc >> 56n) ^ BigInt(byte)) & 0xffn | |
crc = (crc << 8n) ^ crc64Table[Number(crcIdx)] | |
} | |
return crc & 0xffffffffffffffffn | |
} | |
const input = 'abcdef' | |
const crc = crc64(input).toString(16) | |
console.log(crc) |
NOTE: I am not sure if its the version of Webpack I was using but I had to change all the numbers with the 'n' suffix to use the BigInt constructor ie BigInt(8)
Hello, you may need to use https://babeljs.io/docs/babel-plugin-syntax-bigint to support the 'n' suffix syntax.
Thanks for the Tip. I would rather make a slight change to the code than have to introduce yet another step to the "build" process.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: I am not sure if its the version of Webpack I was using but I had to change all the numbers with the 'n' suffix to use the BigInt constructor ie BigInt(8)