Advertisement
Tatantyler

SHA1

Jul 29th, 2013
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. -- SHA1
  2. -- By KillaVanilla
  3.  
  4. local function Preprocessing(msg)
  5.     local msgCp = {}
  6.     local lastPause = os.clock()
  7.     for i=1, #msg do
  8.         msgCp[i] = msg[i]
  9.         if (os.clock() - lastPause) >= 2.90 then
  10.             os.queueEvent("")
  11.             os.pullEvent("")
  12.             lastPause = os.clock()
  13.         end
  14.     end
  15.     local len = #msgCp*8
  16.     local bits = #msgCp*8
  17.     table.insert(msgCp, 0x80)
  18.     while true do
  19.         if bits % 512 == 448 then
  20.             break
  21.         else
  22.             table.insert(msgCp, 0)
  23.             bits = #msgCp*8
  24.         end
  25.     end
  26.     table.insert(msgCp, len)
  27.     return msgCp
  28. end
  29.  
  30. local function breakMsg(message)
  31.     local chunks = {}
  32.     local chunk = 1
  33.     for word=1, #message, 16 do
  34.         chunks[chunk] = {}
  35.         for i2=0, 15 do
  36.             table.insert(chunks[chunk], message[word+i2] or 0)
  37.         end
  38.         chunk = chunk+1
  39.     end
  40.     return chunks
  41. end
  42.  
  43. function leftrotate(a, i)
  44.     local bitmask = (2^i)-1
  45.     local shiftOut = bit.band(a, bit.blshift(bitmask, 32-i))
  46.     if bit.band(shiftOut, 0x80000000) > 0 then -- gotta work around the arithmetic right shift
  47.         --[[
  48.         shiftOut = bit.brshift(shiftOut, 1)
  49.         shiftOut = bit.band(shiftOut, 0x7FFFFFFF)
  50.         shiftOut = bit.brshift(shiftOut, 31-i)
  51.         ]]
  52.         shiftOut = bit.band(bit.brshift(shiftOut, 32-i), bitmask)
  53.     else
  54.         shiftOut = bit.brshift(shiftOut, 32-i)
  55.     end
  56.     local b = bit.band(bit.blshift(a, i), 0xFFFFFFFF)
  57.     b = bit.bor(b, shiftOut)
  58.     return (b  % (2^32))
  59. end
  60.  
  61. function digest(bytes, debugFile)
  62.     bytes = Preprocessing(bytes)
  63.     local chunks = breakMsg(bytes)
  64.     local h = {}
  65.     h[0] = 0x67452301
  66.     h[1] = 0xEFCDAB89
  67.     h[2] = 0x98BADCFE
  68.     h[3] = 0x10325476
  69.     h[4] = 0xC3D2E1F0
  70.     local debug = false
  71.     if debugFile then
  72.         debug = fs.open(debugFile, "w")
  73.     end
  74.     local pauseTimer1 = os.clock()
  75.     for chunkN=1, #chunks do
  76.         local w = {}
  77.         for i=0, 15 do
  78.             w[i] = chunks[chunkN][i+1]
  79.         end
  80.         local pauseTimer2 = os.clock()
  81.         for i=16, 79 do
  82.             w[i] = bit.bxor( bit.bxor(w[i-3], w[i-8]), bit.bxor(w[i-14], w[i-16]) ) % (2^32)
  83.             w[i] = leftrotate(w[i], 1)
  84.             if (os.clock() - pauseTimer2) >= 2.5 then
  85.                 os.queueEvent("")
  86.                 os.pullEvent("")
  87.                 pauseTimer2 = os.clock()
  88.             end
  89.         end
  90.         local a = h[0]
  91.         local b = h[1]
  92.         local c = h[2]
  93.         local d = h[3]
  94.         local e = h[4]
  95.         local pauseTimer3 = os.clock()
  96.         for i=0, 79 do
  97.             local f = 0
  98.             local k = 0
  99.             if (i >= 0) and (i <= 19) then
  100.                 f = bit.bor(bit.band(a, c), bit.band(bit.bnot(b), d)) % (2^32)
  101.                 k = 0x5A827999
  102.             elseif (i >= 20) and (i <= 39) then
  103.                 f = bit.bxor(b, bit.bxor(c, d)) % (2^32)
  104.                 k = 0x6ED9EBA1
  105.             elseif (i >= 40) and (i <= 59) then
  106.                 f = bit.bor(bit.band(b, c), bit.bor(bit.band(b, d), bit.band(c, d))) % (2^32)
  107.                 k = 0x8F1BBCDC
  108.             elseif (i >= 60) and (i <= 79) then
  109.                 f = bit.bxor(b, bit.bxor(c, d)) % (2^32)
  110.                 k = 0xCA62C1D6
  111.             end
  112.             local temp = (leftrotate(a, 5)+f+e+k+w[i]) % (2^32)
  113.             e = d
  114.             d = c
  115.             c = leftrotate(b, 30)
  116.             b = a
  117.             a = temp
  118.             if (os.clock() - pauseTimer3) >= 2.80 then
  119.                 os.queueEvent("")
  120.                 os.pullEvent("")
  121.                 pauseTimer3 = os.clock()
  122.             end
  123.         end
  124.         h[0] = (h[0]+a) % (2^32)
  125.         h[1] = (h[1]+b) % (2^32)
  126.         h[2] = (h[2]+c) % (2^32)
  127.         h[3] = (h[3]+d) % (2^32)
  128.         h[4] = (h[4]+e) % (2^32)
  129.         if debug then
  130.             debug.writeLine(chunkN..": "..string.format("%X", h[0]).."|"..string.format("%X", h[1]).."|"..string.format("%X", h[2]).."|"..string.format("%X", h[3]).."|"..string.format("%X", h[4]))
  131.         end
  132.         if (os.clock() - pauseTimer1) >= 2.80 then
  133.             os.queueEvent("")
  134.             os.pullEvent("")
  135.             pauseTimer1 = os.clock()
  136.         end
  137.     end
  138.     if debug then
  139.         debug.close()
  140.     end
  141.     local hash = {}
  142.     for i=0, 4 do
  143.         table.insert(hash, h[i])
  144.     end
  145.     return hash
  146. end
  147.  
  148. function digestStr(str, debugFile)
  149.     local msg = {}
  150.     for i=1, #str do
  151.         msg[i] = string.byte(str, i, i)
  152.     end
  153.     return digest(msg, debugFile)
  154. end
  155.  
  156. function digest2str(d)
  157.     local str = ""
  158.     for i=1, #d do
  159.         str = str..string.format("%X", d[i])
  160.     end
  161.     return str
  162. end
  163.  
  164. function digest2bytes(d)
  165.     local bytes = {}
  166.     for i=1, #d do
  167.         table.insert(bytes, bit.band(bit.brshift(bit.band(d[i], 0xFF000000), 24), 0xFF))
  168.         table.insert(bytes, bit.band(bit.brshift(bit.band(d[i], 0xFF0000), 16), 0xFF))
  169.         table.insert(bytes, bit.band(bit.brshift(bit.band(d[i], 0xFF00), 8), 0xFF))
  170.         table.insert(bytes, bit.band(d[i], 0xFF))
  171.     end
  172.     return bytes
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement