Guest User

Crypt

a guest
Dec 19th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. --]]
  2.  
  3. -------------------------------------------------------------------------------
  4. -- SHA-1 secure hash computation, and HMAC-SHA1 signature computation,
  5. -- in pure Lua (tested on Lua 5.1)
  6. -- License: GPL
  7. --
  8. -- Usage:
  9. -- local hash_as_hex = sha1(message) -- returns a hex string
  10. -- local hash_as_data = sha1_binary(message) -- returns raw bytes
  11. --
  12. -- Pass sha1() a string, and it returns a hash as a 40-character hex string.
  13. --
  14. -- The "_binary" version does the same, but returns the 20-byte string of raw
  15. -- data that the 40-byte hex string represents.
  16. --
  17. -------------------------------------------------------------------------------
  18. -- based on Zet's implementation (which I found too big)
  19. -- > http://cube3d.de/uploads/Main/sha1.txt
  20. -- > > based on Jeffrey Friedl's implementation (which I found a bit too slow)
  21. -- > > http://regex.info/blog/
  22. -- > > Version 1 [May 28, 2009]
  23. -- Algorithm: http://www.itl.nist.gov/fipspubs/fip180-1.htm
  24.  
  25. -------------------------------------------------------------------------------
  26. -------------------------------------------------------------------------------
  27.  
  28. -- set this to false if you don't want to build several 64k sized tables when
  29. -- loading this file (takes a while but grants a boost of factor 13)
  30. local cfg_caching = false
  31.  
  32. -- local storing of global functions (minor speedup)
  33. local floor,modf = math.floor,math.modf
  34. local char,format,rep = string.char,string.format,string.rep
  35.  
  36. -- merge 4 bytes to an 32 bit word
  37. local function bytes_to_w32 (a,b,c,d) return a*0x1000000+b*0x10000+c*0x100+d end
  38. -- split a 32 bit word into four 8 bit numbers
  39. local function w32_to_bytes (i)
  40. return floor(i/0x1000000)%0x100,floor(i/0x10000)%0x100,floor(i/0x100)%0x100,i%0x100
  41. end
  42.  
  43. -- shift the bits of a 32 bit word. Don't use negative values for "bits"
  44. local function w32_rot (bits,a)
  45. local b2 = 2^(32-bits)
  46. local a,b = modf(a/b2)
  47. return a+b*b2*(2^(bits))
  48. end
  49.  
  50. -- caching function for functions that accept 2 arguments, both of values between
  51. -- 0 and 255. The function to be cached is passed, all values are calculated
  52. -- during loading and a function is returned that returns the cached values (only)
  53. local function cache2arg (fn)
  54. if not cfg_caching then return fn end
  55. local lut = {}
  56. for i=0,0xffff do
  57. local a,b = floor(i/0x100),i%0x100
  58. lut[i] = fn(a,b)
  59. end
  60. return function (a,b)
  61. return lut[a*0x100+b]
  62. end
  63. end
  64.  
  65. -- splits an 8-bit number into 8 bits, returning all 8 bits as booleans
  66. local function byte_to_bits (b)
  67. local b = function (n)
  68. local b = floor(b/n)
  69. return b%2==1
  70. end
  71. return b(1),b(2),b(4),b(8),b(16),b(32),b(64),b(128)
  72. end
  73.  
  74. -- builds an 8bit number from 8 booleans
  75. local function bits_to_byte (a,b,c,d,e,f,g,h)
  76. local function n(b,x) return b and x or 0 end
  77. return n(a,1)+n(b,2)+n(c,4)+n(d,8)+n(e,16)+n(f,32)+n(g,64)+n(h,128)
  78. end
  79.  
  80. -- debug function for visualizing bits in a string
  81. local function bits_to_string (a,b,c,d,e,f,g,h)
  82. local function x(b) return b and "1" or "0" end
  83. return ("%s%s%s%s %s%s%s%s"):format(x(a),x(b),x(c),x(d),x(e),x(f),x(g),x(h))
  84. end
  85.  
  86. -- debug function for converting a 8-bit number as bit string
  87. local function byte_to_bit_string (b)
  88. return bits_to_string(byte_to_bits(b))
  89. end
  90.  
  91. -- debug function for converting a 32 bit number as bit string
  92. local function w32_to_bit_string(a)
  93. if type(a) == "string" then return a end
  94. local aa,ab,ac,ad = w32_to_bytes(a)
  95. local s = byte_to_bit_string
  96. return ("%s %s %s %s"):format(s(aa):reverse(),s(ab):reverse(),s(ac):reverse(),s(ad):reverse()):reverse()
  97. end
  98.  
  99. -- bitwise "and" function for 2 8bit number
  100. local band = cache2arg (function(a,b)
  101. local A,B,C,D,E,F,G,H = byte_to_bits(b)
  102. local a,b,c,d,e,f,g,h = byte_to_bits(a)
  103. return bits_to_byte(
  104. A and a, B and b, C and c, D and d,
  105. E and e, F and f, G and g, H and h)
  106. end)
  107.  
  108. -- bitwise "or" function for 2 8bit numbers
  109. local bor = cache2arg(function(a,b)
  110. local A,B,C,D,E,F,G,H = byte_to_bits(b)
  111. local a,b,c,d,e,f,g,h = byte_to_bits(a)
  112. return bits_to_byte(
  113. A or a, B or b, C or c, D or d,
  114. E or e, F or f, G or g, H or h)
  115. end)
  116.  
  117. -- bitwise "xor" function for 2 8bit numbers
  118. local bxor = cache2arg(function(a,b)
  119. local A,B,C,D,E,F,G,H = byte_to_bits(b)
  120. local a,b,c,d,e,f,g,h = byte_to_bits(a)
  121. return bits_to_byte(
  122. A ~= a, B ~= b, C ~= c, D ~= d,
  123. E ~= e, F ~= f, G ~= g, H ~= h)
  124. end)
  125.  
  126. -- bitwise complement for one 8bit number
  127. local function bnot (x)
  128. return 255-(x % 256)
  129. end
  130.  
  131. -- creates a function to combine to 32bit numbers using an 8bit combination function
  132. local function w32_comb(fn)
  133. return function (a,b)
  134. local aa,ab,ac,ad = w32_to_bytes(a)
  135. local ba,bb,bc,bd = w32_to_bytes(b)
  136. return bytes_to_w32(fn(aa,ba),fn(ab,bb),fn(ac,bc),fn(ad,bd))
  137. end
  138. end
  139.  
  140. -- create functions for and, xor and or, all for 2 32bit numbers
  141. local w32_and = w32_comb(band)
  142. local w32_xor = w32_comb(bxor)
  143. local w32_or = w32_comb(bor)
  144.  
  145. -- xor function that may receive a variable number of arguments
  146. local function w32_xor_n (a,...)
  147. local aa,ab,ac,ad = w32_to_bytes(a)
  148. for i=1,select('#',...) do
  149. local ba,bb,bc,bd = w32_to_bytes(select(i,...))
  150. aa,ab,ac,ad = bxor(aa,ba),bxor(ab,bb),bxor(ac,bc),bxor(ad,bd)
  151. end
  152. return bytes_to_w32(aa,ab,ac,ad)
  153. end
  154.  
  155. -- combining 3 32bit numbers through binary "or" operation
  156. local function w32_or3 (a,b,c)
  157. local aa,ab,ac,ad = w32_to_bytes(a)
  158. local ba,bb,bc,bd = w32_to_bytes(b)
  159. local ca,cb,cc,cd = w32_to_bytes(c)
  160. return bytes_to_w32(
  161. bor(aa,bor(ba,ca)), bor(ab,bor(bb,cb)), bor(ac,bor(bc,cc)), bor(ad,bor(bd,cd))
  162. )
  163. end
  164.  
  165. -- binary complement for 32bit numbers
  166. local function w32_not (a)
  167. return 4294967295-(a % 4294967296)
  168. end
  169.  
  170. -- adding 2 32bit numbers, cutting off the remainder on 33th bit
  171. local function w32_add (a,b) return (a+b) % 4294967296 end
  172.  
  173. -- adding n 32bit numbers, cutting off the remainder (again)
  174. local function w32_add_n (a,...)
  175. for i=1,select('#',...) do
  176. a = (a+select(i,...)) % 4294967296
  177. end
  178. return a
  179. end
  180. -- converting the number to a hexadecimal string
  181. local function w32_to_hexstring (w) return format("%08x",w) end
  182.  
  183. -- calculating the SHA1 for some text
  184. function sha1(msg)
  185. local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
  186. local msg_len_in_bits = #msg * 8
  187.  
  188. local first_append = char(0x80) -- append a '1' bit plus seven '0' bits
  189.  
  190. local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
  191. local current_mod = non_zero_message_bytes % 64
  192. local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""
  193.  
  194. -- now to append the length as a 64-bit number.
  195. local B1, R1 = modf(msg_len_in_bits / 0x01000000)
  196. local B2, R2 = modf( 0x01000000 * R1 / 0x00010000)
  197. local B3, R3 = modf( 0x00010000 * R2 / 0x00000100)
  198. local B4 = 0x00000100 * R3
  199.  
  200. local L64 = char( 0) .. char( 0) .. char( 0) .. char( 0) -- high 32 bits
  201. .. char(B1) .. char(B2) .. char(B3) .. char(B4) -- low 32 bits
  202.  
  203. msg = msg .. first_append .. second_append .. L64
  204.  
  205. assert(#msg % 64 == 0)
  206.  
  207. local chunks = #msg / 64
  208.  
  209. local W = { }
  210. local start, A, B, C, D, E, f, K, TEMP
  211. local chunk = 0
  212.  
  213. while chunk < chunks do
  214. --
  215. -- break chunk up into W[0] through W[15]
  216. --
  217. start,chunk = chunk * 64 + 1,chunk + 1
  218.  
  219. for t = 0, 15 do
  220. W[t] = bytes_to_w32(msg:byte(start, start + 3))
  221. start = start + 4
  222. end
  223.  
  224. --
  225. -- build W[16] through W[79]
  226. --
  227. for t = 16, 79 do
  228. -- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
  229. W[t] = w32_rot(1, w32_xor_n(W[t-3], W[t-8], W[t-14], W[t-16]))
  230. end
  231.  
  232. A,B,C,D,E = H0,H1,H2,H3,H4
  233.  
  234. for t = 0, 79 do
  235. if t <= 19 then
  236. -- (B AND C) OR ((NOT B) AND D)
  237. f = w32_or(w32_and(B, C), w32_and(w32_not(B), D))
  238. K = 0x5A827999
  239. elseif t <= 39 then
  240. -- B XOR C XOR D
  241. f = w32_xor_n(B, C, D)
  242. K = 0x6ED9EBA1
  243. elseif t <= 59 then
  244. -- (B AND C) OR (B AND D) OR (C AND D
  245. f = w32_or3(w32_and(B, C), w32_and(B, D), w32_and(C, D))
  246. K = 0x8F1BBCDC
  247. else
  248. -- B XOR C XOR D
  249. f = w32_xor_n(B, C, D)
  250. K = 0xCA62C1D6
  251. end
  252.  
  253. -- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
  254. A,B,C,D,E = w32_add_n(w32_rot(5, A), f, E, W[t], K),
  255. A, w32_rot(30, B), C, D
  256. end
  257. -- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
  258. H0,H1,H2,H3,H4 = w32_add(H0, A),w32_add(H1, B),w32_add(H2, C),w32_add(H3, D),w32_add(H4, E)
  259. end
  260. local f = w32_to_hexstring
  261. return f(H0) .. f(H1) .. f(H2) .. f(H3) .. f(H4)
  262. end
  263.  
  264. local function hex_to_binary(hex)
  265. return hex:gsub('..', function(hexval)
  266. return string.char(tonumber(hexval, 16))
  267. end)
  268. end
  269.  
  270. function sha1_binary(msg)
  271. return hex_to_binary(sha1(msg))
  272. end
  273.  
  274. local xor_with_0x5c = {}
  275. local xor_with_0x36 = {}
  276. -- building the lookuptables ahead of time (instead of littering the source code
  277. -- with precalculated values)
  278. for i=0,0xff do
  279. xor_with_0x5c[char(i)] = char(bxor(i,0x5c))
  280. xor_with_0x36[char(i)] = char(bxor(i,0x36))
  281. end
  282.  
  283. -- String Randomizer API --
  284. local chars = {}
  285. for loop = 0, 255 do
  286. chars[loop+1] = string.char(loop)
  287. end
  288. local string = table.concat(chars)
  289.  
  290. local built = {['.'] = chars}
  291.  
  292. local addLookup = function(charSet)
  293. local substitute = string.gsub(string, '[^'..charSet..']', '')
  294. local lookup = {}
  295. for loop = 1, string.len(substitute) do
  296. lookup[loop] = string.sub(substitute, loop, loop)
  297. end
  298. built[charSet] = lookup
  299.  
  300. return lookup
  301. end
  302.  
  303. function random(length, charSet)
  304. -- length (number)
  305. -- charSet (string, optional); e.g. %l%d for lower case letters and digits
  306.  
  307. local charSet = charSet or '.'
  308.  
  309. if charSet == '' then
  310. return ''
  311. else
  312. local result = {}
  313. local lookup = built[charSet] or addLookup(charSet)
  314. local range = table.getn(lookup)
  315.  
  316. for loop = 1,length do
  317. result[loop] = lookup[math.random(1, range)]
  318. end
  319.  
  320. return table.concat(result)
  321. end
  322. end
  323.  
  324. function randomULN(length)
  325. return random(length, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789")
  326. end
  327.  
  328. -- Hashing API --
  329. function hashPassword(password)
  330. local salt = string.sub(sha1(randomULN(20)), 1, 12)
  331. local hash = sha1(salt .. password)
  332. local saltPos = string.len(password)
  333. if saltPos > string.len(hash) then
  334. saltPos = string.len(hash)
  335. end
  336. return string.sub(hash, 1, saltPos) .. salt .. string.sub(hash, saltPos + 1, string.len(hash))
  337. end
  338.  
  339. function checkPassword(input, correct)
  340. local saltPos = string.len(correct)
  341. if saltPos > string.len(input) - 12 then
  342. saltPos = string.len(input) - 12
  343. end
  344. local salt = string.sub(input, saltPos + 1, saltPos + 12)
  345. local password = sha1(salt .. correct)
  346. return (password == (string.sub(input, 1, saltPos) .. string.sub(input, saltPos + 13, string.len(input))))
  347. end
  348.  
  349. -- crypt.checkPassword(crypt.hashPassword("password"), "password")
Advertisement
Add Comment
Please, Sign In to add comment