benthomas7777

BM-Bank Crypt

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