Guest User

Untitled

a guest
Nov 12th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. local FILLER = 61
  2.  
  3. local Alphabet = {}
  4. local Indexes = {}
  5.  
  6. for index = 65, 90 do table.insert(Alphabet, index) end -- A-Z
  7. for index = 97, 122 do table.insert(Alphabet, index) end -- a-z
  8. for index = 48, 57 do table.insert(Alphabet, index) end -- 0-9
  9.  
  10. table.insert(Alphabet, 43) -- +
  11. table.insert(Alphabet, 47) -- /
  12.  
  13. for index, character in pairs(Alphabet) do
  14. Indexes[character] = index
  15. end
  16.  
  17. local function BuildString(values)
  18. local output = {}
  19.  
  20. for index = 1, #values, 4096 do
  21. table.insert(output, string.char(
  22. unpack(values, index, math.min(index + 4096 - 1, #values))
  23. ))
  24. end
  25.  
  26. return table.concat(output, "")
  27. end
  28.  
  29. local Base64 = {}
  30.  
  31. function Base64.Encode(input)
  32. local output = {}
  33.  
  34. for index = 1, #input, 3 do
  35. local C1, C2, C3 = string.byte(input, index, index + 2)
  36.  
  37. local A = bit.rshift(C1, 2)
  38. local B = bit.lshift(bit.band(C1, 3), 4) + bit.rshift(C2 or 0, 4)
  39. local C = bit.lshift(bit.band(C2 or 0, 15), 2) + bit.rshift(C3 or 0, 6)
  40. local D = bit.band(C3 or 0, 63)
  41.  
  42. output[#output + 1] = Alphabet[A + 1]
  43. output[#output + 1] = Alphabet[B + 1]
  44. output[#output + 1] = C2 and Alphabet[C + 1] or Filler
  45. output[#output + 1] = C3 and Alphabet[D + 1] or Filler
  46. end
  47.  
  48. return BuildString(output)
  49. end
  50.  
  51. function Base64.Decode(input)
  52. local output = {}
  53.  
  54. for index = 1, #input, 4 do
  55. local C1, C2, C3, C4 = string.byte(input, index, index + 3)
  56.  
  57. local I1 = Indexes[C1] - 1
  58. local I2 = Indexes[C2] - 1
  59. local I3 = (Indexes[C3] or 1) - 1
  60. local I4 = (Indexes[C4] or 1) - 1
  61.  
  62. local A = bit.lshift(I1, 2) + bit.rshift(I2, 4)
  63. local B = bit.lshift(bit.band(I2, 15), 4) + bit.rshift(I3, 2)
  64. local C = bit.lshift(bit.band(I3, 3), 6) + I4
  65.  
  66. output[#output + 1] = A
  67. if C3 ~= Filler then output[#output + 1] = B end
  68. if C4 ~= Filler then output[#output + 1] = C end
  69. end
  70.  
  71. return BuildString(output)
  72. end
  73.  
  74. return Base64
Add Comment
Please, Sign In to add comment