jille_Jr

SHA2 implementation (sha2.lua)

Nov 12th, 2020 (edited)
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.55 KB | None | 0 0
  1. -- Taken from http://lua-users.org/wiki/SecureHashAlgorithm
  2. -- The code here was originally written by Roberto Ierusalimschy,
  3. -- and is licensed under MIT (see http://lua-users.org/lists/lua-l/2014-08/msg00628.html)
  4.  
  5. -- SHA-256 code in Lua 5.2; based on the pseudo-code from
  6. -- Wikipedia (http://en.wikipedia.org/wiki/SHA-2)
  7.  
  8. local band, rrotate, bxor, rshift, bnot =
  9.   bit32.band, bit32.rrotate, bit32.bxor, bit32.rshift, bit32.bnot
  10.  
  11. local string, setmetatable, assert = string, setmetatable, assert
  12.  
  13. _ENV = nil
  14.  
  15. -- Initialize table of round constants
  16. -- (first 32 bits of the fractional parts of the cube roots of the first
  17. -- 64 primes 2..311):
  18. local k = {
  19.    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  20.    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  21.    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  22.    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  23.    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  24.    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  25.    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  26.    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  27.    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  28.    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  29.    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  30.    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  31.    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  32.    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  33.    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  34.    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  35. }
  36.  
  37.  
  38. -- transform a string of bytes in a string of hexadecimal digits
  39. local function str2hexa (s)
  40.   local h = string.gsub(s, ".", function(c)
  41.               return string.format("%02x", string.byte(c))
  42.             end)
  43.   return h
  44. end
  45.  
  46.  
  47. -- transform number 'l' in a big-endian sequence of 'n' bytes
  48. -- (coded as a string)
  49. local function num2s (l, n)
  50.   local s = ""
  51.   for i = 1, n do
  52.     local rem = l % 256
  53.     s = string.char(rem) .. s
  54.     l = (l - rem) / 256
  55.   end
  56.   return s
  57. end
  58.  
  59. -- transform the big-endian sequence of four bytes starting at
  60. -- index 'i' in 's' into a number
  61. local function s232num (s, i)
  62.   local n = 0
  63.   for i = i, i + 3 do
  64.     n = n*256 + string.byte(s, i)
  65.   end
  66.   return n
  67. end
  68.  
  69.  
  70. -- append the bit '1' to the message
  71. -- append k bits '0', where k is the minimum number >= 0 such that the
  72. -- resulting message length (in bits) is congruent to 448 (mod 512)
  73. -- append length of message (before pre-processing), in bits, as 64-bit
  74. -- big-endian integer
  75. local function preproc (msg, len)
  76.   local extra = -(len + 1 + 8) % 64
  77.   len = num2s(8 * len, 8)    -- original len in bits, coded
  78.   msg = msg .. "\128" .. string.rep("\0", extra) .. len
  79.   assert(#msg % 64 == 0)
  80.   return msg
  81. end
  82.  
  83.  
  84. local function initH224 (H)
  85.   -- (second 32 bits of the fractional parts of the square roots of the
  86.   -- 9th through 16th primes 23..53)
  87.   H[1] = 0xc1059ed8
  88.   H[2] = 0x367cd507
  89.   H[3] = 0x3070dd17
  90.   H[4] = 0xf70e5939
  91.   H[5] = 0xffc00b31
  92.   H[6] = 0x68581511
  93.   H[7] = 0x64f98fa7
  94.   H[8] = 0xbefa4fa4
  95.   return H
  96. end
  97.  
  98.  
  99. local function initH256 (H)
  100.   -- (first 32 bits of the fractional parts of the square roots of the
  101.   -- first 8 primes 2..19):
  102.   H[1] = 0x6a09e667
  103.   H[2] = 0xbb67ae85
  104.   H[3] = 0x3c6ef372
  105.   H[4] = 0xa54ff53a
  106.   H[5] = 0x510e527f
  107.   H[6] = 0x9b05688c
  108.   H[7] = 0x1f83d9ab
  109.   H[8] = 0x5be0cd19
  110.   return H
  111. end
  112.  
  113.  
  114. local function digestblock (msg, i, H)
  115.  
  116.     -- break chunk into sixteen 32-bit big-endian words w[1..16]
  117.     local w = {}
  118.     for j = 1, 16 do
  119.       w[j] = s232num(msg, i + (j - 1)*4)
  120.     end
  121.  
  122.     -- Extend the sixteen 32-bit words into sixty-four 32-bit words:
  123.     for j = 17, 64 do
  124.       local v = w[j - 15]
  125.       local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  126.       v = w[j - 2]
  127.       local s1 = bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  128.       w[j] = w[j - 16] + s0 + w[j - 7] + s1
  129.     end
  130.  
  131.     -- Initialize hash value for this chunk:
  132.     local a, b, c, d, e, f, g, h =
  133.         H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  134.  
  135.     -- Main loop:
  136.     for i = 1, 64 do
  137.       local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  138.       local maj = bxor(band(a, b), band(a, c), band(b, c))
  139.       local t2 = s0 + maj
  140.       local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  141.       local ch = bxor (band(e, f), band(bnot(e), g))
  142.       local t1 = h + s1 + ch + k[i] + w[i]
  143.  
  144.       h = g
  145.       g = f
  146.       f = e
  147.       e = d + t1
  148.       d = c
  149.       c = b
  150.       b = a
  151.       a = t1 + t2
  152.     end
  153.  
  154.     -- Add (mod 2^32) this chunk's hash to result so far:
  155.     H[1] = band(H[1] + a)
  156.     H[2] = band(H[2] + b)
  157.     H[3] = band(H[3] + c)
  158.     H[4] = band(H[4] + d)
  159.     H[5] = band(H[5] + e)
  160.     H[6] = band(H[6] + f)
  161.     H[7] = band(H[7] + g)
  162.     H[8] = band(H[8] + h)
  163.  
  164. end
  165.  
  166.  
  167. local function finalresult224 (H)
  168.   -- Produce the final hash value (big-endian):
  169.   return
  170.     str2hexa(num2s(H[1], 4)..num2s(H[2], 4)..num2s(H[3], 4)..num2s(H[4], 4)..
  171.              num2s(H[5], 4)..num2s(H[6], 4)..num2s(H[7], 4))
  172. end
  173.  
  174.  
  175. local function finalresult256 (H)
  176.   -- Produce the final hash value (big-endian):
  177.   return
  178.     str2hexa(num2s(H[1], 4)..num2s(H[2], 4)..num2s(H[3], 4)..num2s(H[4], 4)..
  179.              num2s(H[5], 4)..num2s(H[6], 4)..num2s(H[7], 4)..num2s(H[8], 4))
  180. end
  181.  
  182.  
  183. ----------------------------------------------------------------------
  184. local HH = {}    -- to reuse
  185.  
  186. local function hash224 (msg)
  187.   msg = preproc(msg, #msg)
  188.   local H = initH224(HH)
  189.  
  190.   -- Process the message in successive 512-bit (64 bytes) chunks:
  191.   for i = 1, #msg, 64 do
  192.     digestblock(msg, i, H)
  193.   end
  194.  
  195.   return finalresult224(H)
  196. end
  197.  
  198.  
  199. local function hash256 (msg)
  200.   msg = preproc(msg, #msg)
  201.   local H = initH256(HH)
  202.  
  203.   -- Process the message in successive 512-bit (64 bytes) chunks:
  204.   for i = 1, #msg, 64 do
  205.     digestblock(msg, i, H)
  206.   end
  207.  
  208.   return finalresult256(H)
  209. end
  210. ----------------------------------------------------------------------
  211. local mt = {}
  212.  
  213. local function new256 ()
  214.   local o = {H = initH256({}), msg = "", len = 0}
  215.   setmetatable(o, mt)
  216.   return o
  217. end
  218.  
  219. mt.__index = mt
  220.  
  221. function mt:add (m)
  222.   self.msg = self.msg .. m
  223.   self.len = self.len + #m
  224.   local t = 0
  225.   while #self.msg - t >= 64 do
  226.     digestblock(self.msg, t + 1, self.H)
  227.     t = t + 64
  228.   end
  229.   self.msg = self.msg:sub(t + 1, -1)
  230. end
  231.  
  232.  
  233. function mt:close ()
  234.   self.msg = preproc(self.msg, self.len)
  235.   self:add("")
  236.   return finalresult256(self.H)
  237. end
  238. ----------------------------------------------------------------------
  239.  
  240. return {
  241.   hash224 = hash224,
  242.   hash256 = hash256,
  243.   new256 = new256,
  244. }
Advertisement
Add Comment
Please, Sign In to add comment