Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/other/boatbomber-HashLib/Base64.lua
2727 views
1
-- @original: https://gist.github.com/Reselim/40d62b17d138cc74335a1b0709e19ce2
2
local Alphabet = {}
3
local Indexes = {}
4
5
-- A-Z
6
for Index = 65, 90 do
7
table.insert(Alphabet, Index)
8
end
9
10
-- a-z
11
for Index = 97, 122 do
12
table.insert(Alphabet, Index)
13
end
14
15
-- 0-9
16
for Index = 48, 57 do
17
table.insert(Alphabet, Index)
18
end
19
20
table.insert(Alphabet, 43) -- +
21
table.insert(Alphabet, 47) -- /
22
23
for Index, Character in ipairs(Alphabet) do
24
Indexes[Character] = Index
25
end
26
27
local Base64 = {}
28
29
local bit32_rshift = bit32.rshift
30
local bit32_lshift = bit32.lshift
31
local bit32_band = bit32.band
32
33
--[[**
34
Encodes a string in Base64.
35
@param [t:string] Input The input string to encode.
36
@returns [t:string] The string encoded in Base64.
37
**--]]
38
function Base64.Encode(Input)
39
local Output = {}
40
local Length = 0
41
42
for Index = 1, #Input, 3 do
43
local C1, C2, C3 = string.byte(Input, Index, Index + 2)
44
45
local A = bit32_rshift(C1, 2)
46
local B = bit32_lshift(bit32_band(C1, 3), 4) + bit32_rshift(C2 or 0, 4)
47
local C = bit32_lshift(bit32_band(C2 or 0, 15), 2) + bit32_rshift(C3 or 0, 6)
48
local D = bit32_band(C3 or 0, 63)
49
50
Length = Length + 1
51
Output[Length] = Alphabet[A + 1]
52
53
Length = Length + 1
54
Output[Length] = Alphabet[B + 1]
55
56
Length = Length + 1
57
Output[Length] = C2 and Alphabet[C + 1] or 61
58
59
Length = Length + 1
60
Output[Length] = C3 and Alphabet[D + 1] or 61
61
end
62
63
local NewOutput = {}
64
local NewLength = 0
65
local IndexAdd4096Sub1
66
67
for Index = 1, Length, 4096 do
68
NewLength = NewLength + 1
69
IndexAdd4096Sub1 = Index + 4096 - 1
70
71
NewOutput[NewLength] = string.char(
72
table.unpack(Output, Index, IndexAdd4096Sub1 > Length and Length or IndexAdd4096Sub1)
73
)
74
end
75
76
return table.concat(NewOutput)
77
end
78
79
--[[**
80
Decodes a string from Base64.
81
@param [t:string] Input The input string to decode.
82
@returns [t:string] The newly decoded string.
83
**--]]
84
function Base64.Decode(Input)
85
local Output = {}
86
local Length = 0
87
88
for Index = 1, #Input, 4 do
89
local C1, C2, C3, C4 = string.byte(Input, Index, Index + 3)
90
91
local I1 = Indexes[C1] - 1
92
local I2 = Indexes[C2] - 1
93
local I3 = (Indexes[C3] or 1) - 1
94
local I4 = (Indexes[C4] or 1) - 1
95
96
local A = bit32_lshift(I1, 2) + bit32_rshift(I2, 4)
97
local B = bit32_lshift(bit32_band(I2, 15), 4) + bit32_rshift(I3, 2)
98
local C = bit32_lshift(bit32_band(I3, 3), 6) + I4
99
100
Length = Length + 1
101
Output[Length] = A
102
103
if C3 ~= 61 then
104
Length = Length + 1
105
Output[Length] = B
106
end
107
108
if C4 ~= 61 then
109
Length = Length + 1
110
Output[Length] = C
111
end
112
end
113
114
local NewOutput = {}
115
local NewLength = 0
116
local IndexAdd4096Sub1
117
118
for Index = 1, Length, 4096 do
119
NewLength = NewLength + 1
120
IndexAdd4096Sub1 = Index + 4096 - 1
121
122
NewOutput[NewLength] = string.char(
123
table.unpack(Output, Index, IndexAdd4096Sub1 > Length and Length or IndexAdd4096Sub1)
124
)
125
end
126
127
return table.concat(NewOutput)
128
end
129
130
return Base64
131
132