Advertisement
King0fGamesYami

Lua Hashing 256

Apr 9th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.96 KB | None | 0 0
  1. -- SHA-256 code in Lua 5.2; based on the pseudo-code from
  2. -- Wikipedia (http://en.wikipedia.org/wiki/SHA-2)
  3.  
  4.  
  5. local band, rrotate, bxor, rshift, bnot =
  6.   bit32.band, bit32.rrotate, bit32.bxor, bit32.rshift, bit32.bnot
  7.  
  8. local string, setmetatable, assert = string, setmetatable, assert
  9.  
  10. _ENV = nil
  11.  
  12. -- Initialize table of round constants
  13. -- (first 32 bits of the fractional parts of the cube roots of the first
  14. -- 64 primes 2..311):
  15. local k = {
  16.    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  17.    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  18.    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  19.    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  20.    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  21.    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  22.    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  23.    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  24.    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  25.    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  26.    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  27.    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  28.    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  29.    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  30.    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  31.    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  32. }
  33.  
  34.  
  35. -- transform a string of bytes in a string of hexadecimal digits
  36. local function str2hexa (s)
  37.   local h = string.gsub(s, ".", function(c)
  38.               return string.format("%02x", string.byte(c))
  39.             end)
  40.   return h
  41. end
  42.  
  43.  
  44. -- transform number 'l' in a big-endian sequence of 'n' bytes
  45. -- (coded as a string)
  46. local function num2s (l, n)
  47.   local s = ""
  48.   for i = 1, n do
  49.     local rem = l % 256
  50.     s = string.char(rem) .. s
  51.     l = (l - rem) / 256
  52.   end
  53.   return s
  54. end
  55.  
  56. -- transform the big-endian sequence of four bytes starting at
  57. -- index 'i' in 's' into a number
  58. local function s232num (s, i)
  59.   local n = 0
  60.   for i = i, i + 3 do
  61.     n = n*256 + string.byte(s, i)
  62.   end
  63.   return n
  64. end
  65.  
  66.  
  67. -- append the bit '1' to the message
  68. -- append k bits '0', where k is the minimum number >= 0 such that the
  69. -- resulting message length (in bits) is congruent to 448 (mod 512)
  70. -- append length of message (before pre-processing), in bits, as 64-bit
  71. -- big-endian integer
  72. local function preproc (msg, len)
  73.   local extra = 64 - ((len + 1 + 8) % 64)
  74.   len = num2s(8 * len, 8)    -- original len in bits, coded
  75.   msg = msg .. "\128" .. string.rep("\0", extra) .. len
  76.   assert(#msg % 64 == 0)
  77.   return msg
  78. end
  79.  
  80.  
  81. local function initH224 (H)
  82.   -- (second 32 bits of the fractional parts of the square roots of the
  83.   -- 9th through 16th primes 23..53)
  84.   H[1] = 0xc1059ed8
  85.   H[2] = 0x367cd507
  86.   H[3] = 0x3070dd17
  87.   H[4] = 0xf70e5939
  88.   H[5] = 0xffc00b31
  89.   H[6] = 0x68581511
  90.   H[7] = 0x64f98fa7
  91.   H[8] = 0xbefa4fa4
  92.   return H
  93. end
  94.  
  95.  
  96. local function initH256 (H)
  97.   -- (first 32 bits of the fractional parts of the square roots of the
  98.   -- first 8 primes 2..19):
  99.   H[1] = 0x6a09e667
  100.   H[2] = 0xbb67ae85
  101.   H[3] = 0x3c6ef372
  102.   H[4] = 0xa54ff53a
  103.   H[5] = 0x510e527f
  104.   H[6] = 0x9b05688c
  105.   H[7] = 0x1f83d9ab
  106.   H[8] = 0x5be0cd19
  107.   return H
  108. end
  109.  
  110.  
  111. local function digestblock (msg, i, H)
  112.  
  113.     -- break chunk into sixteen 32-bit big-endian words w[1..16]
  114.     local w = {}
  115.     for j = 1, 16 do
  116.       w[j] = s232num(msg, i + (j - 1)*4)
  117.     end
  118.  
  119.     -- Extend the sixteen 32-bit words into sixty-four 32-bit words:
  120.     for j = 17, 64 do
  121.       local v = w[j - 15]
  122.       local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  123.       v = w[j - 2]
  124.       local s1 = bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  125.       w[j] = w[j - 16] + s0 + w[j - 7] + s1
  126.     end
  127.  
  128.     -- Initialize hash value for this chunk:
  129.     local a, b, c, d, e, f, g, h =
  130.         H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  131.  
  132.     -- Main loop:
  133.     for i = 1, 64 do
  134.       local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  135.       local maj = bxor(band(a, b), band(a, c), band(b, c))
  136.       local t2 = s0 + maj
  137.       local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  138.       local ch = bxor (band(e, f), band(bnot(e), g))
  139.       local t1 = h + s1 + ch + k[i] + w[i]
  140.  
  141.       h = g
  142.       g = f
  143.       f = e
  144.       e = d + t1
  145.       d = c
  146.       c = b
  147.       b = a
  148.       a = t1 + t2
  149.     end
  150.  
  151.     -- Add (mod 2^32) this chunk's hash to result so far:
  152.     H[1] = band(H[1] + a)
  153.     H[2] = band(H[2] + b)
  154.     H[3] = band(H[3] + c)
  155.     H[4] = band(H[4] + d)
  156.     H[5] = band(H[5] + e)
  157.     H[6] = band(H[6] + f)
  158.     H[7] = band(H[7] + g)
  159.     H[8] = band(H[8] + h)
  160.  
  161. end
  162.  
  163.  
  164. local function finalresult224 (H)
  165.   -- Produce the final hash value (big-endian):
  166.   return
  167.     str2hexa(num2s(H[1], 4)..num2s(H[2], 4)..num2s(H[3], 4)..num2s(H[4], 4)..
  168.              num2s(H[5], 4)..num2s(H[6], 4)..num2s(H[7], 4))
  169. end
  170.  
  171.  
  172. local function finalresult256 (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)..num2s(H[8], 4))
  177. end
  178.  
  179.  
  180. ----------------------------------------------------------------------
  181. local HH = {}    -- to reuse
  182.  
  183. local function hash224 (msg)
  184.   msg = preproc(msg, #msg)
  185.   local H = initH224(HH)
  186.  
  187.   -- Process the message in successive 512-bit (64 bytes) chunks:
  188.   for i = 1, #msg, 64 do
  189.     digestblock(msg, i, H)
  190.   end
  191.  
  192.   return finalresult224(H)
  193. end
  194.  
  195.  
  196. local function hash256 (msg)
  197.   msg = preproc(msg, #msg)
  198.   local H = initH256(HH)
  199.  
  200.   -- Process the message in successive 512-bit (64 bytes) chunks:
  201.   for i = 1, #msg, 64 do
  202.     digestblock(msg, i, H)
  203.   end
  204.  
  205.   return finalresult256(H)
  206. end
  207. ----------------------------------------------------------------------
  208. local mt = {}
  209.  
  210. local function new256 ()
  211.   local o = {H = initH256({}), msg = "", len = 0}
  212.   setmetatable(o, mt)
  213.   return o
  214. end
  215.  
  216. mt.__index = mt
  217.  
  218. function mt:add (m)
  219.   self.msg = self.msg .. m
  220.   self.len = self.len + #m
  221.   local t = 0
  222.   while #self.msg - t >= 64 do
  223.     digestblock(self.msg, t + 1, self.H)
  224.     t = t + 64
  225.   end
  226.   self.msg = self.msg:sub(t + 1, -1)
  227. end
  228.  
  229.  
  230. function mt:close ()
  231.   self.msg = preproc(self.msg, self.len)
  232.   self:add("")
  233.   return finalresult256(self.H)
  234. end
  235. ----------------------------------------------------------------------
  236.  
  237. return {
  238.   hash224 = hash224,
  239.   hash256 = hash256,
  240.   new256 = new256,
  241. }
  242. The following script performs some basic tests over the previous implementation and shows how to use the library:
  243.  
  244. -- tests for SHA-2 in Lua 5.2
  245.  
  246. local sha2 = require 'sha2'
  247.  
  248. -- a few examples from the Web
  249.  
  250. assert(sha2.hash224"The quick brown fox jumps over the lazy dog" ==
  251.   "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525")
  252.  
  253. assert(sha2.hash224"" ==
  254.   "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
  255.  
  256. assert(sha2.hash256"The quick brown fox jumps over the lazy dog" ==
  257.   "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")
  258.  
  259. assert(sha2.hash256"The quick brown fox jumps over the lazy cog" ==
  260.   "e4c4d8f3bf76b692de791a173e05321150f7a345b46484fe427f6acc7ecc81be")
  261.  
  262. assert(sha2.hash256"" ==
  263.   "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  264.  
  265. assert(sha2.new256():close() ==
  266.   "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  267.  
  268. assert(sha2.hash256"123456" ==
  269.   "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92")
  270.  
  271.  
  272. -- most other examples here are checked against a "correct" answer
  273. -- given by 'sha224sum'/'sha256sum'
  274.  
  275.  
  276. -- border cases (sizes around 64 bytes)
  277.  
  278. assert(sha2.hash256(string.rep('a', 62) .. '\n') ==
  279.   "290b30a68148b3ee27ab7b744c297a5d986c1011938a09e73058430593bf83f0")
  280. assert(sha2.hash256(string.rep('a', 63) .. '\n') ==
  281.   "a229eaed30f4991d1fcdab77c70b604efd780502c82be0732b310811dc43b2b3")
  282. assert(sha2.hash256(string.rep('a', 64) .. '\n') ==
  283.   "44c2336fedab8ff6a85c74c2b94165377b0981f526adb9487895ca6314165e86")
  284. assert(sha2.hash256(string.rep('a', 65) .. '\n') ==
  285.   "574883a9977284a46845620eaa55c3fa8209eaa3ebffe44774b6eb2dba2cb325")
  286.  
  287. local x = sha2.new256()
  288. for i = 1, 65 do x:add('a') end
  289. x:add('\n')
  290. assert(x:close() ==
  291.   "574883a9977284a46845620eaa55c3fa8209eaa3ebffe44774b6eb2dba2cb325")
  292.  
  293.  
  294. -- some large files
  295. local function parts (s, j)
  296.   local x = sha2.new256()
  297.   local i = 1; j = 1
  298.   while i <= #s do
  299.     x:add(s:sub(i, i + j))
  300.     i = i + j + 1
  301.   end
  302.   return x:close()
  303. end
  304.  
  305. -- 80 lines of 80 '0's each
  306. local s = string.rep('0', 80) .. '\n'
  307. s = string.rep(s, 80)
  308. assert(parts(s, 70) ==
  309.   "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  310. assert(parts(s, 7) ==
  311.   "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  312. assert(parts(s, #s + 10) ==
  313.   "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  314.  
  315.  
  316.  
  317.  
  318. -- read a file and prints its hash, if given a file name
  319.  
  320. if arg[1] then
  321.   local file = assert(io.open (arg[1], 'rb'))
  322.   local x = sha2.new256()
  323.   for b in file:lines(2^12) do
  324.     x:add(b)
  325.   end
  326.   file:close()
  327.   print(x:close())
  328. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement