Advertisement
dannysmc95

Current API

Jun 14th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.71 KB | None | 0 0
  1. -- Binary Conversions (To/From Binary)
  2. convert = {}
  3. convert.__index = convert
  4.  
  5. -- Base64 Encode and Decode
  6. basesf = {}
  7. basesf.__index = basesf
  8.  
  9. -- Draw Functions
  10. draw = {}
  11. draw.__index = draw
  12.  
  13. -- Configurations Functions
  14. config = {}
  15. config.__index = config
  16.  
  17. -- Database Functions
  18. db = {}
  19. db.__index = db
  20.  
  21. -- Encryption, Decryption and Hashing Functions
  22. crypt = {}
  23. crypt.__index = crypt
  24.  
  25. -- Colour Functions
  26. col = {}
  27. col.__index = col
  28.  
  29. -- Misc Functions
  30. misc = {}
  31. misc.__index = misc
  32.  
  33. -- Data Manipulation Functions
  34. data = {}
  35. data.__index = data
  36.  
  37. -- Help Functions
  38. help = {}
  39. help.__index = help
  40.  
  41. -- Image Manipulation Functions
  42. image = {}
  43. image.__index = image
  44.  
  45. -- Auto Load API's (on-the-fly) Functions
  46. apis = {}
  47. apis.__index = apis
  48.  
  49. -- Popup functions
  50. popup ={}
  51. popup.__index = popup
  52.  
  53. function popup.alert(text)
  54.     if text then
  55.         draw.box(1, 51, 9, 1, " ", "grey", "grey")
  56.         draw.textc(" "..text, 9, false, "white", "grey")
  57.     end
  58. end
  59.  
  60. function basesf.lsh(value,shift)
  61.     return (value*(2^shift)) % 256
  62. end
  63.  
  64. -- shift right
  65. function basesf.rsh(value,shift)
  66.     return math.floor(value/2^shift) % 256
  67. end
  68.  
  69. -- return single bit (for OR)
  70. function basesf.bit(x,b)
  71.     return (x % 2^b - x % 2^(b-1) > 0)
  72. end
  73.  
  74. -- logic OR for number values
  75. function basesf.lor(x,y)
  76.     result = 0
  77.     for p=1,8 do result = result + (((basesf.bit(x,p) or basesf.bit(y,p)) == true) and 2^(p-1) or 0) end
  78.     return result
  79. end
  80.  
  81. local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  82.  
  83. function basesf.encode(data)
  84.     local bytes = {}
  85.     local result = ""
  86.     for spos=0,string.len(data)-1,3 do
  87.         for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  88.         result = string.format('%s%s%s%s%s',result,base64chars[basesf.rsh(bytes[1],2)],base64chars[basesf.lor(basesf.lsh((bytes[1] % 4),4), basesf.rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[basesf.lor(basesf.lsh(bytes[2] % 16,2), basesf.rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  89.     end
  90.     return result
  91. end
  92.  
  93. local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
  94. function basesf.decode(data)
  95.     local chars = {}
  96.     local result=""
  97.     for dpos=0,string.len(data)-1,4 do
  98.         for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  99.         result = string.format('%s%s%s%s',result,string.char(basesf.lor(basesf.lsh(chars[1],2), basesf.rsh(chars[2],4))),(chars[3] ~= nil) and string.char(basesf.lor(basesf.lsh(chars[2],4), basesf.rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(basesf.lor(basesf.lsh(chars[3],6) % 192, (chars[4]))) or "")
  100.     end
  101.     return result
  102. end
  103.  
  104. function apis.load(urlcode)
  105.     aa = aa or {}
  106.     if urlcode:len() == 8 then
  107.         local a = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(tostring(urlcode)))
  108.     else
  109.         local a = http.get(textutils.urlEncode(tostring(urlcode)))
  110.     end
  111.     a = a.readAll()
  112.     local env = {}
  113.     a = loadstring(a)
  114.     local env = getfenv()
  115.     setfenv(a,env)
  116.     local status, err = pcall(a, unpack(aa))
  117.     if (not status) and err then
  118.         printError("Error loading api")
  119.         return false
  120.     end
  121.     local returned = err
  122.     env = env
  123.     _G["snet"] = env
  124. end
  125.  
  126. function config.save(table, file)
  127.   fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
  128.   fConfig.write(textutils.serialize(table))
  129.   fConfig.close()
  130. end
  131.  
  132. function config.load(file)
  133.   fConfig = fs.open(file, "r")
  134.   ret = textutils.unserialize(fConfig.readAll())
  135.   return ret
  136. end
  137.  
  138. imgColours = {
  139.     "white",
  140.     "orange",
  141.     "magenta",
  142.     "lightBlue",
  143.     "yellow",
  144.     "lime",
  145.     "pink",
  146.     "grey",
  147.     "lightGrey",
  148.     "cyan",
  149.     "purple",
  150.     "blue",
  151.     "brown",
  152.     "green",
  153.     "red",
  154.     "black",
  155. }
  156.  
  157.  
  158.  
  159. function image.draw(tablename, intx, inty)
  160.     if tablename then
  161.         if type(tablename) == "table" then
  162.             for k1, v1 in ipairs(tablename) do
  163.                 for k2, v2 in ipairs(tablename[k1]) do
  164.                     local bc = v2:sub(1, 2)-9
  165.                     local tc = v2:sub(4, 5)-9
  166.                     local char = v2:sub(7)
  167.                     term.setBackgroundColour(colours[imgColours[bc]])
  168.                     term.setTextColour(colours[imgColours[tc]])
  169.                     term.setCursorPos(k2+intx-1, k1+inty-1)
  170.                     write(char)
  171.                 end
  172.             end
  173.         end
  174.     end
  175. end
  176.  
  177. function image.save(tablename, filepath)
  178.     if fs.exists(filepath) then
  179.         print("[ERROR]: file already exists.")
  180.     else
  181.         local imgfile = fs.open(filepath, "w")
  182.         imgfile.write(textutils.serialize(tablename))
  183.         imgfile.close()
  184.     end
  185. end
  186.  
  187. function image.load(filepath)
  188.     local imgfile = fs.open(filepath, "r")
  189.     imgfile = imgfile.readAll()
  190.     imgfile.close()
  191.     return imgfile
  192. end
  193.  
  194. function draw.cscreen()
  195.     term.clear()
  196.     term.setCursorPos(1,1)
  197.     return
  198. end
  199.  
  200. function draw.textc(Text, Line, NextLine, Color, BkgColor)
  201.     local x, y = term.getSize()
  202.     x = x/2 - #Text/2
  203.     term.setCursorPos(x, Line)
  204.     if Color then
  205.         col.set(Color, BkgColor)
  206.     end
  207.     term.write(Text)
  208.     if NextLine then
  209.         term.setCursorPos(1, NextLine)
  210.     end
  211.     if Color then
  212.         col.reset(Color, BkgColor)
  213.     end
  214.     return true  
  215. end
  216.  
  217. function draw.texta(Text, xx, yy, NextLine, Color, BkgColor)
  218.     term.setCursorPos(xx,yy)
  219.     if Color then
  220.         col.set(Color, BkgColor)
  221.     end
  222.     term.write(Text)
  223.     if NextLine then  
  224.         term.setCursorPos(1, NextLine)
  225.     end
  226.     if Color then
  227.         col.reset(Color, BkgColor)
  228.     end
  229.     return true  
  230. end
  231.  
  232. function draw.cline(Line, NextLine)
  233.     local x, y = term.getSize()
  234.     for i = 1, x do
  235.         term.setCursorPos(i, Line)
  236.         term.write(" ")
  237.     end  
  238.     if not NextLine then  
  239.         x, y = term.getCursorPos()
  240.         term.setCursorPos(1, y+1)
  241.     end
  242.     return true  
  243. end
  244.  
  245. function draw.popup(text)
  246.     draw.box(1, 51, 8, 1, " ", "lime", "red")
  247.     draw.textc(text, 8, false, "lime", "red")
  248.     sleep(1.5)
  249. end
  250.  
  251. function draw.box(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor)
  252.     local x, y = term.getSize()
  253.     if Color then
  254.         col.set(Color, BkgColor)
  255.     end
  256.     if not Text then
  257.         Text = "*"
  258.     end
  259.     lengthX = lengthX - 1
  260.     lengthY = lengthY - 1
  261.     EndX = StartX + lengthX  
  262.     EndY = StartY + lengthY
  263.     term.setCursorPos(StartX, StartY)
  264.     term.write(string.rep(Text, lengthX))
  265.     term.setCursorPos(StartX, EndY)
  266.     term.write(string.rep(Text, lengthX))
  267.     for i = StartY, EndY do
  268.         term.setCursorPos(StartX, i)
  269.         term.write(Text)
  270.         term.setCursorPos(EndX, i)    
  271.         term.write(Text)
  272.     end
  273.     col.reset(Color, BkgColor)
  274.     return true  
  275. end
  276.  
  277. function db.delete(Filename)
  278.     if fs.exists(Filename) then
  279.         fs.delete(Filename)
  280.         return true
  281.     end
  282.     return false
  283. end
  284.  
  285. function db.load(Filename)
  286.     if not fs.exists(Filename) then
  287.         local F = fs.open(Filename, "w")
  288.         F.write("{}")
  289.         F.close()
  290.     end
  291.     local F = fs.open(Filename, "r")
  292.     local Data = F.readAll()
  293.     F.close()
  294.     Data = textutils.unserialize(Data)
  295.     return Data
  296. end
  297.  
  298. function db.save(Filename, ATable)
  299.     local Data = textutils.serialize(ATable)
  300.     local F = fs.open(Filename, "w")
  301.     F.write(Data)
  302.     F.close()
  303.     return true
  304. end
  305.  
  306. function db.search(searchstring, ATable)
  307.     for i, V in pairs(ATable) do
  308.         if tostring(ATable[i]) == tostring(searchstring) then
  309.             return i
  310.         end
  311.     end
  312.     return 0
  313. end
  314.  
  315. function db.removeString(Filename, AString)
  316.     local TempT = db.load(Filename)
  317.     if type(TempT) ~= "table" then
  318.         return false
  319.     end
  320.     local Pos = db.search(AString, TempT)
  321.     if Pos > 0 then
  322.         table.remove(TempT, Pos)
  323.         db.save(Filename, TempT)
  324.         return true
  325.     else
  326.         return false
  327.     end
  328. end
  329.  
  330. function db.insertString(Filename, AString)
  331.     local TempT = db.load(Filename)
  332.     if type(TempT) ~= "table" then
  333.         TempT = {}
  334.     end
  335.     table.insert(TempT, AString)
  336.     db.save(Filename, TempT)
  337.     return true
  338. end
  339.  
  340. local MOD = 2^32
  341. local MODM = MOD-1
  342. local function memoize(f)
  343.     local mt = {}
  344.     local t = setmetatable({}, mt)
  345.     function mt:__index(k)
  346.         local v = f(k)
  347.         t[k] = v
  348.         return v
  349.     end
  350.     return t
  351. end
  352. local function make_bitop_uncached(t, m)
  353.     local function bitop(a, b)
  354.         local res,p = 0,1
  355.         while a ~= 0 and b ~= 0 do
  356.             local am, bm = a % m, b % m
  357.             res = res + t[am][bm] * p
  358.             a = (a - am) / m
  359.             b = (b - bm) / m
  360.             p = p*m
  361.         end
  362.         res = res + (a + b) * p
  363.         return res
  364.     end
  365.     return bitop
  366. end
  367. local function make_bitop(t)
  368.     local op1 = make_bitop_uncached(t,2^1)
  369.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  370.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  371. end
  372. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  373. local function bxor(a, b, c, ...)
  374.     local z = nil
  375.     if b then
  376.         a = a % MOD
  377.         b = b % MOD
  378.         z = bxor1(a, b)
  379.         if c then z = bxor(z, c, ...) end
  380.         return z
  381.     elseif a then return a % MOD
  382.     else return 0 end
  383. end
  384. local function band(a, b, c, ...)
  385.     local z
  386.     if b then
  387.         a = a % MOD
  388.         b = b % MOD
  389.         z = ((a + b) - bxor1(a,b)) / 2
  390.         if c then z = bit32_band(z, c, ...) end
  391.         return z
  392.     elseif a then return a % MOD
  393.     else return MODM end
  394. end
  395. local function bnot(x) return (-1 - x) % MOD end
  396. local function rshift1(a, disp)
  397.     if disp < 0 then return lshift(a,-disp) end
  398.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  399. end
  400. local function rshift(x, disp)
  401.     if disp > 31 or disp < -31 then return 0 end
  402.     return rshift1(x % MOD, disp)
  403. end
  404. local function lshift(a, disp)
  405.     if disp < 0 then return rshift(a,-disp) end
  406.     return (a * 2 ^ disp) % 2 ^ 32
  407. end
  408. local function rrotate(x, disp)
  409.     x = x % MOD
  410.     disp = disp % 32
  411.     local low = band(x, 2 ^ disp - 1)
  412.     return rshift(x, disp) + lshift(low, 32 - disp)
  413. end
  414. local k = {
  415.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  416.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  417.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  418.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  419.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  420.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  421.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  422.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  423.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  424.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  425.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  426.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  427.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  428.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  429.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  430.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  431. }
  432. local function str2hexa(s)
  433.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  434. end
  435. local function num2s(l, n)
  436.     local s = ""
  437.     for i = 1, n do
  438.         local rem = l % 256
  439.         s = string.char(rem) .. s
  440.         l = (l - rem) / 256
  441.     end
  442.     return s
  443. end
  444. local function s232num(s, i)
  445.     local n = 0
  446.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  447.     return n
  448. end
  449. local function preproc(msg, len)
  450.     local extra = 64 - ((len + 9) % 64)
  451.     len = num2s(8 * len, 8)
  452.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  453.     assert(#msg % 64 == 0)
  454.     return msg
  455. end
  456. local function initH256(H)
  457.     H[1] = 0x6a09e667
  458.     H[2] = 0xbb67ae85
  459.     H[3] = 0x3c6ef372
  460.     H[4] = 0xa54ff53a
  461.     H[5] = 0x510e527f
  462.     H[6] = 0x9b05688c
  463.     H[7] = 0x1f83d9ab
  464.     H[8] = 0x5be0cd19
  465.     return H
  466. end
  467. local function digestblock(msg, i, H)
  468.     local w = {}
  469.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  470.     for j = 17, 64 do
  471.         local v = w[j - 15]
  472.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  473.         v = w[j - 2]
  474.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  475.     end
  476.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  477.     for i = 1, 64 do
  478.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  479.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  480.         local t2 = s0 + maj
  481.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  482.         local ch = bxor (band(e, f), band(bnot(e), g))
  483.         local t1 = h + s1 + ch + k[i] + w[i]
  484.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  485.     end
  486.     H[1] = band(H[1] + a)
  487.     H[2] = band(H[2] + b)
  488.     H[3] = band(H[3] + c)
  489.     H[4] = band(H[4] + d)
  490.     H[5] = band(H[5] + e)
  491.     H[6] = band(H[6] + f)
  492.     H[7] = band(H[7] + g)
  493.     H[8] = band(H[8] + h)
  494. end
  495. function crypt.sha256(msg)
  496.     msg = preproc(msg, #msg)
  497.     local H = initH256({})
  498.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  499.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  500.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  501. end
  502.  
  503. local function zfill(N)
  504.     N=string.format("%X",N)
  505.     Zs=""
  506.     if #N==1 then
  507.         Zs="0"
  508.     end
  509.     return Zs..N
  510. end
  511.  
  512. local function serializeImpl(t)
  513.     local sType = type(t)
  514.     if sType == "table" then
  515.         local lstcnt=0
  516.         for k,v in pairs(t) do
  517.             lstcnt = lstcnt + 1
  518.         end
  519.         local result = "{"
  520.         local aset=1
  521.         for k,v in pairs(t) do
  522.             if k==aset then
  523.                 result = result..serializeImpl(v)..","
  524.                 aset=aset+1
  525.             else
  526.                 result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  527.             end
  528.         end
  529.         result = result.."}"
  530.         return result
  531.     elseif sType == "string" then
  532.         return string.format("%q",t)
  533.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  534.         return tostring(t)
  535.     elseif sType == "function" then
  536.         local status,data=pcall(string.dump,t)
  537.         if status then
  538.             return 'func('..string.format("%q",data)..')'
  539.         else
  540.             error()
  541.         end
  542.     else
  543.         error()
  544.     end
  545. end
  546.  
  547. local function split(T,func)
  548.     if func then
  549.         T=func(T)
  550.     end
  551.     local Out={}
  552.     if type(T)=="table" then
  553.         for k,v in pairs(T) do
  554.             Out[split(k)]=split(v)
  555.         end
  556.     else
  557.         Out=T
  558.     end
  559.     return Out
  560. end
  561.  
  562. local function serialize( t )
  563.     t=split(t)
  564.     return serializeImpl( t, tTracking )
  565. end
  566.  
  567. local function unserialize( s )
  568.     local func, e = loadstring( "return "..s, "serialize" )
  569.     local funcs={}
  570.     if not func then
  571.         return e
  572.     end
  573.     setfenv( func, {
  574.         func=function(S)
  575.             local new={}
  576.             funcs[new]=S
  577.             return new
  578.         end,
  579.     })
  580.     return split(func(),function(val)
  581.         if funcs[val] then
  582.             return loadstring(funcs[val])
  583.         else
  584.             return val
  585.         end
  586.     end)
  587. end
  588.  
  589. local function sure(N,n)
  590.     if (l2-n)<1 then N="0" end
  591.     return N
  592. end
  593.  
  594. local function splitnum(S)
  595.     Out=""
  596.     for l1=1,#S,2 do
  597.         l2=(#S-l1)+1
  598.         CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
  599.         Out=string.char(CNum)..Out
  600.     end
  601.     return Out
  602. end
  603.  
  604. local function wrap(N)
  605.     return N-(math.floor(N/256)*256)
  606. end
  607.  
  608. function checksum(S,num) -- args strInput and intPassNumber
  609.     local sum=0
  610.     for char in string.gmatch(S,".") do
  611.         for l1=1,(num or 1) do
  612.             math.randomseed(string.byte(char)+sum)
  613.             sum=sum+math.random(0,9999)
  614.         end
  615.     end
  616.     math.randomseed(sum)
  617.     return sum
  618. end
  619.  
  620. local function genkey(len,psw)
  621.     checksum(psw)
  622.     local key={}
  623.     local tKeys={}
  624.     for l1=1,len do
  625.         local num=math.random(1,len)
  626.         while tKeys[num] do
  627.             num=math.random(1,len)
  628.         end
  629.         tKeys[num]=true
  630.         key[l1]={num,math.random(0,255)}
  631.     end
  632.     return key
  633. end
  634.  
  635. function crypt.encrypt(data,psw) -- args strInput and strPassword
  636.     data=serialize(data)
  637.     local chs=checksum(data)
  638.     local key=genkey(#data,psw)
  639.     local out={}
  640.     local cnt=1
  641.     for char in string.gmatch(data,".") do
  642.         table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
  643.         cnt=cnt+1
  644.     end
  645.     return string.sub(serialize({chs,table.concat(out)}),2,-3)
  646. end
  647.  
  648. function crypt.decrypt(data,psw) -- args strInput and strPassword
  649.     local oData=data
  650.     data=unserialize("{"..data.."}")
  651.     if type(data)~="table" then
  652.         return oData
  653.     end
  654.     local chs=data[1]
  655.     data=data[2]
  656.     local key=genkey((#data)/2,psw)
  657.     local sKey={}
  658.     for k,v in pairs(key) do
  659.         sKey[v[1]]={k,v[2]}
  660.     end
  661.     local str=splitnum(data)
  662.     local cnt=1
  663.     local out={}
  664.     for char in string.gmatch(str,".") do
  665.         table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
  666.         cnt=cnt+1
  667.     end
  668.     out=table.concat(out)
  669.     if checksum(out or "")==chs then
  670.         return unserialize(out)
  671.     end
  672.     return oData,out,chs
  673. end
  674.  
  675. function col.set(textColour, backgroundColour)
  676.     if textColour and backgroundColour then
  677.         if term.isColour() then
  678.             term.setTextColour(colours[textColour])
  679.             term.setBackgroundColour(colours[backgroundColour])
  680.             return true
  681.         else
  682.             return false
  683.         end
  684.     else
  685.         return false
  686.     end
  687. end
  688.  
  689. function col.screen(colour)
  690.     intX = 1
  691.     intY = 1
  692.     col.set(colour, colour)
  693.     repeat
  694.         intX = 1
  695.         repeat
  696.             term.setCursorPos(intX, intY)
  697.             write(" ")
  698.             intX = intX + 1
  699.         until intX == 52
  700.         intY = intY + 1
  701.     until intY == 20
  702. end
  703.  
  704. function col.reset()
  705.     if term.isColour then
  706.         term.setTextColour(colours.white)
  707.         term.setBackgroundColour(colours.black)
  708.         return true
  709.     else
  710.         return false
  711.     end
  712. end
  713.  
  714. function misc.find(Perihp)
  715.   for _,s in ipairs(rs.getSides()) do
  716.     if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  717.       return s  
  718.     end
  719.   end
  720.   return false
  721. end
  722.  
  723. function misc.serialgen(digits)
  724.   local serial
  725.   for i = 1, digits do
  726.     if i == 1 then
  727.       serial = math.random(9)
  728.     else
  729.       serial = serial.. math.random(9)
  730.     end
  731.   end
  732.   serial = tonumber(serial)
  733.   return serial
  734. end
  735.  
  736. function misc.time()
  737.     local nTime = textutils.formatTime(os.time(), true)
  738.     if string.len(nTime) == 4 then
  739.         nTime = "0"..nTime
  740.     end
  741.     os.startTimer(1)
  742.     return nTime
  743. end
  744.  
  745. function data.wordwrap(str, limit)
  746.   limit = limit or 72
  747.   local here = 1
  748.   local buf = ""
  749.   local t = {}
  750.   str:gsub("(%s*)()(%S+)()",
  751.   function(sp, st, word, fi)
  752.         if fi-here > limit then
  753.            --# Break the line
  754.            here = st
  755.            table.insert(t, buf)
  756.            buf = word
  757.         else
  758.            buf = buf..sp..word  --# Append
  759.         end
  760.   end)
  761.   --# Tack on any leftovers
  762.   if(buf ~= "") then
  763.         table.insert(t, buf)
  764.   end
  765.   return t
  766. end
  767.  
  768. function convert.split(str, delimiter)
  769.     local result = { }
  770.     local from = 1
  771.     local delim_from, delim_to = string.find( str, delimiter, from )
  772.     while delim_from do
  773.         table.insert( result, string.sub( str, from , delim_from-1 ) )
  774.         from = delim_to + 1
  775.         delim_from, delim_to = string.find( str, delimiter, from )
  776.     end
  777.     table.insert( result, string.sub( str, from  ) )
  778.     return result
  779. end
  780.  
  781. function convert.toBits(num)
  782.     local t={}
  783.     while num>0 do
  784.         rest=math.fmod(num,2)
  785.         t[#t+1]=rest
  786.         num=(num-rest)/2
  787.     end
  788.     return t
  789. end
  790.  
  791. function convert.makeEight( t )
  792.     return ("0"):rep( 8 - #t ) .. t
  793. end
  794.  
  795. function textToBinary( txt2 )
  796.     local txt = ""
  797.     for i=1, #txt2 do
  798.             txt = txt .. convert.makeEight( table.concat( convert.toBits( string.byte( txt2:sub( i, i ) ) ) ):reverse() )
  799.             sleep(0)
  800.     end
  801.     return txt
  802. end
  803.  
  804. function binaryToText( txt )
  805.     local txt2 = ""
  806.     local t = convert.split( txt, "\n" )
  807.     for k, v in pairs( t ) do
  808.         q = 1
  809.         for i=1, #v / 8 do
  810.             txt2 = txt2 .. string.char( tonumber( v:sub( q, q + 7), 2 ) )
  811.             q = q + 8
  812.         end
  813.         sleep(0)
  814.     end
  815.     return txt2
  816. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement