Advertisement
justplayerde

jutils.lua

Dec 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | None | 0 0
  1. --[[
  2.     This is a util code for Computercraft
  3.     download:
  4.     pastebin get zLQD13rf jutils
  5.  
  6.     then load it with
  7.     os.loadAPI("jutils")
  8.  
  9.     usage example:
  10.     jutils.Decrypt("VmZ0ZlZmBGtwAQVjVmDlAvZmAQRwAQN5VmDkAPZ0ZQxwZmDkVmDkAPZ0ZwHwZmDlVmZ0Zj==","thekey")
  11.    
  12. --]]
  13.  
  14. Jutils = {}
  15.  
  16. -- string stuff
  17. function split(text, delimiter)
  18.   if text.find(text, delimiter) == nil then
  19.     return { text }
  20.   end
  21.  
  22.   local splited_texts = {}
  23.   local last_position
  24.  
  25.   for splited_text, position in text:gmatch("(.-)"..delimiter.."()") do
  26.     table.insert(splited_texts, splited_text)
  27.     last_position = position
  28.   end
  29.   table.insert(splited_texts, string.sub(text, last_position))
  30.   return splited_texts
  31. end
  32.  
  33. function split_lines(text)
  34.   local lines = {}
  35.   local function splitter(line)
  36.     table.insert(lines, line)
  37.     return ""
  38.   end
  39.   text:gsub("(.-\r?\n)", splitter)
  40.   return lines
  41. end
  42.  
  43. -- Returns the ASCII bytecode of either 'a' or 'A'
  44. function ascii_base(s)
  45.   return s:lower() == s and ('a'):byte() or ('A'):byte()
  46. end
  47.  
  48. -- ROT13 is based on Caesar ciphering algorithm, using 13 as a key
  49. function caesar_cipher(str, key)
  50.   return (str:gsub('%a', function(s)
  51.     local base = ascii_base(s)
  52.     return string.char(((s:byte() - base + key) % 26) + base)
  53.   end))
  54. end
  55.  
  56. -- str     : a string to be ciphered
  57. -- returns : the ciphered string
  58. function rot13(str)
  59.   return caesar_cipher(str, 13)
  60. end
  61.  
  62. -- str     : a string to be deciphered
  63. -- returns : the deciphered string
  64. function derot13(str)
  65.   return caesar_cipher(str, -13)
  66. end
  67.  
  68.  
  69. function _keyToInt(str)
  70.     local z=0;
  71.     for i = 1, #str do
  72.         z= z+string.byte(i)
  73.     end
  74.     return z
  75. end
  76. -- number stuff
  77. function round(num, n)
  78.   local mult = 10^(n or 0)
  79.   return math.floor(num * mult + 0.5) / mult
  80. end
  81.  
  82.  
  83. -- table stuff
  84. function dump(table)
  85.   for key, value in pairs(table) do
  86.     print("["..key.."] ", value)
  87.   end
  88. end
  89.  
  90. function equal(table1, table2)
  91.   local result = true
  92.  
  93.   for i=1, #table1 do
  94.     if table1[i] ~= table2[i] then
  95.       result = false
  96.       break
  97.     end
  98.   end
  99.  
  100.   return result
  101. end
  102.  
  103. -- other stuff
  104.  
  105. -- bitshift functions (<<, >> equivalent)
  106. -- shift left
  107. function lsh(value,shift)
  108.     return (value*(2^shift)) % 256
  109. end
  110.  
  111. -- shift right
  112. function rsh(value,shift)
  113.     return math.floor(value/2^shift) % 256
  114. end
  115.  
  116. -- return single bit (for OR)
  117. function bit(x,b)
  118.     return (x % 2^b - x % 2^(b-1) > 0)
  119. end
  120.  
  121. -- logic OR for number values
  122. function lor(x,y)
  123.     local bit = bit
  124.     result = 0
  125.     for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  126.     return result
  127. end
  128.  
  129. -- encryption table
  130. local base64chars = {
  131. [0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',
  132. [9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',
  133. [17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',
  134. [25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',
  135. [33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',
  136. [41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',
  137. [49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',
  138. [57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  139.  
  140. -- function encode
  141. -- encodes input string to base64.
  142. function enc(data)
  143.     local bytes = {}
  144.     local result = ""
  145.     local rsh = rsh
  146.     local lsh = lsh
  147.     local lor = lor
  148.     local strformat = string.format
  149.     local strchar = string.char
  150.     local base64chars = base64chars
  151.    
  152.     for spos=0,string.len(data)-1,3 do
  153.         for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  154.         result = strformat('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  155.     end
  156.     return result
  157. end
  158.  
  159. -- decryption table
  160. 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}
  161.  
  162. -- function decode
  163. -- decode base64 input to string
  164. function dec(data)
  165.     local chars = {}
  166.     local result=""
  167.     local rsh = rsh
  168.     local lsh = lsh
  169.     local lor = lor
  170.     local strformat = string.format
  171.     local strchar = string.char
  172.     local base64chars = base64chars
  173.        
  174.     for dpos=0,string.len(data)-1,4 do
  175.         for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  176.         result = strformat('%s%s%s%s',result,strchar(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and strchar(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and strchar(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
  177.     end
  178.     return result
  179. end
  180.  
  181. -- some type of encryption here
  182. function Encrypt(str,encryptionKey)
  183.     local str = textutils.serialize( str )
  184.     local encrypted = ''
  185.     for i = 1, #str do
  186.         encrypted = encrypted .. '#' .. string.byte(str:sub(i, i)) + _keyToInt(encryptionKey)
  187.     end
  188.     return rot13( enc( derot13(encrypted)) )
  189. end
  190. function Decrypt(str,encryptionKey)
  191.     local decrypted = ''
  192.     local   str = derot13(str)
  193.             str = dec(str)
  194.             str = rot13(str)
  195.     for x in str:gmatch('#(%d+)') do
  196.         decrypted = decrypted .. string.char(x - _keyToInt(encryptionKey))
  197.     end
  198.     return textutils.unserialize(decrypted)
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement