Advertisement
osmarks

CRC

Jan 12th, 2019
2,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.61 KB | None | 0 0
  1. local CRC = {}
  2.  
  3. local xor = bit.bxor
  4. local rshift = bit.brshift
  5. local band = bit.band
  6.  
  7. function CRC.table()
  8.     if CRC._table == nil then
  9.         local tab = {}
  10.         for i = 0, 255 do
  11.             local v = i
  12.             for _ = 1, 8 do
  13.                 if v % 2 == 1 then
  14.                     v = xor(0xEDB88320, rshift(v, 1))
  15.                 else
  16.                     v = rshift(v, 1)
  17.                 end
  18.             end
  19.             tab[i] = v
  20.         end
  21.         CRC._table = tab
  22.     end
  23.     return CRC._table
  24. end
  25.  
  26. function CRC.hash(v)
  27.     local tab = CRC.table()
  28.     local hash = 0xFFFFFFFF
  29.     for i = 1, #v do
  30.         hash = xor(tab[band(xor(hash, v:byte(i)), 0xFF)], rshift(hash, 8))
  31.     end
  32.     return xor(hash, 0xFFFFFFFF)
  33. end
  34.  
  35. return CRC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement