jille_Jr

[CC] Lua secure hashing

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