Advertisement
theinsekt

sha256copied

Sep 9th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. --copied almost exactly from http://pastebin.com/gsFrNjbt
  2. --the only change is that the function sha256 isn't local
  3. --so that you can use this file as an api
  4. --everything below is the copy:
  5.  
  6.  
  7. --  
  8. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  9. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  10. --  
  11. --  Using an adapted version of the bit library
  12. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  13. --  
  14.  
  15. local MOD = 2^32
  16. local MODM = MOD-1
  17.  
  18. local function memoize(f)
  19.     local mt = {}
  20.     local t = setmetatable({}, mt)
  21.     function mt:__index(k)
  22.         local v = f(k)
  23.         t[k] = v
  24.         return v
  25.     end
  26.     return t
  27. end
  28.  
  29. local function make_bitop_uncached(t, m)
  30.     local function bitop(a, b)
  31.         local res,p = 0,1
  32.         while a ~= 0 and b ~= 0 do
  33.             local am, bm = a % m, b % m
  34.             res = res + t[am][bm] * p
  35.             a = (a - am) / m
  36.             b = (b - bm) / m
  37.             p = p*m
  38.         end
  39.         res = res + (a + b) * p
  40.         return res
  41.     end
  42.     return bitop
  43. end
  44.  
  45. local function make_bitop(t)
  46.     local op1 = make_bitop_uncached(t,2^1)
  47.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  48.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  49. end
  50.  
  51. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  52.  
  53. local function bxor(a, b, c, ...)
  54.     local z = nil
  55.     if b then
  56.         a = a % MOD
  57.         b = b % MOD
  58.         z = bxor1(a, b)
  59.         if c then z = bxor(z, c, ...) end
  60.         return z
  61.     elseif a then return a % MOD
  62.     else return 0 end
  63. end
  64.  
  65. local function band(a, b, c, ...)
  66.     local z
  67.     if b then
  68.         a = a % MOD
  69.         b = b % MOD
  70.         z = ((a + b) - bxor1(a,b)) / 2
  71.         if c then z = bit32_band(z, c, ...) end
  72.         return z
  73.     elseif a then return a % MOD
  74.     else return MODM end
  75. end
  76.  
  77. local function bnot(x) return (-1 - x) % MOD end
  78.  
  79. local function rshift1(a, disp)
  80.     if disp < 0 then return lshift(a,-disp) end
  81.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  82. end
  83.  
  84. local function rshift(x, disp)
  85.     if disp > 31 or disp < -31 then return 0 end
  86.     return rshift1(x % MOD, disp)
  87. end
  88.  
  89. local function lshift(a, disp)
  90.     if disp < 0 then return rshift(a,-disp) end
  91.     return (a * 2 ^ disp) % 2 ^ 32
  92. end
  93.  
  94. local function rrotate(x, disp)
  95.     x = x % MOD
  96.     disp = disp % 32
  97.     local low = band(x, 2 ^ disp - 1)
  98.     return rshift(x, disp) + lshift(low, 32 - disp)
  99. end
  100.  
  101. local k = {
  102.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  103.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  104.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  105.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  106.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  107.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  108.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  109.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  110.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  111.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  112.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  113.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  114.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  115.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  116.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  117.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  118. }
  119.  
  120. local function str2hexa(s)
  121.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  122. end
  123.  
  124. local function num2s(l, n)
  125.     local s = ""
  126.     for i = 1, n do
  127.         local rem = l % 256
  128.         s = string.char(rem) .. s
  129.         l = (l - rem) / 256
  130.     end
  131.     return s
  132. end
  133.  
  134. local function s232num(s, i)
  135.     local n = 0
  136.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  137.     return n
  138. end
  139.  
  140. local function preproc(msg, len)
  141.     local extra = 64 - ((len + 9) % 64)
  142.     len = num2s(8 * len, 8)
  143.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  144.     assert(#msg % 64 == 0)
  145.     return msg
  146. end
  147.  
  148. local function initH256(H)
  149.     H[1] = 0x6a09e667
  150.     H[2] = 0xbb67ae85
  151.     H[3] = 0x3c6ef372
  152.     H[4] = 0xa54ff53a
  153.     H[5] = 0x510e527f
  154.     H[6] = 0x9b05688c
  155.     H[7] = 0x1f83d9ab
  156.     H[8] = 0x5be0cd19
  157.     return H
  158. end
  159.  
  160. local function digestblock(msg, i, H)
  161.     local w = {}
  162.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  163.     for j = 17, 64 do
  164.         local v = w[j - 15]
  165.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  166.         v = w[j - 2]
  167.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  168.     end
  169.  
  170.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  171.     for i = 1, 64 do
  172.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  173.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  174.         local t2 = s0 + maj
  175.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  176.         local ch = bxor (band(e, f), band(bnot(e), g))
  177.         local t1 = h + s1 + ch + k[i] + w[i]
  178.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  179.     end
  180.  
  181.     H[1] = band(H[1] + a)
  182.     H[2] = band(H[2] + b)
  183.     H[3] = band(H[3] + c)
  184.     H[4] = band(H[4] + d)
  185.     H[5] = band(H[5] + e)
  186.     H[6] = band(H[6] + f)
  187.     H[7] = band(H[7] + g)
  188.     H[8] = band(H[8] + h)
  189. end
  190.  
  191. function sha256(msg)
  192.     msg = preproc(msg, #msg)
  193.     local H = initH256({})
  194.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  195.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  196.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement