Advertisement
Anavrins

BLAKE-256

Feb 3rd, 2016 (edited)
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | None | 0 0
  1. -- BLAKE-256 hash function in ComputerCraft
  2. -- By Anavrins
  3. -- For help and details, you can DM me on Discord (Anavrins#4600)
  4. -- MIT License
  5. -- http://pastebin.com/XRy3LMBG
  6. -- Last update: May 13, 2019
  7.  
  8. local mod = 2^32
  9. local bor = bit32.bor
  10. local band = bit32.band
  11. local bxor = bit32.bxor
  12. local blshift = bit32.lshift
  13. local brshift = bit32.arshift
  14.  
  15. local function rrotate(n, b)
  16.     local s = n/(2^b)
  17.     local f = s%1
  18.     return (s-f) + f*mod
  19. end
  20.  
  21. local const = {[0]=
  22.     0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344,
  23.     0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89,
  24.     0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C,
  25.     0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917
  26. }
  27.  
  28. local iv = {[0]=
  29.     0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  30.     0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
  31. }
  32.  
  33. local sigma = {[0]=
  34.     {[0]= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15},
  35.     {[0]=14,10, 4, 8, 9,15,13, 6, 1,12, 0, 2,11, 7, 5, 3},
  36.     {[0]=11, 8,12, 0, 5, 2,15,13,10,14, 3, 6, 7, 1, 9, 4},
  37.     {[0]= 7, 9, 3, 1,13,12,11,14, 2, 6, 5,10, 4, 0,15, 8},
  38.     {[0]= 9, 0, 5, 7, 2, 4,10,15,14, 1,11,12, 6, 8, 3,13},
  39.     {[0]= 2,12, 6,10, 0,11, 8, 3, 4,13, 7, 5,15,14, 1, 9},
  40.     {[0]=12, 5, 1,15,14,13, 4,10, 0, 7, 6, 3, 9, 2, 8,11},
  41.     {[0]=13,11, 7,14,12, 1, 3, 9, 5, 0,15, 4, 8, 6, 2,10},
  42.     {[0]= 6,15,14, 9,11, 3, 0, 8,12, 2,13, 7, 1, 4,10, 5},
  43.     {[0]=10, 2, 8, 4, 7, 6, 1, 5,15,11, 9,14, 3,12,13, 0}
  44. }
  45.  
  46. local function quarterRound(s, a, b, c, d, m, r, i)
  47.     local j, k = sigma[r%10][2*i], sigma[r%10][(2*i)+1]
  48.     s[a] = (s[a]+(s[b]+(bxor(m[j], const[k]))))%mod
  49.     s[d] = rrotate(bxor(s[d], s[a]), 16)
  50.     s[c] = (s[c]+s[d])%mod
  51.     s[b] = rrotate(bxor(s[b], s[c]), 12)
  52.     s[a] = (s[a]+(s[b]+(bxor(m[k], const[j]))))%mod
  53.     s[d] = rrotate(bxor(s[d], s[a]), 8)
  54.     s[c] = (s[c]+s[d])%mod
  55.     s[b] = rrotate(bxor(s[b], s[c]), 7)
  56.     return s
  57. end
  58.  
  59. local function compress(h, m, s)
  60.     local v = {[0]=
  61.         h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7],
  62.         h[ 8], h[ 9], h[10], h[11], h[12], h[13], h[14], h[15]
  63.     }
  64.     for r = 0, 13 do
  65.         v = quarterRound(v, 0, 4, 8,12, m, r, 0)
  66.         v = quarterRound(v, 1, 5, 9,13, m, r, 1)
  67.         v = quarterRound(v, 2, 6,10,14, m, r, 2)
  68.         v = quarterRound(v, 3, 7,11,15, m, r, 3)
  69.         v = quarterRound(v, 0, 5,10,15, m, r, 4)
  70.         v = quarterRound(v, 1, 6,11,12, m, r, 5)
  71.         v = quarterRound(v, 2, 7, 8,13, m, r, 6)
  72.         v = quarterRound(v, 3, 4, 9,14, m, r, 7)
  73.     end
  74.     for i = 0, 7 do
  75.         h[i] = bxor(bxor(bxor(h[i], v[i]), v[i+8]), s[i] or s[i-4])
  76.     end
  77.     return h
  78. end
  79.  
  80. local function BE_toInt(bs, i)
  81.     return blshift((bs[i+1] or 0), 24) + blshift((bs[i+2] or 0), 16) + blshift((bs[i+3] or 0), 8) + (bs[i+4] or 0)
  82. end
  83.  
  84. local function incr(t, incr)
  85.     if 0xFFFFFFFF - t[0] < incr then
  86.         t[1] = t[1] + 1
  87.         t[0] = incr - (0xFFFFFFFF - t[0]) - 1      
  88.     else
  89.         t[0] = t[0] + incr
  90.     end
  91.     return t
  92. end
  93.  
  94. local mt = {
  95.     __tostring = function(a) return string.char(unpack(a)) end,
  96.     __index = {
  97.         toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
  98.         isEqual = function(self, t)
  99.             if type(t) ~= "table" then return false end
  100.             if #self ~= #t then return false end
  101.             local ret = 0
  102.             for i = 1, #self do
  103.                 ret = bor(ret, bxor(self[i], t[i]))
  104.             end
  105.             return ret == 0
  106.         end
  107.     }
  108. }
  109.  
  110. local function toBytes(t, n)
  111.     local b = {}
  112.     for i = 1, n do
  113.         b[(i-1)*4+1] = band(brshift(t[i], 24), 0xFF)
  114.         b[(i-1)*4+2] = band(brshift(t[i], 16), 0xFF)
  115.         b[(i-1)*4+3] = band(brshift(t[i], 8), 0xFF)
  116.         b[(i-1)*4+4] = band(t[i], 0xFF)
  117.     end
  118.     return setmetatable(b, mt)
  119. end
  120.  
  121. local function digest(data, salt)
  122.     local data = type(data) == "string" and {data:byte(1,-1)} or data
  123.     local salt = type(salt) == "string" and {salt:byte(1,-1)} or salt
  124.     local salt = salt or {}
  125.  
  126.     local m = {}
  127.     local v = {}
  128.     local t = {[0]=0,0}
  129.     local h = {[0]=iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]}
  130.     local s = {[0]=BE_toInt(salt, 0), BE_toInt(salt, 4), BE_toInt(salt, 8), BE_toInt(salt,12)}
  131.  
  132.     local mLen = #data
  133.     data[#data+1] = 0x80
  134.     while #data%64~=56 do data[#data+1] = 0 end
  135.     data[#data] = bor(data[#data], 1)
  136.     t = incr(t, mLen*8)
  137.     for i = 1, 0, -1 do
  138.         data[#data+1] = band(brshift(t[i], 24), 0xFF)
  139.         data[#data+1] = band(brshift(t[i], 16), 0xFF)
  140.         data[#data+1] = band(brshift(t[i], 8), 0xFF)
  141.         data[#data+1] = band(t[i], 0xFF)
  142.     end
  143.  
  144.     t[0], t[1] = 0, 0
  145.  
  146.     local blockAmt = math.ceil(#data/64)
  147.     for i = 0, blockAmt-1 do
  148.         for j = 0, 15 do m[j] = BE_toInt(data, (64*i)+4*j) end
  149.  
  150.         if mLen == 0 then t[0], t[1] = 0, 0
  151.         elseif (mLen-64) >= 0 then
  152.             t = incr(t, 512)
  153.             mLen = mLen-64
  154.         elseif (mLen-64) < 0 then
  155.             t = incr(t, mLen*8)
  156.             mLen = 0
  157.         end
  158.  
  159.         v = {[0]=
  160.             h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
  161.             bxor(s[0], const[0]), bxor(s[1], const[1]), bxor(s[2], const[2]), bxor(s[3], const[3]),
  162.             bxor(t[0], const[4]), bxor(t[0], const[5]), bxor(t[1], const[6]), bxor(t[1], const[7]),
  163.         }
  164.  
  165.         h = compress(v, m, s)
  166.     end
  167.     return toBytes({h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]}, 8)
  168. end
  169.  
  170. return {
  171.     digest = digest,
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement