Advertisement
Tatantyler

ECC Functions

May 3rd, 2013
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.99 KB | None | 0 0
  1. function adler32(input)
  2.     local a = 1
  3.     local b = 0
  4.     for i=1, #input do
  5.         a = (a + input[i]) % 65521
  6.         b = (b + a) % 65521
  7.     end
  8.     return (b * 65536) + a
  9. end
  10.  
  11. function fletcher32(input)
  12.     local a,b = 0, 0
  13.     for i=1, #input, 2 do
  14.         a = (a + (input[i]*256) + input[i+1]) % 65535
  15.         b = (b + a) % 65535
  16.     end
  17.     return (b * 65536) + a
  18. end
  19.  
  20. function crc16(input) -- poly = 0x1021 (CRC-16-CCITT)
  21.     local rem = 0
  22.     for i=1, #input do
  23.         rem = bit.bxor(rem, bit.blshift(input[i], 8))
  24.         for j=1, 8 do
  25.             rem = bit.blshift(rem, 1)
  26.             if bit.band(rem, 0x8000) > 0 then
  27.                 rem = bit.bxor(rem, 0x1021)
  28.             end
  29.             rem = bit.band(rem, 0xFFFF)
  30.         end
  31.     end
  32.     return rem
  33. end
  34.  
  35. function crc32(input) -- poly = 0x04C11DB7 (CRC-32)
  36.     local rem = 0
  37.     for i=1, #input do
  38.         rem = bit.bxor(rem, bit.blshift(input[i], 24))
  39.         for j=1, 8 do
  40.             rem = bit.blshift(rem, 1)
  41.             if bit.band(rem, 0x80000000) > 0 then
  42.                 rem = bit.bxor(rem, 0x04C11DB7)
  43.             end
  44.             rem = bit.band(rem, 0xFFFFFFFF)
  45.         end
  46.     end
  47.     return rem
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement