Jetro

Base64.api

Nov 27th, 2021 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. local Data = {
  2.     Name = "Base64.api",
  3.     Version = "1.1.1",
  4.     Date = "16/2/2022",
  5.     Time = "10:59",
  6.     Author = "Unknown / http://www.computercraft.info/forums2/index.php?/topic/22560-base64-api/",
  7.     Pastebin = "zAKYhveJ",
  8. }
  9.  
  10. if fs.exists("OS/APIs/VersionUpdater.api") then
  11.     os.loadAPI("OS/APIs/VersionUpdater.api")
  12. else
  13.     error("VersionUpdater API not found")
  14. end
  15. _G["VersionUpdater"] = _G["VersionUpdater.api"]
  16. VersionUpdater.Update("Base64",Data)
  17.  
  18. -- Characters
  19. local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  20.  
  21. -- Encoding
  22. function encode(data)
  23.     return ((data:gsub('.', function(x)
  24.         local r,b='',x:byte()
  25.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  26.         return r;
  27.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  28.         if (#x < 6) then return '' end
  29.         local c=0
  30.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  31.         return b:sub(c+1,c+1)
  32.     end)..({ '', '==', '=' })[#data%3+1])
  33. end
  34.  
  35. -- Decoding
  36. function decode(data)
  37.     data = string.gsub(data, '[^'..b..'=]', '')
  38.     return (data:gsub('.', function(x)
  39.         if (x == '=') then return '' end
  40.         local r,f='',(b:find(x)-1)
  41.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  42.         return r;
  43.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  44.         if (#x ~= 8) then return '' end
  45.         local c=0
  46.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  47.         return string.char(c)
  48.     end))
  49. end
Add Comment
Please, Sign In to add comment