SHARE
TWEET

MD5

Anavrins Mar 22nd, 2016 (edited) 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- MD5 hash function in ComputerCraft
  2. -- Insecure, for educational uses only
  3. -- By Anavrins
  4. -- For help and details, you can PM me on the CC forums
  5. -- You may use this code in your projects without asking me, as long as credit is given and this header is kept intact
  6. -- http://www.computercraft.info/forums2/index.php?/user/12870-anavrins
  7. -- http://pastebin.com/6PVSRckQ
  8. -- Last update: May 13, 2019
  9.  
  10. local mod32 = 2^32
  11. local bor = bit32.bor
  12. local band = bit32.band
  13. local bnot = bit32.bnot
  14. local bxor = bit32.bxor
  15. local blshift = bit32.lshift
  16. local upack = unpack
  17.  
  18. local function lrotate(int, by)
  19.     local s = int/(2^(32-by))
  20.     local f = s%1
  21.     return (s-f)+f*mod32
  22. end
  23. local function brshift(int, by) -- Thanks bit32 for bad rshift
  24.     local s = int / (2^by)
  25.     return s-s%1
  26. end
  27.  
  28. local s = {
  29.      7, 12, 17, 22,
  30.      5,  9, 14, 20,
  31.      4, 11, 16, 23,
  32.      6, 10, 15, 21,
  33. }
  34.  
  35. local K = {
  36.     0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  37.     0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  38.     0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  39.     0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  40.     0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  41.     0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  42.     0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  43.     0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  44.     0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  45.     0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  46.     0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  47.     0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  48.     0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  49.     0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  50.     0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  51.     0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
  52. }
  53.  
  54. local H = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}
  55.  
  56. local function counter(incr)
  57.     local t1, t2 = 0, 0
  58.     if 0xFFFFFFFF - t1 < incr then
  59.         t2 = t2 + 1
  60.         t1 = incr - (0xFFFFFFFF - t1) - 1      
  61.     else t1 = t1 + incr
  62.     end
  63.     return t2, t1
  64. end
  65.  
  66. local function LE_toInt(bs, i)
  67.     return (bs[i] or 0) + blshift((bs[i+1] or 0), 8) + blshift((bs[i+2] or 0), 16) + blshift((bs[i+3] or 0), 24)
  68. end
  69.  
  70. local function preprocess(data)
  71.     local len = #data
  72.     local proc = {}
  73.     data[#data+1] = 0x80
  74.     while #data%64~=56 do data[#data+1] = 0 end
  75.     local blocks = math.ceil(#data/64)
  76.     for i = 1, blocks do
  77.         proc[i] = {}
  78.         for j = 1, 16 do
  79.             proc[i][j] = LE_toInt(data, 1+((i-1)*64)+((j-1)*4))
  80.         end
  81.     end
  82.     proc[blocks][16], proc[blocks][15] = counter(len*8)
  83.     return proc
  84. end
  85.  
  86. local function digestblock(m, C)
  87.     local a, b, c, d = upack(C)
  88.     for j = 0, 63 do
  89.         local f, g, r = 0, j, brshift(j, 4)
  90.         if r == 0 then
  91.             f = bor(band(b, c), band(bnot(b), d))
  92.         elseif r == 1 then
  93.             f = bor(band(d, b), band(bnot(d), c))
  94.             g = (5*j+1)%16
  95.         elseif r == 2 then
  96.             f = bxor(b, bxor(c, d))
  97.             g = (3*j+5)%16
  98.         elseif r == 3 then
  99.             f = bxor(c, bor(b, bnot(d)))
  100.             g = (7*j)%16
  101.         end
  102.         local dTemp = d
  103.         a, b, c, d = dTemp, (b+lrotate((a + f + K[j+1] + m[g+1])%mod32, s[bor(blshift(r, 2), band(j, 3))+1]))%mod32, b, c
  104.     end
  105.     C[1] = (C[1] + a)%mod32
  106.     C[2] = (C[2] + b)%mod32
  107.     C[3] = (C[3] + c)%mod32
  108.     C[4] = (C[4] + d)%mod32
  109.     return C
  110. end
  111.  
  112. local mt = {
  113.     __tostring = function(a) return string.char(unpack(a)) end,
  114.     __index = {
  115.         toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
  116.         isEqual = function(self, t)
  117.             if type(t) ~= "table" then return false end
  118.             if #self ~= #t then return false end
  119.             local ret = 0
  120.             for i = 1, #self do
  121.                 ret = bor(ret, bxor(self[i], t[i]))
  122.             end
  123.             return ret == 0
  124.         end
  125.     }
  126. }
  127.  
  128. local function toBytes(t, n)
  129.     local b = {}
  130.     for i = 1, n do
  131.         b[(i-1)*4+1] = band(t[i], 0xFF)
  132.         b[(i-1)*4+2] = band(brshift(t[i], 8), 0xFF)
  133.         b[(i-1)*4+3] = band(brshift(t[i], 16), 0xFF)
  134.         b[(i-1)*4+4] = band(brshift(t[i], 24), 0xFF)
  135.     end
  136.     return setmetatable(b, mt)
  137. end
  138.  
  139. function digest(data)
  140.     data = data or ""
  141.     data = type(data) == "string" and {data:byte(1,-1)} or data
  142.  
  143.     data = preprocess(data)
  144.     local C = {upack(H)}
  145.     for i = 1, #data do C = digestblock(data[i], C) end
  146.     return toBytes(C, 4)
  147. end
  148.  
  149. return {
  150.     digest = digest,
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top