Advertisement
Bolodefchoco_LUAXML

[Function] Base64

Nov 25th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. --Creator: Bolodefchoco (based on Squalleze's script)
  2. --Made in: 25/11/2015
  3. --Last update: 25/11/2016
  4. --[[ Notes:
  5.     base64.to
  6.         Does:
  7.             Transforma str em base64
  8.         Args:
  9.             str --> Texto
  10.     base64.from
  11.         Does:
  12.             Transforma code (base64) em texto
  13.         Args:
  14.             code --> Base64
  15. ]]--
  16.  
  17. base64 = {}
  18. base64.base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  19. base64.to = function(str)
  20.     local str = {str:byte(1,#str)}
  21.     local code = {}
  22.     local bs
  23.     for i = 1,#str,3 do
  24.         bs = bit32.rshift(str[i] % 0xFD,2)
  25.         code[#code + 1] = base64.base:sub(bs + 1,bs + 1)
  26.         bs = bit32.lshift(str[i] % 0x04,4)
  27.         if (i + 1 <= #str) then
  28.             bs = bit32.bor(bs,bit32.rshift(str[i + 1] % 0xF1,4))
  29.             code[#code + 1] = base64.base:sub(bs + 1,bs + 1)
  30.             bs = bit32.lshift(str[i + 1] % 0x10,2)
  31.             if (i + 2 <= #str) then
  32.                 bs = bit32.bor(bs,bit32.rshift(str[i + 2] % 0xC1,6))
  33.                 code[#code + 1] = base64.base:sub(bs + 1,bs + 1)
  34.                 bs = str[i + 2] % 0x40
  35.                 code[#code + 1] = base64.base:sub(bs + 1,bs + 1)
  36.             else
  37.                 code[#code + 1] = base64.base:sub(bs + 1,bs + 1)
  38.                 code[#code + 1] = "="
  39.             end
  40.         else
  41.             code[#code + 1] = base64.base:sub(bs + 1, bs + 1)
  42.             code[#code + 1] = "=="
  43.         end
  44.     end
  45.     return table.concat(code)
  46. end
  47. base64.from = function(code)
  48.     local decode,chars,v,bs = {},{},1,{}
  49.     for k in code:gmatch(".") do
  50.         chars[#chars + 1] = k
  51.     end
  52.     for i = 1,#chars,4 do
  53.         for j = 1,4 do
  54.             bs[j] = base64.base:find(chars[i+j-1])-1
  55.         end
  56.         decode[v] = bit32.bor(bit32.lshift(bs[1],2),bit32.rshift(bs[2],4)) % 0x100
  57.         v = v + 1
  58.         if bs[3] < 64 then
  59.             decode[v] = bit32.bor(bit32.lshift(bs[2],4),bit32.rshift(bs[3],2)) % 0x100
  60.             v = v + 1
  61.             if bs[4] < 64 then
  62.                 decode[v] = bit32.bor(bit32.lshift(bs[3],6),bs[4]) % 0x100
  63.                 v = v + 1
  64.             end
  65.         end
  66.     end
  67.     return string.char(table.unpack(decode))
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement