ThommyCraft

crypt

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