ReIative

ComputerCraft relench

May 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.              relenc
  3.  an "encryption" library for CCLua
  4.  
  5.  made by relative
  6.  
  7.  Credits:
  8.    base64 - lua-users.org/wiki/BaseSixtyFour
  9. --]]
  10. --[[ BASE64 ]]--
  11. local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  12. local function base64encode(data)
  13.     return ((data:gsub('.', function(x)
  14.       local r,b='',x:byte()
  15.       for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  16.       return r;
  17.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  18.       if(#x < 6) then return '' end
  19.       local c=0
  20.       for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  21.       return b:sub(c+1,c+1)
  22.     end)..({'', '==', '='})[#data%3+1])
  23. end
  24. local function base64decode(data)
  25.   data = string.gsub(data, '[^'..b..'=]', '')
  26.   return (data:gsub('.', function(x)
  27.     if (x=='=') then return '' end
  28.     local r,f='',(b:find(x)-1)
  29.     for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  30.     return r;
  31.   end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  32.     if(#x ~= 8) then return '' end
  33.     local c=0
  34.     for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  35.     return string.char(c)
  36.   end))
  37. end
  38. local ENCTESTP = "RU5DT0RFX1RFU1Q="
  39. local ENCTESTS = base64encode("ENCODE_TEST")
  40. local DECTESTP = "DECODE_TEST"
  41. local DECTESTS = base64decode(base64encode(DECTESTP))
  42. if ENCTESTP ~= ENCTESTS then
  43.   error("Base64 encode test FAILED.")
  44. end
  45. if DECTESTP ~= DECTESTS then
  46.   error("Base64 decode test FAILED.")
  47. end
  48.  
  49. --[[ Encoding ]]--
  50.  
  51. function encode(str)
  52.   return base64encode(str)
  53. end
  54.  
  55. function decode(str)
  56.   return base64decode(str)
  57. end
Add Comment
Please, Sign In to add comment