Advertisement
Guest User

Untitled

a guest
Aug 1st, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1.  
  2. local uint32_lrot=_G'bit'.lrot
  3. local byte_xor=_G'bit'.bxor8
  4. local uint32_xor_3=_G'bit'.bxor
  5. local uint32_xor_4=_G'bit'.bxor
  6. local uint32_ternary=_G'bit'.ternary
  7. local uint32_majority=_G'bit'.majority
  8.  
  9. local common={}
  10.  
  11. -- Merges four bytes into a uint32 number.
  12. function common.bytes_to_uint32(a,b,c,d)
  13.     return a*0x1000000+b*0x10000+c*0x100+d
  14. end
  15.  
  16. -- Splits a uint32 number into four bytes.
  17. function common.uint32_to_bytes(a)
  18.     local a4=a%256
  19.     a=(a-a4)/256
  20.     local a3=a%256
  21.     a=(a-a3)/256
  22.     local a2=a%256
  23.     local a1=(a-a2)/256
  24.     return a1,a2,a3,a4
  25. end
  26.  
  27. local bytes_to_uint32=common.bytes_to_uint32
  28. local uint32_to_bytes=common.uint32_to_bytes
  29.  
  30. local sbyte=string.byte
  31. local schar=string.char
  32. local sformat=string.format
  33. local srep=string.rep
  34.  
  35. local function hex_to_binary(hex)
  36.     return(hex:gsub('..',function(hexval)
  37.         return schar(tonumber(hexval,16))
  38.     end))
  39. end
  40.  
  41. local sha1={}
  42.  
  43. -- Calculates SHA1 for a string, returns it encoded as 40 hexadecimal digits.
  44. function sha1.sha1(str)
  45.     -- Input preprocessing.
  46.     -- First, append a `1` bit and seven `0` bits.
  47.     local first_append=schar(0x80)
  48.    
  49.     -- Next, append some zero bytes to make the length of the final message a multiple of 64.
  50.     -- Eight more bytes will be added next.
  51.     local non_zero_message_bytes=#str+1+8
  52.     local second_append=srep(schar(0),-non_zero_message_bytes%64)
  53.    
  54.     -- Finally, append the length of the original message in bits as a 64-bit number.
  55.     -- Assume that it fits into the lower 32 bits.
  56.     local third_append=schar(0,0,0,0,uint32_to_bytes(#str*8))
  57.    
  58.     str=str..first_append..second_append..third_append
  59.     assert(#str%64==0)
  60.    
  61.     -- Initialize hash value.
  62.     local h0=0x67452301
  63.     local h1=0xefcdab89
  64.     local h2=0x98badcfe
  65.     local h3=0x10325476
  66.     local h4=0xc3d2e1f0
  67.    
  68.     local w={}
  69.    
  70.     -- Process the input in successive 64-byte chunks.
  71.     for chunk_start=1,#str,64 do
  72.         -- Load the chunk into W[0..15] as uint32 numbers.
  73.         local uint32_start=chunk_start
  74.        
  75.         for i=0,15 do
  76.             w[i]=bytes_to_uint32(sbyte(str,uint32_start,uint32_start+3))
  77.             uint32_start=uint32_start+4
  78.         end
  79.        
  80.         -- Extend the input vector.
  81.         for i=16,79 do
  82.             -- For i = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
  83.             w[i]=uint32_lrot(uint32_xor_4(w[i-3],w[i-8],w[i-14],w[i-16]),1)
  84.         end
  85.        
  86.         -- Initialize hash value for this chunk.
  87.         local a=h0
  88.         local b=h1
  89.         local c=h2
  90.         local d=h3
  91.         local e=h4
  92.        
  93.         -- Main loop.
  94.         for i=0,79 do
  95.             local f
  96.             local k
  97.            
  98.             if i<=19 then
  99.                 f=uint32_ternary(b,c,d)
  100.                 k=0x5a827999
  101.             elseif i<=39 then
  102.                 f=uint32_xor_3(b,c,d)
  103.                 k=0x6ed9eba1
  104.             elseif i<=59 then
  105.                 f=uint32_majority(b,c,d)
  106.                 k=0x8f1bbcdc
  107.             else
  108.                 f=uint32_xor_3(b,c,d)
  109.                 k=0xca62c1d6
  110.             end
  111.            
  112.             local temp=(uint32_lrot(a,5)+f+e+k+w[i])%0x100000000
  113.             e=d
  114.             d=c
  115.             c=uint32_lrot(b,30)
  116.             b=a
  117.             a=temp
  118.         end
  119.        
  120.         -- Add this chunk's hash to result so far.
  121.         h0=(h0+a)%0x100000000
  122.         h1=(h1+b)%0x100000000
  123.         h2=(h2+c)%0x100000000
  124.         h3=(h3+d)%0x100000000
  125.         h4=(h4+e)%0x100000000
  126.     end
  127.    
  128.     return sformat('%08x%08x%08x%08x%08x',h0,h1,h2,h3,h4)
  129. end
  130. sha1.new=sha1.sha1
  131.  
  132. function sha1.binary(str)
  133.     return hex_to_binary(sha1.sha1(str))
  134. end
  135.  
  136. -- Precalculate replacement tables.
  137. local xor_with_0x5c={}
  138. local xor_with_0x36={}
  139.  
  140. for i=0,0xff do
  141.     xor_with_0x5c[schar(i)]=schar(byte_xor(0x5c,i))
  142.     xor_with_0x36[schar(i)]=schar(byte_xor(0x36,i))
  143. end
  144.  
  145. -- 512 bits.
  146. local BLOCK_SIZE=64
  147.  
  148. function sha1.hmac(key,text)
  149.     if#key>BLOCK_SIZE then
  150.         key=sha1.binary(key)
  151.     end
  152.    
  153.     local key_xord_with_0x36=key:gsub('.',xor_with_0x36)..srep(schar(0x36),BLOCK_SIZE-#key)
  154.     local key_xord_with_0x5c=key:gsub('.',xor_with_0x5c)..srep(schar(0x5c),BLOCK_SIZE-#key)
  155.    
  156.     return sha1.sha1(key_xord_with_0x5c..sha1.binary(key_xord_with_0x36..text))
  157. end
  158.  
  159. function sha1.hmac_binary(key,text)
  160.     return hex_to_binary(sha1.hmac(key,text))
  161. end
  162.  
  163. return sha1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement