Advertisement
Tatantyler

SHA-256

May 2nd, 2013
1,712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. -- SHA-256
  2. -- By KillaVanilla
  3.  
  4. k = {
  5.    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  6.    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  7.    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  8.    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  9.    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  10.    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  11.    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  12.    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  13. }
  14.  
  15. local function generateShiftBitmask(bits)
  16.     local bitmask = 0
  17.     for i=1, bits do
  18.         bit.bor(bitmask, 0x8000000)
  19.         bit.brshift(bitmask, 1)
  20.     end
  21.     return bitmask
  22. end
  23.  
  24. local function rrotate(input, shiftAmount)
  25.     if input > (2^32) then
  26.         input = input % (2^32)
  27.     end
  28.     return bit.bor( bit.brshift(input, shiftAmount), bit.blshift(input, 32-shiftAmount) )
  29. end
  30.  
  31. local function Preprocessing(message)
  32.     local len = #message*8
  33.     local bits = #message*8
  34.     table.insert(message, 1)
  35.     while true do
  36.         if bits % 512 == 448 then
  37.             break
  38.         else
  39.             table.insert(message, 0)
  40.             bits = #message*8
  41.         end
  42.     end
  43.     table.insert(message, len)
  44.     return message
  45. end
  46.  
  47. local function breakMsg(message)
  48.     local chunks = {}
  49.     local chunk = 1
  50.     for word=1, #message, 16 do
  51.         chunks[chunk] = {}
  52.         table.insert(chunks[chunk], message[word] or 0)
  53.         table.insert(chunks[chunk], message[word+1] or 0)
  54.         table.insert(chunks[chunk], message[word+2] or 0)
  55.         table.insert(chunks[chunk], message[word+3] or 0)
  56.         table.insert(chunks[chunk], message[word+4] or 0)
  57.         table.insert(chunks[chunk], message[word+5] or 0)
  58.         table.insert(chunks[chunk], message[word+6] or 0)
  59.         table.insert(chunks[chunk], message[word+7] or 0)
  60.         table.insert(chunks[chunk], message[word+8] or 0)
  61.         table.insert(chunks[chunk], message[word+9] or 0)
  62.         table.insert(chunks[chunk], message[word+10] or 0)
  63.         table.insert(chunks[chunk], message[word+11] or 0)
  64.         table.insert(chunks[chunk], message[word+12] or 0)
  65.         table.insert(chunks[chunk], message[word+13] or 0)
  66.         table.insert(chunks[chunk], message[word+14] or 0)
  67.         table.insert(chunks[chunk], message[word+15] or 0)
  68.         chunk = chunk+1
  69.     end
  70.     return chunks
  71. end
  72.  
  73. local function digestChunk(chunk, hash)
  74.     for i=17, 64 do
  75.         local s0 = bit.bxor( bit.brshift(chunk[i-15], 3), bit.bxor( rrotate(chunk[i-15], 7), rrotate(chunk[i-15], 18) ) )
  76.         local s1 = bit.bxor( bit.brshift(chunk[i-2], 10), bit.bxor( rrotate(chunk[i-2], 17), rrotate(chunk[i-2], 19) ) )
  77.         chunk[i] = (chunk[i-16] + s0 + chunk[i-7] + s1) % (2^32)
  78.     end
  79.  
  80.     local a = hash[1]
  81.     local b = hash[2]
  82.     local c = hash[3]
  83.     local d = hash[4]
  84.     local e = hash[5]
  85.     local f = hash[6]
  86.     local g = hash[7]
  87.     local h = hash[8]
  88.    
  89.     for i=1, 64 do
  90.         local S1 = bit.bxor(rrotate(e, 6), bit.bxor(rrotate(e,11),rrotate(e,25)))
  91.         local ch = bit.bxor( bit.band(e, f), bit.band(bit.bnot(e), g) )
  92.         local t1 = h + S1 + ch + k[i] + chunk[i]
  93.         --d = d+h
  94.         S0 = bit.bxor(rrotate(a,2), bit.bxor( rrotate(a,13), rrotate(a,22) ))
  95.         local maj = bit.bxor( bit.band( a, bit.bxor(b, c) ), bit.band(b, c) )
  96.         local t2 = S0 + maj
  97.        
  98.         h = g
  99.         g = f
  100.         f = e
  101.         e = d + t1
  102.         d = c
  103.         c = b
  104.         b = a
  105.         a = t1 + t2
  106.        
  107.         a = a % (2^32)
  108.         b = b % (2^32)
  109.         c = c % (2^32)
  110.         d = d % (2^32)
  111.         e = e % (2^32)
  112.         f = f % (2^32)
  113.         g = g % (2^32)
  114.         h = h % (2^32)
  115.        
  116.     end
  117.        
  118.     hash[1] = (hash[1] + a) % (2^32)
  119.     hash[2] = (hash[2] + b) % (2^32)
  120.     hash[3] = (hash[3] + c) % (2^32)
  121.     hash[4] = (hash[4] + d) % (2^32)
  122.     hash[5] = (hash[5] + e) % (2^32)
  123.     hash[6] = (hash[6] + f) % (2^32)
  124.     hash[7] = (hash[7] + g) % (2^32)
  125.     hash[8] = (hash[8] + h) % (2^32)
  126.    
  127.     return hash
  128. end
  129.  
  130. function digest(msg)
  131.     msg = Preprocessing(msg)
  132.     local hash = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}
  133.     local chunks = breakMsg(msg)
  134.     for i=1, #chunks do
  135.         hash = digestChunk(chunks[i], hash)
  136.     end
  137.     return hash
  138. end
  139.  
  140. function digestStr(input)
  141.     -- transform the input into a table of ints:
  142.     local output = {}
  143.     local outputStr = ""
  144.     for i=1, #input do
  145.         output[i] = string.byte(input, i, i)
  146.     end
  147.     output = digest(output)
  148.     for i=1, #output do
  149.         outputStr = outputStr..string.format("%X", output[i])
  150.     end
  151.     return outputStr, output
  152. end
  153.  
  154. function hashToBytes(hash)
  155.     local bytes = {}
  156.     for i=1, 8 do
  157.         table.insert(bytes, bit.band(bit.brshift(bit.band(hash[i], 0xFF000000), 24), 0xFF))
  158.         table.insert(bytes, bit.band(bit.brshift(bit.band(hash[i], 0xFF0000), 16), 0xFF))
  159.         table.insert(bytes, bit.band(bit.brshift(bit.band(hash[i], 0xFF00), 8), 0xFF))
  160.         table.insert(bytes, bit.band(hash[i], 0xFF))
  161.     end
  162.     return bytes
  163. end
  164.  
  165. function hmac(input, key)
  166.     -- HMAC(H,K,m) = H( (K <xor> opad) .. H((K <xor> ipad) .. m))
  167.     -- Where:
  168.     -- H - cryptographic hash function. In this case, H is SHA-256.
  169.     -- K - The secret key.
  170.     --  if length(K) > 256 bits or 32 bytes, then K = H(K)
  171.     --  if length(K) < 256 bits or 32 bytes, then pad K to the right with zeroes. (i.e pad(K) = K .. repeat(0, 32 - byte_length(K)))
  172.     -- m - The message to be authenticated.
  173.     -- .. - byte concentration
  174.     -- <xor> eXclusive OR.
  175.     -- opad - Outer Padding, equal to repeat(0x5C, 32).
  176.     -- ipad - Inner Padding, equal to repeat(0x36, 32).
  177.     if #key > 32 then
  178.         local keyDigest = digest(key)
  179.         key = keyDigest
  180.     elseif #key < 32 then
  181.         for i=#key, 32 do
  182.             key[i] = 0
  183.         end
  184.     end
  185.     local opad = {}
  186.     local ipad = {}
  187.     for i=1, 32 do
  188.         opad[i] = bit.bxor(0x5C, key[i] or 0)
  189.         ipad[i] = bit.bxor(0x36, key[i] or 0)
  190.     end
  191.     local padded_key = {}
  192.     for i=1, #input do
  193.         ipad[32+i] = input[i]
  194.     end
  195.     local ipadHash = hashToBytes(digest(ipad))
  196.     ipad = ipadHash
  197.     for i=1, 32 do
  198.         padded_key[i] = opad[i]
  199.         padded_key[32+i] = ipad[i]
  200.     end
  201.     return digest(padded_key)
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement