Advertisement
Guest User

crypt

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