Advertisement
Guest User

crypt

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