Advertisement
Derek1017

New OS

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