Advertisement
GopherAtl

SHA1 (code by tomas1666) (computercraft lua)

Sep 29th, 2012
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. --[[
  2. NOTE: This is a modified version of the SHA1 routines from the StringUtils api by tomass1996
  3. which have been revised to use the much faster java bit api now available in cc instead of
  4. the original native lua bitwise operations where appropriate.
  5. No other changes made.
  6.  
  7. Original license from StringUtils api:
  8.  
  9. Copyright (C) 2012 Thomas Farr a.k.a tomass1996 [farr.thomas@gmail.com]
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  12. associated documentation files (the "Software"), to deal in the Software without restriction,
  13. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  15. subject to the following conditions:
  16.  
  17. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  18. -Visible credit is given to the original author.
  19. -The software is distributed in a non-profit way.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  22. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  23. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26.  
  27. --]]
  28.  
  29. local floor,modf, insert = math.floor,math.modf, table.insert
  30. local char,format,rep = string.char,string.format,string.rep
  31.  
  32.  
  33.  
  34. local tSHA1 = {}
  35. tSHA1["bytes_to_w32"] = function(a,b,c,d) return a*0x1000000+b*0x10000+c*0x100+d end
  36. tSHA1["w32_rot"] = function(bits,a)
  37.     local b2 = 2^(32-bits)
  38.     local a,b = modf(a/b2)
  39.     return a+b*b2*(2^(bits))
  40. end
  41. tSHA1["w32_to_hexstring"] = function(w) return format("%08x",w) end
  42.  
  43.  
  44.  
  45. function SHA1(str) --Returns SHA1 Hash of @str
  46.     if not str then return nil end
  47.     str = tostring(str)
  48.     local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
  49.     local msg_len_in_bits = #str * 8
  50.     local first_append = char(0x80)
  51.     local non_zero_message_bytes = #str +1 +8
  52.     local current_mod = non_zero_message_bytes % 64
  53.     local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""
  54.     local B1, R1 = modf(msg_len_in_bits  / 0x01000000)
  55.     local B2, R2 = modf( 0x01000000 * R1 / 0x00010000)
  56.     local B3, R3 = modf( 0x00010000 * R2 / 0x00000100)
  57.     local B4    = 0x00000100 * R3
  58.     local L64 = char( 0) .. char( 0) .. char( 0) .. char( 0)
  59.             .. char(B1) .. char(B2) .. char(B3) .. char(B4)
  60.     str = str .. first_append .. second_append .. L64
  61.     assert(#str % 64 == 0)
  62.     local chunks = #str / 64
  63.     local W = { }
  64.     local start, A, B, C, D, E, f, K, TEMP
  65.     local chunk = 0
  66.     while chunk < chunks do
  67.         start,chunk = chunk * 64 + 1,chunk + 1
  68.         for t = 0, 15 do
  69.             W[t] = tSHA1.bytes_to_w32(str:byte(start, start + 3))
  70.             start = start + 4
  71.         end
  72.         for t = 16, 79 do
  73.             W[t] = tSHA1.w32_rot(1, bit.bxor(bit.bxor(W[t-3], W[t-8]), bit.bxor(W[t-14], W[t-16])))
  74.         end
  75.         A,B,C,D,E = H0,H1,H2,H3,H4
  76.         for t = 0, 79 do
  77.             if t <= 19 then
  78.                 f = bit.bor(bit.band(B, C), bit.band(bit.bnot(B), D))
  79.                 K = 0x5A827999
  80.             elseif t <= 39 then
  81.                 f = bit.bxor(bit.bxor(B, C), D)
  82.                 K = 0x6ED9EBA1
  83.             elseif t <= 59 then
  84.                 f = bit.bor(bit.bor(bit.band(B, C), bit.band(B, D)), bit.band(C, D))
  85.                 K = 0x8F1BBCDC
  86.             else
  87.                 f = bit.bxor(bit.bxor(B, C), D)
  88.                 K = 0xCA62C1D6
  89.             end
  90.             A,B,C,D,E = (tSHA1.w32_rot(5, A)+ f + E + W[t] + K)% 0x100000000,
  91.             A, tSHA1.w32_rot(30, B), C, D
  92.         end        
  93.         H0,H1,H2,H3,H4 = (H0+A)%0x100000000,(H1+B)%0x100000000,(H2+C)%0x100000000,(H3+D)%0x100000000,(H4+E)%0x100000000
  94.     end
  95.     local f = tSHA1.w32_to_hexstring
  96.     return f(H0) .. f(H1) .. f(H2) .. f(H3) .. f(H4)
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement