Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Created August 18, 2022 13:10
Show Gist options
  • Save cxmeel/5f220d1c387aaa49f40e1e9e72dc616e to your computer and use it in GitHub Desktop.
Save cxmeel/5f220d1c387aaa49f40e1e9e72dc616e to your computer and use it in GitHub Desktop.
--!strict
local POLYNOMIAL = 0xEDB88320
local HASH_TABLE = {}
for index = 0, 255 do
local value = index
for _ = 1, 8 do
if value % 2 == 1 then
value = bit32.bxor(POLYNOMIAL, bit32.rshift(value, 1))
else
value = bit32.rshift(value, 1)
end
end
HASH_TABLE[index] = value
end
local function CreateCRC32Hash(value: string): string
local hash = 0xFFFFFFFF
for index = 1, #value do
local character = string.byte(value, index)
hash = bit32.bxor(HASH_TABLE[bit32.band(bit32.bxor(hash, character), 0xFF)], bit32.rshift(hash, 8))
end
return string.format("%08x", bit32.bxor(hash, 0xFFFFFFFF))
end
local function VerifyCRC32Hash(value: string, hash: string): boolean
return CreateCRC32Hash(value) == hash
end
return {
hash = CreateCRC32Hash,
verify = VerifyCRC32Hash,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment