Advertisement
Guest User

Crypt

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