Advertisement
Anavrins

MD5

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