Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local FILLER = 61
- local Alphabet = {}
- local Indexes = {}
- for index = 65, 90 do table.insert(Alphabet, index) end -- A-Z
- for index = 97, 122 do table.insert(Alphabet, index) end -- a-z
- for index = 48, 57 do table.insert(Alphabet, index) end -- 0-9
- table.insert(Alphabet, 43) -- +
- table.insert(Alphabet, 47) -- /
- for index, character in pairs(Alphabet) do
- Indexes[character] = index
- end
- local function BuildString(values)
- local output = {}
- for index = 1, #values, 4096 do
- table.insert(output, string.char(
- unpack(values, index, math.min(index + 4096 - 1, #values))
- ))
- end
- return table.concat(output, "")
- end
- local Base64 = {}
- function Base64.Encode(input)
- local output = {}
- for index = 1, #input, 3 do
- local C1, C2, C3 = string.byte(input, index, index + 2)
- local A = bit.rshift(C1, 2)
- local B = bit.lshift(bit.band(C1, 3), 4) + bit.rshift(C2 or 0, 4)
- local C = bit.lshift(bit.band(C2 or 0, 15), 2) + bit.rshift(C3 or 0, 6)
- local D = bit.band(C3 or 0, 63)
- output[#output + 1] = Alphabet[A + 1]
- output[#output + 1] = Alphabet[B + 1]
- output[#output + 1] = C2 and Alphabet[C + 1] or Filler
- output[#output + 1] = C3 and Alphabet[D + 1] or Filler
- end
- return BuildString(output)
- end
- function Base64.Decode(input)
- local output = {}
- for index = 1, #input, 4 do
- local C1, C2, C3, C4 = string.byte(input, index, index + 3)
- local I1 = Indexes[C1] - 1
- local I2 = Indexes[C2] - 1
- local I3 = (Indexes[C3] or 1) - 1
- local I4 = (Indexes[C4] or 1) - 1
- local A = bit.lshift(I1, 2) + bit.rshift(I2, 4)
- local B = bit.lshift(bit.band(I2, 15), 4) + bit.rshift(I3, 2)
- local C = bit.lshift(bit.band(I3, 3), 6) + I4
- output[#output + 1] = A
- if C3 ~= Filler then output[#output + 1] = B end
- if C4 ~= Filler then output[#output + 1] = C end
- end
- return BuildString(output)
- end
- return Base64
Add Comment
Please, Sign In to add comment