Advertisement
Jazza

strUtils

Jun 10th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 33.32 KB | None | 0 0
  1. --[[
  2. StringUtils
  3. Copyright (C) 2012 Thomas Farr a.k.a tomass1996 [[email protected]]
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. associated documentation files (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10.  
  11. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. -Visible credit is given to the original author.
  13. -The software is distributed in a non-profit way.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  16. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. --]]
  20.  
  21. local floor,modf, insert = math.floor,math.modf, table.insert
  22. local char,format,rep = string.char,string.format,string.rep
  23.  
  24. local function basen(n,b)
  25.     if n < 0 then
  26.         n = -n
  27.     end
  28.        local t = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~"
  29.    if n < b then
  30.     local ret = ""
  31.     ret = ret..string.sub(t, (n%b)+1,(n%b)+1)
  32.     return ret
  33.    else
  34.     local tob = tostring(basen(math.floor(n/b), b))
  35.     local ret = tob..t:sub((n%b)+1,(n%b)+1)
  36.     return ret
  37.    end
  38. end
  39.  
  40. local Base64 = {}
  41. Base64["lsh"] = function(value,shift)
  42.     return (value*(2^shift)) % 256
  43. end
  44. Base64["rsh"] = function(value,shift)
  45.     return math.floor(value/2^shift) % 256
  46. end
  47. Base64["bit"] = function(x,b)
  48.     return (x % 2^b - x % 2^(b-1) > 0)
  49. end
  50. Base64["lor"] = function(x,y)
  51.     local result = 0
  52.     for p=1,8 do result = result + (((Base64.bit(x,p) or Base64.bit(y,p)) == true) and 2^(p-1) or 0) end
  53.     return result
  54. end
  55. Base64["base64chars"] = {
  56.     [0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',
  57.     [11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',
  58.     [21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',
  59.     [31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',
  60.     [41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',
  61.     [51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',
  62.     [61]='9',[62]='-',[63]='_'}
  63. Base64["base64bytes"] = {
  64.     ['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,
  65.     ['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,
  66.     ['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,
  67.     ['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,
  68.     ['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,
  69.     ['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,
  70.     ['9']=61,['-']=62,['_']=63,['=']=nil}
  71.  
  72. local base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
  73.  
  74. local tSHA1 = {}
  75. tSHA1["bytes_to_w32"] = function(a,b,c,d) return a*0x1000000+b*0x10000+c*0x100+d end
  76. tSHA1["w32_to_bytes"] = function(i) return floor(i/0x1000000)%0x100,floor(i/0x10000)%0x100,floor(i/0x100)%0x100,i%0x100 end
  77. tSHA1["w32_rot"] = function(bits,a)
  78.     local b2 = 2^(32-bits)
  79.     local a,b = modf(a/b2)
  80.     return a+b*b2*(2^(bits))
  81. end
  82. tSHA1["byte_to_bits"] = function(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. tSHA1["bits_to_byte"] = function(a,b,c,d,e,f,g,h)
  90.     local function n(b,x) return b and x or 0 end
  91.     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)
  92. end
  93. tSHA1["bits_to_string"] = function(a,b,c,d,e,f,g,h)
  94.     local function x(b) return b and "1" or "0" end
  95.     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))
  96. end
  97. tSHA1["byte_to_bit_string"] = function(b) return tSHA1.bits_to_string(byte_to_bits(b)) end
  98. tSHA1["w32_to_bit_string"] = function(a)
  99.     if type(a) == "string" then return a end
  100.     local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  101.     local s = tSHA1.byte_to_bit_string
  102.     return ("%s %s %s %s"):format(s(aa):reverse(),s(ab):reverse(),s(ac):reverse(),s(ad):reverse()):reverse()
  103. end
  104. tSHA1["band"] = function(a,b)
  105.     local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  106.     local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  107.     return tSHA1.bits_to_byte(
  108.         A and a, B and b, C and c, D and d,
  109.         E and e, F and f, G and g, H and h)
  110. end
  111. tSHA1["bor"] = function(a,b)
  112.     local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  113.     local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  114.     return tSHA1.bits_to_byte(
  115.         A or a, B or b, C or c, D or d,
  116.         E or e, F or f, G or g, H or h)
  117. end
  118. tSHA1["bxor"] = function(a,b)
  119.     local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  120.     local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  121.     return tSHA1.bits_to_byte(
  122.         A ~= a, B ~= b, C ~= c, D ~= d,
  123.         E ~= e, F ~= f, G ~= g, H ~= h)
  124. end
  125. tSHA1["bnot"] = function(x) return 255-(x % 256) end
  126. tSHA1["w32_comb"] = function(fn)
  127.     return function (a,b)
  128.         local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  129.         local ba,bb,bc,bd = tSHA1.w32_to_bytes(b)
  130.         return tSHA1.bytes_to_w32(fn(aa,ba),fn(ab,bb),fn(ac,bc),fn(ad,bd))
  131.     end
  132. end
  133. tSHA1["w32_xor_n"] = function(a,...)
  134.     local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  135.     for i=1,select('#',...) do
  136.         local ba,bb,bc,bd = tSHA1.w32_to_bytes(select(i,...))
  137.         aa,ab,ac,ad = tSHA1.bxor(aa,ba),tSHA1.bxor(ab,bb),tSHA1.bxor(ac,bc),tSHA1.bxor(ad,bd)
  138.     end
  139.     return tSHA1.bytes_to_w32(aa,ab,ac,ad)
  140. end
  141. tSHA1["w32_or3"] = function(a,b,c)
  142.     local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  143.     local ba,bb,bc,bd = tSHA1.w32_to_bytes(b)
  144.     local ca,cb,cc,cd = tSHA1.w32_to_bytes(c)
  145.     return tSHA1.bytes_to_w32(
  146.         tSHA1.bor(aa,tSHA1.bor(ba,ca)), tSHA1.bor(ab,tSHA1.bor(bb,cb)), tSHA1.bor(ac,tSHA1.bor(bc,cc)), tSHA1.bor(ad,tSHA1.bor(bd,cd))
  147.     )
  148. end
  149. tSHA1["w32_not"] = function(a) return 4294967295-(a % 4294967296) end
  150. tSHA1["w32_add"] = function(a,b) return (a+b) % 4294967296 end
  151. tSHA1["w32_add_n"] = function(a,...)
  152.     for i=1,select('#',...) do
  153.         a = (a+select(i,...)) % 4294967296
  154.     end
  155.     return a
  156. end
  157. tSHA1["w32_to_hexstring"] = function(w) return format("%08x",w) end
  158. tSHA1["w32_and"] = tSHA1.w32_comb(tSHA1.band)
  159. tSHA1["w32_xor"] = tSHA1.w32_comb(tSHA1.bxor)
  160. tSHA1["w32_or"] = tSHA1.w32_comb(tSHA1.bor)
  161.  
  162. local CRC = {}
  163. CRC.crc32 = {
  164.     0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  165.     0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  166.     0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  167.     0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  168.     0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  169.     0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  170.     0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  171.     0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  172.     0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  173.     0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  174.     0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  175.     0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  176.     0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  177.     0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  178.     0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  179.     0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  180.     0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  181.     0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  182.     0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  183.     0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  184.     0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  185.     0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  186.     0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  187.     0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  188.     0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  189.     0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  190.     0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  191.     0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  192.     0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  193.     0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  194.     0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  195.     0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  196.     0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  197.     0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  198.     0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  199.     0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  200.     0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  201.     0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  202.     0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  203.     0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  204.     0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  205.     0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  206.     0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }
  207.  
  208. local bit = {}
  209. bit["bnot"] = function(n)
  210.     local tbl = bit.tobits(n)
  211.     local size = math.max(table.getn(tbl), 32)
  212.     for i = 1, size do
  213.         if(tbl[i] == 1) then
  214.             tbl[i] = 0
  215.         else
  216.             tbl[i] = 1
  217.         end
  218.     end
  219.     return bit.tonumb(tbl)
  220. end
  221. bit["band"] = function(m, n)
  222.     local tbl_m = bit.tobits(m)
  223.     local tbl_n = bit.tobits(n)
  224.     bit.expand(tbl_m, tbl_n)
  225.     local tbl = {}
  226.     local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  227.     for i = 1, rslt do
  228.         if(tbl_m[i]== 0 or tbl_n[i] == 0) then
  229.             tbl[i] = 0
  230.         else
  231.             tbl[i] = 1
  232.         end
  233.     end
  234.     return bit.tonumb(tbl)
  235. end
  236. bit["bor"] = function(m, n)
  237.     local tbl_m = bit.tobits(m)
  238.     local tbl_n = bit.tobits(n)
  239.     bit.expand(tbl_m, tbl_n)
  240.     local tbl = {}
  241.     local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  242.     for i = 1, rslt do
  243.         if(tbl_m[i]== 0 and tbl_n[i] == 0) then
  244.             tbl[i] = 0
  245.         else
  246.             tbl[i] = 1
  247.         end
  248.     end
  249.     return bit.tonumb(tbl)
  250. end
  251. bit["bxor"] = function(m, n)
  252.     local tbl_m = bit.tobits(m)
  253.     local tbl_n = bit.tobits(n)
  254.     bit.expand(tbl_m, tbl_n)
  255.     local tbl = {}
  256.     local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  257.     for i = 1, rslt do
  258.         if(tbl_m[i] ~= tbl_n[i]) then
  259.             tbl[i] = 1
  260.         else
  261.             tbl[i] = 0
  262.         end
  263.     end
  264.     return bit.tonumb(tbl)
  265. end
  266. bit["brshift"] = function(n, bits)
  267.     bit.checkint(n)
  268.     local high_bit = 0
  269.     if(n < 0) then
  270.         n = bit.bnot(math.abs(n)) + 1
  271.         high_bit = 2147483648
  272.     end
  273.     for i=1, bits do
  274.         n = n/2
  275.         n = bit.bor(math.floor(n), high_bit)
  276.     end
  277.     return math.floor(n)
  278. end
  279. bit["blshift"] = function(n, bits)
  280.     bit.checkint(n)
  281.     if(n < 0) then
  282.         n = bit.bnot(math.abs(n)) + 1
  283.     end
  284.     for i=1, bits do
  285.         n = n*2
  286.     end
  287.     return bit.band(n, 4294967295)
  288. end
  289. bit["bxor2"] = function(m, n)
  290.     local rhs = bit.bor(bit.bnot(m), bit.bnot(n))
  291.     local lhs = bit.bor(m, n)
  292.     local rslt = bit.band(lhs, rhs)
  293.     return rslt
  294. end
  295. bit["blogic_rshift"] = function(n, bits)
  296.     bit.checkint(n)
  297.     if(n < 0) then
  298.         n = bit.bnot(math.abs(n)) + 1
  299.     end
  300.     for i=1, bits do
  301.         n = n/2
  302.     end
  303.     return math.floor(n)
  304. end
  305. bit["tobits"] = function(n)
  306.     bit.checkint(n)
  307.     if(n < 0) then
  308.         return bit.tobits(bit.bnot(math.abs(n)) + 1)
  309.     end
  310.     local tbl = {}
  311.     local cnt = 1
  312.     while (n > 0) do
  313.         local last = math.fmod(n,2)
  314.         if(last == 1) then
  315.             tbl[cnt] = 1
  316.         else
  317.             tbl[cnt] = 0
  318.         end
  319.         n = (n-last)/2
  320.         cnt = cnt + 1
  321.     end
  322.     return tbl
  323. end
  324. bit["tonumb"] = function(tbl)
  325.     local n = table.getn(tbl)
  326.     local rslt = 0
  327.     local power = 1
  328.     for i = 1, n do
  329.         rslt = rslt + tbl[i]*power
  330.         power = power*2
  331.     end
  332.     return rslt
  333. end
  334. bit["checkint"] = function(n)
  335.     if(n - math.floor(n) > 0) then
  336.         error("trying to use bitwise operation on non-integer!")
  337.     end
  338. end
  339. bit["expand"] = function(tbl_m, tbl_n)
  340.     local big = {}
  341.     local small = {}
  342.     if(table.getn(tbl_m) > table.getn(tbl_n)) then
  343.         big = tbl_m
  344.         small = tbl_n
  345.     else
  346.         big = tbl_n
  347.         small = tbl_m
  348.     end
  349.     for i = table.getn(small) + 1, table.getn(big) do
  350.         small[i] = 0
  351.     end
  352. end
  353.  
  354. local FCS = {}
  355. FCS["16"] = {
  356.     [0]=0, 4489, 8978, 12955, 17956, 22445, 25910, 29887,
  357.     35912, 40385, 44890, 48851, 51820, 56293, 59774, 63735,
  358.     4225, 264, 13203, 8730, 22181, 18220, 30135, 25662,
  359.     40137, 36160, 49115, 44626, 56045, 52068, 63999, 59510,
  360.     8450, 12427, 528, 5017, 26406, 30383, 17460, 21949,
  361.     44362, 48323, 36440, 40913, 60270, 64231, 51324, 55797,
  362.     12675, 8202, 4753, 792, 30631, 26158, 21685, 17724,
  363.     48587, 44098, 40665, 36688, 64495, 60006, 55549, 51572,
  364.     16900, 21389, 24854, 28831, 1056, 5545, 10034, 14011,
  365.     52812, 57285, 60766, 64727, 34920, 39393, 43898, 47859,
  366.     21125, 17164, 29079, 24606, 5281, 1320, 14259, 9786,
  367.     57037, 53060, 64991, 60502, 39145, 35168, 48123, 43634,
  368.     25350, 29327, 16404, 20893, 9506, 13483, 1584, 6073,
  369.     61262, 65223, 52316, 56789, 43370, 47331, 35448, 39921,
  370.     29575, 25102, 20629, 16668, 13731, 9258, 5809, 1848,
  371.     65487, 60998, 56541, 52564, 47595, 43106, 39673, 35696,
  372.     33800, 38273, 42778, 46739, 49708, 54181, 57662, 61623,
  373.     2112, 6601, 11090, 15067, 20068, 24557, 28022, 31999,
  374.     38025, 34048, 47003, 42514, 53933, 49956, 61887, 57398,
  375.     6337, 2376, 15315, 10842, 24293, 20332, 32247, 27774,
  376.     42250, 46211, 34328, 38801, 58158, 62119, 49212, 53685,
  377.     10562, 14539, 2640, 7129, 28518, 32495, 19572, 24061,
  378.     46475, 41986, 38553, 34576, 62383, 57894, 53437, 49460,
  379.     14787, 10314, 6865, 2904, 32743, 28270, 23797, 19836,
  380.     50700, 55173, 58654, 62615, 32808, 37281, 41786, 45747,
  381.     19012, 23501, 26966, 30943, 3168, 7657, 12146, 16123,
  382.     54925, 50948, 62879, 58390, 37033, 33056, 46011, 41522,
  383.     23237, 19276, 31191, 26718, 7393, 3432, 16371, 11898,
  384.     59150, 63111, 50204, 54677, 41258, 45219, 33336, 37809,
  385.     27462, 31439, 18516, 23005, 11618, 15595, 3696, 8185,
  386.     63375, 58886, 54429, 50452, 45483, 40994, 37561, 33584,
  387.     31687, 27214, 22741, 18780, 15843, 11370, 7921, 3960 }
  388. FCS["32"] = {
  389.     [0]=0, 1996959894, -301047508, -1727442502, 124634137, 1886057615, -379345611, -1637575261,
  390.     249268274, 2044508324, -522852066, -1747789432, 162941995, 2125561021, -407360249, -1866523247,
  391.     498536548, 1789927666, -205950648, -2067906082, 450548861, 1843258603, -187386543, -2083289657,
  392.     325883990, 1684777152, -43845254, -1973040660, 335633487, 1661365465, -99664541, -1928851979,
  393.     997073096, 1281953886, -715111964, -1570279054, 1006888145, 1258607687, -770865667, -1526024853,
  394.     901097722, 1119000684, -608450090, -1396901568, 853044451, 1172266101, -589951537, -1412350631,
  395.     651767980, 1373503546, -925412992, -1076862698, 565507253, 1454621731, -809855591, -1195530993,
  396.     671266974, 1594198024, -972236366, -1324619484, 795835527, 1483230225, -1050600021, -1234817731,
  397.     1994146192, 31158534, -1731059524, -271249366, 1907459465, 112637215, -1614814043, -390540237,
  398.     2013776290, 251722036, -1777751922, -519137256, 2137656763, 141376813, -1855689577, -429695999,
  399.     1802195444, 476864866, -2056965928, -228458418, 1812370925, 453092731, -2113342271, -183516073,
  400.     1706088902, 314042704, -1950435094, -54949764, 1658658271, 366619977, -1932296973, -69972891,
  401.     1303535960, 984961486, -1547960204, -725929758, 1256170817, 1037604311, -1529756563, -740887301,
  402.     1131014506, 879679996, -1385723834, -631195440, 1141124467, 855842277, -1442165665, -586318647,
  403.     1342533948, 654459306, -1106571248, -921952122, 1466479909, 544179635, -1184443383, -832445281,
  404.     1591671054, 702138776, -1328506846, -942167884, 1504918807, 783551873, -1212326853, -1061524307,
  405.     -306674912, -1698712650, 62317068, 1957810842, -355121351, -1647151185, 81470997, 1943803523,
  406.     -480048366, -1805370492, 225274430, 2053790376, -468791541, -1828061283, 167816743, 2097651377,
  407.     -267414716, -2029476910, 503444072, 1762050814, -144550051, -2140837941, 426522225, 1852507879,
  408.     -19653770, -1982649376, 282753626, 1742555852, -105259153, -1900089351, 397917763, 1622183637,
  409.     -690576408, -1580100738, 953729732, 1340076626, -776247311, -1497606297, 1068828381, 1219638859,
  410.     -670225446, -1358292148, 906185462, 1090812512, -547295293, -1469587627, 829329135, 1181335161,
  411.     -882789492, -1134132454, 628085408, 1382605366, -871598187, -1156888829, 570562233, 1426400815,
  412.     -977650754, -1296233688, 733239954, 1555261956, -1026031705, -1244606671, 752459403, 1541320221,
  413.     -1687895376, -328994266, 1969922972, 40735498, -1677130071, -351390145, 1913087877, 83908371,
  414.     -1782625662, -491226604, 2075208622, 213261112, -1831694693, -438977011, 2094854071, 198958881,
  415.     -2032938284, -237706686, 1759359992, 534414190, -2118248755, -155638181, 1873836001, 414664567,
  416.     -2012718362, -15766928, 1711684554, 285281116, -1889165569, -127750551, 1634467795, 376229701,
  417.     -1609899400, -686959890, 1308918612, 956543938, -1486412191, -799009033, 1231636301, 1047427035,
  418.     -1362007478, -640263460, 1088359270, 936918000, -1447252397, -558129467, 1202900863, 817233897,
  419.     -1111625188, -893730166, 1404277552, 615818150, -1160759803, -841546093, 1423857449, 601450431,
  420.     -1285129682, -1000256840, 1567103746, 711928724, -1274298825, -1022587231, 1510334235, 755167117 }
  421.  
  422. --String Utils :
  423.  
  424. function toCharTable(str)  --Returns table of @str's chars
  425.     if not str then return nil end
  426.     str = tostring(str)
  427.     local chars = {}
  428.     for n=1,#str do
  429.         chars[n] = str:sub(n,n)
  430.     end
  431.     return chars
  432. end
  433.  
  434. function toByteTable(str)  --Returns table of @str's bytes
  435.     if not str then return nil end
  436.     str = tostring(str)
  437.     local bytes = {}
  438.     for n=1,#str do
  439.         bytes[n] = str:byte(n)
  440.     end
  441.     return bytes
  442. end
  443.  
  444. function fromCharTable(chars)  --Returns string made of chracters in @chars
  445.     if not chars or type(chars)~="table" then return nil end
  446.     return table.concat(chars)
  447. end
  448.  
  449. function fromByteTable(bytes)  --Returns string made of bytes in @bytes
  450.     if not bytes or type(bytes)~="table" then return nil end
  451.     local str = ""
  452.     for n=1,#bytes do
  453.         str = str..string.char(bytes[n])
  454.     end
  455.     return str
  456. end
  457.  
  458. function contains(str,find)  --Returns true if @str contains @find
  459.     if not str then return nil end
  460.     str = tostring(str)
  461.     for n=1, #str-#find+1 do
  462.         if str:sub(n,n+#find-1) == find then return true end
  463.     end
  464.     return false
  465. end
  466.  
  467. function startsWith(str,Start) --Check if @str starts with @Start
  468.     if not str then return nil end
  469.     str = tostring(str)
  470.     return str:sub(1,Start:len())==Start
  471. end
  472.  
  473. function endsWith(str,End)  --Check if @str ends with @End
  474.     if not str then return nil end
  475.     str = tostring(str)
  476.     return End=='' or str:sub(#str-#End+1)==End
  477. end
  478.  
  479. function trim(str)  --Trim @str of initial/trailing whitespace
  480.     if not str then return nil end
  481.     str = tostring(str)
  482.     return (str:gsub("^%s*(.-)%s*$", "%1"))
  483. end
  484.  
  485. function firstLetterUpper(str)  --Capitilizes first letter of @str
  486.     if not str then return nil end
  487.     str = tostring(str)
  488.     str = str:gsub("%a", string.upper, 1)
  489.     return str
  490. end
  491.  
  492. function titleCase(str)  --Changes @str to title case
  493.     if not str then return nil end
  494.     str = tostring(str)
  495.     local function tchelper(first, rest)
  496.         return first:upper()..rest:lower()
  497.     end
  498.     str = str:gsub("(%a)([%w_']*)", tchelper)
  499.     return str
  500. end
  501.  
  502. function isRepetition(str, pat)  --Checks if @str is a repetition of @pat
  503.     if not str then return nil end
  504.     str = tostring(str)
  505.     return "" == str:gsub(pat, "")
  506. end
  507.  
  508. function isRepetitionWS(str, pat)  --Checks if @str is a repetition of @pat seperated by whitespaces
  509.     if not str then return nil end
  510.     str = tostring(str)
  511.     return not str:gsub(pat, ""):find"%S"
  512. end
  513.  
  514. function urlDecode(str)  --Url decodes @str
  515.     if not str then return nil end
  516.     str = tostring(str)
  517.     str = string.gsub (str, "+", " ")
  518.     str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
  519.     str = string.gsub (str, "\r\n", "\n")
  520.     return str
  521. end
  522.  
  523. function urlEncode(str)  --Url encodes @str
  524.     if not str then return nil end
  525.     str = tostring(str)
  526.     if (str) then
  527.         str = string.gsub (str, "\n", "\r\n")
  528.         str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end)
  529.         str = string.gsub (str, " ", "+")
  530.     end
  531.     return str
  532. end
  533.  
  534. function isEmailAddress(str)  --Checks if @str is a valid email address
  535.     if not str then return nil end
  536.     str = tostring(str)
  537.     if (str:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")) then
  538.         return true
  539.     else
  540.         return false
  541.     end
  542. end
  543.  
  544. function chunk(str, size)  --Splits @str into chunks of length @size
  545.     if not size then return nil end
  546.     str = tostring(str)
  547.     local num2App = size - (#str%size)
  548.     str = str..(rep(char(0), num2App) or "")
  549.     assert(#str%size==0)
  550.     local chunks = {}
  551.     local numChunks = #str / size
  552.     local chunk = 0
  553.     while chunk < numChunks do
  554.         local start = chunk * size + 1
  555.         chunk = chunk+1
  556.         if start+size-1 > #str-num2App then
  557.             if str:sub(start, #str-num2App) ~= (nil or "") then
  558.                 chunks[chunk] = str:sub(start, #str-num2App)
  559.             end
  560.         else
  561.             chunks[chunk] = str:sub(start, start+size-1)
  562.         end
  563.     end
  564.     return chunks
  565. end
  566.  
  567. function find(str, match, startIndex)  --Finds @match in @str optionally after @startIndex
  568.     if not match then return nil end
  569.     str = tostring(str)
  570.     local _ = startIndex or 1
  571.     local _s = nil
  572.     local _e = nil
  573.     local _len = match:len()
  574.     while true do
  575.         local _t = str:sub( _ , _len + _ - 1)
  576.         if _t == match then
  577.             _s = _
  578.             _e = _ + _len - 1
  579.             break
  580.         end
  581.         _ = _ + 1
  582.         if _ > str:len() then break end
  583.     end
  584.     if _s == nil then return nil else return _s, _e end
  585. end
  586.  
  587. function seperate(str, divider)  --Separates @str on @divider
  588.     if not divider then return nil end
  589.     str = tostring(str)
  590.     local start = {}
  591.     local endS = {}
  592.     local n=1
  593.     repeat
  594.         if n==1 then
  595.             start[n], endS[n] = find(str, divider)
  596.         else
  597.             start[n], endS[n] = find(str, divider, endS[n-1]+1)
  598.         end
  599.         n=n+1
  600.     until start[n-1]==nil
  601.     local subs = {}
  602.     for n=1, #start+1 do
  603.         if n==1 then
  604.             subs[n] = str:sub(1, start[n]-1)
  605.         elseif n==#start+1 then
  606.             subs[n] = str:sub(endS[n-1]+1)
  607.         else
  608.             subs[n] = str:sub(endS[n-1]+1, start[n]-1)
  609.         end
  610.     end
  611.     return subs
  612. end
  613.  
  614. function replace(str, from, to)  --Replaces @from to @to in @str
  615.     if not from then return nil end
  616.     str = tostring(str)
  617.     local pcs = seperate(str, from)
  618.     str = pcs[1]
  619.     for n=2,#pcs do
  620.         str = str..to..pcs[n]
  621.     end
  622.     return str
  623. end
  624.  
  625. function jumble(str)  --Jumbles @str
  626.     if not str then return nil end
  627.     str = tostring(str)
  628.     local chars = {}
  629.     for i = 1, #str do
  630.         chars[i] = str:sub(i, i)
  631.     end
  632.     local usedNums = ":"
  633.     local res = ""
  634.     local rand = 0
  635.     for i=1, #chars do
  636.         while true do
  637.             rand = math.random(#chars)
  638.             if find(usedNums, ":"..rand..":") == nil then break end
  639.         end
  640.         res = res..chars[rand]
  641.         usedNums = usedNums..rand..":"
  642.     end
  643.     return res
  644. end
  645.  
  646. function toBase(str, base)  --Encodes @str in @base
  647.     if not base then return nil end
  648.     str = tostring(str)
  649.     local res = ""
  650.     for i = 1, str:len() do
  651.         if i == 1 then
  652.             res = basen(str:byte(i), base)
  653.         else
  654.             res = res..":"..basen(str:byte(i), base)
  655.         end
  656.     end
  657.     return res
  658. end
  659.  
  660. function fromBase(str, base)  --Decodes @str from @base
  661.     if not base then return nil end
  662.     str = tostring(str)
  663.     local bytes = seperate(str, ":")
  664.     local res = ""
  665.     for i = 1, #bytes do
  666.         res = res..(string.char(basen(tonumber(bytes[i], base), 10)))
  667.     end
  668.     return res
  669. end
  670.  
  671. function toBinary(str)  --Encodes @str in binary
  672.     if not str then return nil end
  673.     str = tostring(str)
  674.     return toBase(str, 2)
  675. end
  676.  
  677. function fromBinary(str)  --Decodes @str from binary
  678.     if not str then return nil end
  679.     str = tostring(str)
  680.     return fromBase(str, 2)
  681. end
  682.  
  683. function toOctal(str)  --Encodes @str in octal
  684.     if not str then return nil end
  685.     str = tostring(str)
  686.     return toBase(str, 8)
  687. end
  688.  
  689. function fromOctal(str)  --Decodes @str from octal
  690.     if not str then return nil end
  691.     str = tostring(str)
  692.     return fromBase(str, 8)
  693. end
  694.  
  695. function toHex(str)  --Encodes @str in hex
  696.     if not str then return nil end
  697.     str = tostring(str)
  698.     return toBase(str, 16)
  699. end
  700.  
  701. function fromHex(str)  --Decodes @str from hex
  702.     if not str then return nil end
  703.     str = tostring(str)
  704.     return fromBase(str, 16)
  705. end
  706.  
  707. function toBase36(str)  --Encodes @str in Base36
  708.     if not str then return nil end
  709.     str = tostring(str)
  710.     return toBase(str, 36)
  711. end
  712.  
  713. function fromBase36(str)  --Decodes @str from Base36
  714.     if not str then return nil end
  715.     str = tostring(str)
  716.     return fromBase(str, 36)
  717. end
  718.  
  719. function toBase32(str)  --Encodes @str in Base32
  720.     if not str then return nil end
  721.     str = tostring(str)
  722.     local byte=0
  723.     local bits=0
  724.     local rez=""
  725.     local i=0
  726.     for i = 1, str:len() do
  727.         byte=byte*256+str:byte(i)
  728.         bits=bits+8
  729.         repeat
  730.             bits=bits-5
  731.             local mul=(2^(bits))
  732.             local b32n=math.floor(byte/mul)
  733.             byte=byte-(b32n*mul)
  734.             b32n=b32n+1
  735.             rez=rez..string.sub(base32,b32n,b32n)
  736.         until bits<5
  737.     end
  738.     if bits>0 then
  739.         local b32n= math.fmod(byte*(2^(5-bits)),32)
  740.         b32n=b32n+1
  741.         rez=rez..string.sub(base32,b32n,b32n)
  742.     end
  743.     return rez
  744. end
  745.  
  746. function fromBase32(str)  --Decodes @str from Base32
  747.     if not str then return nil end
  748.     str = tostring(str)
  749.     local b32n=0
  750.     local bits=0
  751.     local rez=""
  752.     local i=0
  753.     string.gsub(str:upper(), "["..base32.."]", function (char)
  754.         local num = string.find(base32, char, 1, true)
  755.         b32n=b32n*32+(num - 1)
  756.         bits=bits+5
  757.         while  bits>=8 do
  758.             bits=bits-8
  759.             local mul=(2^(bits))
  760.             local byte = math.floor(b32n/mul)
  761.             b32n=b32n-(byte*mul)
  762.             rez=rez..string.char(byte)
  763.         end
  764.     end)
  765.     return rez
  766. end
  767.  
  768. function toBase64(str)  --Encodes @str in Base64
  769.     if not str then return nil end
  770.     str = tostring(str)
  771.     local bytes = {}
  772.     local result = ""
  773.     for spos=0,str:len()-1,3 do
  774.         for byte=1,3 do bytes[byte] = str:byte(spos+byte) or 0 end
  775.         result = string.format('%s%s%s%s%s',result,Base64.base64chars[Base64.rsh(bytes[1],2)],Base64.base64chars[Base64.lor(Base64.lsh((bytes[1] % 4),4), Base64.rsh(bytes[2],4))] or "=",((str:len()-spos) > 1) and Base64.base64chars[Base64.lor(Base64.lsh(bytes[2] % 16,2), Base64.rsh(bytes[3],6))] or "=",((str:len()-spos) > 2) and Base64.base64chars[(bytes[3] % 64)] or "=")
  776.     end
  777.     return result
  778. end
  779.  
  780. function fromBase64(str)  --Decodes @str from Base64
  781.     if not str then return nil end
  782.     str = tostring(str)
  783.     local chars = {}
  784.     local result=""
  785.     for dpos=0,str:len()-1,4 do
  786.         for char=1,4 do chars[char] = Base64.base64bytes[(str:sub((dpos+char),(dpos+char)) or "=")] end
  787.         result = string.format('%s%s%s%s',result,string.char(Base64.lor(Base64.lsh(chars[1],2), Base64.rsh(chars[2],4))),(chars[3] ~= nil) and string.char(Base64.lor(Base64.lsh(chars[2],4), Base64.rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(Base64.lor(Base64.lsh(chars[3],6) % 192, (chars[4]))) or "")
  788.     end
  789.     return result
  790. end
  791.  
  792. function rot13(str)  --Rot13s @str
  793.     if not str then return nil end
  794.     str = tostring(str)
  795.     local rot = ""
  796.     local len = str:len()
  797.     for i = 1, len do
  798.         local k = str:byte(i)
  799.         if (k >= 65 and k <= 77) or (k >= 97 and k <=109) then
  800.             rot = rot..string.char(k+13)
  801.         elseif (k >= 78 and k <= 90) or (k >= 110 and k <= 122) then
  802.             rot = rot..string.char(k-13)
  803.         else
  804.             rot = rot..string.char(k)
  805.         end
  806.     end
  807.     return rot
  808. end
  809.  
  810. function rot47(str)  --Rot47s @str
  811.     if not str then return nil end
  812.     str = tostring(str)
  813.     local rot = ""
  814.     for i = 1, str:len() do
  815.         local p = str:byte(i)
  816.         if p >= string.byte('!') and p <= string.byte('O') then
  817.             p = ((p + 47) % 127)
  818.         elseif p >= string.byte('P') and p <= string.byte('~') then
  819.             p = ((p - 47) % 127)
  820.         end
  821.         rot = rot..string.char(p)
  822.     end
  823.     return rot
  824. end
  825.  
  826. function SHA1(str) --Returns SHA1 Hash of @str
  827.     if not str then return nil end
  828.     str = tostring(str)
  829.     local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
  830.     local msg_len_in_bits = #str * 8
  831.     local first_append = char(0x80)
  832.     local non_zero_message_bytes = #str +1 +8
  833.     local current_mod = non_zero_message_bytes % 64
  834.     local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""
  835.     local B1, R1 = modf(msg_len_in_bits  / 0x01000000)
  836.     local B2, R2 = modf( 0x01000000 * R1 / 0x00010000)
  837.     local B3, R3 = modf( 0x00010000 * R2 / 0x00000100)
  838.     local B4      = 0x00000100 * R3
  839.     local L64 = char( 0) .. char( 0) .. char( 0) .. char( 0)
  840.             .. char(B1) .. char(B2) .. char(B3) .. char(B4)
  841.     str = str .. first_append .. second_append .. L64
  842.     assert(#str % 64 == 0)
  843.     local chunks = #str / 64
  844.     local W = { }
  845.     local start, A, B, C, D, E, f, K, TEMP
  846.     local chunk = 0
  847.     while chunk < chunks do
  848.         start,chunk = chunk * 64 + 1,chunk + 1
  849.         for t = 0, 15 do
  850.             W[t] = tSHA1.bytes_to_w32(str:byte(start, start + 3))
  851.             start = start + 4
  852.         end
  853.         for t = 16, 79 do
  854.             W[t] = tSHA1.w32_rot(1, tSHA1.w32_xor_n(W[t-3], W[t-8], W[t-14], W[t-16]))
  855.         end
  856.         A,B,C,D,E = H0,H1,H2,H3,H4
  857.         for t = 0, 79 do
  858.             if t <= 19 then
  859.                 f = tSHA1.w32_or(tSHA1.w32_and(B, C), tSHA1.w32_and(tSHA1.w32_not(B), D))
  860.                 K = 0x5A827999
  861.             elseif t <= 39 then
  862.                 f = tSHA1.w32_xor_n(B, C, D)
  863.                 K = 0x6ED9EBA1
  864.             elseif t <= 59 then
  865.                 f = tSHA1.w32_or3(tSHA1.w32_and(B, C), tSHA1.w32_and(B, D), tSHA1.w32_and(C, D))
  866.                 K = 0x8F1BBCDC
  867.             else
  868.                 f = tSHA1.w32_xor_n(B, C, D)
  869.                 K = 0xCA62C1D6
  870.             end
  871.             A,B,C,D,E = tSHA1.w32_add_n(tSHA1.w32_rot(5, A), f, E, W[t], K),
  872.             A, tSHA1.w32_rot(30, B), C, D
  873.         end
  874.         H0,H1,H2,H3,H4 = tSHA1.w32_add(H0, A),tSHA1.w32_add(H1, B),tSHA1.w32_add(H2, C),tSHA1.w32_add(H3, D),tSHA1.w32_add(H4, E)
  875.     end
  876.     local f = tSHA1.w32_to_hexstring
  877.     return f(H0) .. f(H1) .. f(H2) .. f(H3) .. f(H4)
  878. end
  879.  
  880. function CRC32(str) --Returns CRC32 Hash of @str
  881.     local crc, l, i = 0xFFFFFFFF, string.len(str)
  882.     for i = 1, l, 1 do
  883.         crc = bit.bxor(bit.brshift(crc, 8), CRC.crc32[bit.band(bit.bxor(crc, string.byte(str, i)), 0xFF) + 1])
  884.     end
  885.     return bit.bxor(crc, -1)
  886. end
  887.  
  888. function FCS16(str) --Returns FCS16 Hash of @str
  889.     local i
  890.     local l=string.len(str)
  891.     local uFcs16 = 65535
  892.     for i = 1,l do
  893.         uFcs16 = bit.bxor(bit.brshift(uFcs16,8), FCS["16"][bit.band(bit.bxor(uFcs16, string.byte(str,i)), 255)])
  894.     end
  895.     return  bit.bxor(uFcs16, 65535)
  896. end
  897.  
  898. function FCS32(str) --Returns FCS32 Hash of @str
  899.     local i
  900.     local l = string.len(str)
  901.     local uFcs32 = -1
  902.     for i=1,l do
  903.         uFcs32 = bit.bxor(bit.brshift(uFcs32,8), FCS["32"][bit.band(bit.bxor(uFcs32, string.byte(str,i)), 255)])
  904.     end
  905.     return bit.bnot(uFcs32)
  906. end
  907.  
  908. function encrypt(str, key)  --Encrypts @str with @key
  909.     if not key then return nil end
  910.     str = tostring(str)
  911.     local alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~"
  912.     local _rand = math.random(#alphabet-10)
  913.     local iv = string.sub(jumble(alphabet), _rand, _rand  + 9)
  914.     iv = jumble(iv)
  915.     str = iv..str
  916.     local key = SHA1(key)
  917.     local strLen = str:len()
  918.     local keyLen = key:len()
  919.     local j=1
  920.     local result = ""
  921.     for i=1, strLen do
  922.         local ordStr = string.byte(str:sub(i,i))
  923.         if j == keyLen then j=1 end
  924.         local ordKey = string.byte(key:sub(j,j))
  925.         result = result..string.reverse(basen(ordStr+ordKey, 36))
  926.         j = j+1
  927.     end
  928.     return result
  929. end
  930.  
  931. function decrypt(str, key)  --Decrypts @str with @key
  932.     if not key then return nil end
  933.     str = tostring(str)
  934.     local key = SHA1(key)
  935.     local strLen = str:len()
  936.     local keyLen = key:len()
  937.     local j=1
  938.     local result = ""
  939.     for i=1, strLen, 2 do
  940.         local ordStr = basen(tonumber(string.reverse(str:sub(i, i+1)),36),10)
  941.         if j==keyLen then j=1 end
  942.         local ordKey = string.byte(key:sub(j,j))
  943.         result = result..string.char(ordStr-ordKey)
  944.         j = j+1
  945.     end
  946.     return result:sub(11)
  947. end
  948.  
  949. function setRandSeed(seed)  --Sets random seed to @seed
  950.     math.randomseed(seed)
  951. end
  952.  
  953. setRandSeed(os.time())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement