Skip to content

Instantly share code, notes, and snippets.

@CompeyDev
Last active December 26, 2024 06:22
Show Gist options
  • Select an option

  • Save CompeyDev/3c6d5917b2a4024723605f5c656599b1 to your computer and use it in GitHub Desktop.

Select an option

Save CompeyDev/3c6d5917b2a4024723605f5c656599b1 to your computer and use it in GitHub Desktop.
An example CRC32 implementation in pure luau.
-- This is a dysfunctional PoC for a luau implementation of crc32.
local POLY = 0x04C11DB7
local crc32 = {}
local crc32_t = {}
setmetatable(crc32_t, {
__call = function (crc32_t, ...): {[number]: number}
local c = 1
for i = 1, 256, 1 do
i = c
for j = 1, 8, 1 do
if bit32.band(c, 1) then
c = bit32.bxor(POLY, bit32.rshift(c, 1))
else
c = bit32.rshift(c, 1)
end
end
crc32_t[i] = c
end
return crc32_t
end
})
function crc32.compute_hash(msg: string, msg_len: number, crc: number?)
local crc32_t = crc32_t()
local crc_c = bit32.bnot(crc or 0)
for i = 1, msg_len, 1 do
crc_c = bit32.bxor(bit32.rshift(crc_c, 8), crc32_t[bit32.bxor(crc_c, bit32.band(msg:byte(i), 0xFF))])
end
crc_c = bit32.bxor(bit32.bnot(crc_c), 0)
return string.format("%x", crc_c)
end
return crc32
local crc32 = require("crc32")
local hashed = crc32.compute_hash("Hello, world!", 13)
print(hashed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment