-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.sh
392 lines (366 loc) · 8.39 KB
/
base64.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#
# Copyright (c) 2015 Tristan Le Guern <tleguern@bouledef.eu>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#
# zsh can't execute this function properly because of its peculiar field
# splitting behaviour. Bash, dash, OpenBSD ksh, MirBSD ksh and yash all
# ignores IFS characters at the begining or end of a word. Zsh doesn't when
# IFS characters are not strictly space, tab or newline.
# It might result in an extra nul byte at the end of $_bytes.
#
encode_add_block() {
local _byte=""
local _bytes="$(echo "$1" | sed "s/./&$encode_us/g")"
OLDIFS="$IFS"
IFS="$encode_us"
for _byte in $_bytes; do
# Zsh treats IFS white spaces at the end of a word differently
# than the other shells: it creates a final null value.
# Ignore it.
#if [ "$_byte" -eq 0 ]; then
# continue
#fi
# ord() doesn't need a clean IFS but dectobin() does,
# because of its use of enum().
_byte="$(ord $_byte)"
IFS="$OLDIFS"
_byte="$(dectobin $_byte)"
encode_block="${encode_block}${_byte}"
IFS="$encode_us"
done
IFS="$OLDIFS"
}
encode_add_blocks() {
local _block_size="$1"
local _blocks="$2"
local _block=""
OLDIFS="$IFS"
IFS="$encode_us"
for _block in $_blocks; do
IFS="$OLDIFS"
if [ ${#_block} -ne $_block_size ]; then
encode_dirty_block=$_block
fi
encode_add_block "$_block"
encode_blocks="${encode_blocks}${encode_block}"
IFS="$encode_us"
# Reset for next time
encode_block=""
done
IFS="$OLDIFS"
}
encode_finalize() {
local _block_size="$1"
if [ $((${#encode_blocks} % _block_size)) -eq 0 ]; then
encode_dirty_block=""
fi
if [ -n "$encode_dirty_block" ]; then
local _blocklen="${#encode_dirty_block}"
encode_padded=$((_block_size - _blocklen))
encode_blocks="$(rightpad $encode_blocks $((8*encode_padded)))"
fi
}
encode_init() {
encode_block=""
encode_blocks=""
encode_dirty_block=""
encode_us="$(printf "\037")"
encode_padded=0
# Unset globing if the option is not already activated
if ! echo $- | grep f > /dev/null 2>&1; then
set -f
encode_resetf=1
else
encode_resetf=0
fi
}
encode_cleanup() {
if [ $encode_resetf -eq 1 ]; then
set +f
fi
unset encode_block
unset encode_blocks
unset encode_dirty_block
unset encode_us
unset encode_padded
unset encode_resetf
unset OLDIFS
}
base64_encode() {
encode_init
local _groups=3
local _bits=6
local _block=""
local _blocks="$(echo "$1"|sed -E "s/.{$_groups}/&$encode_us/g")"
local _outstr=""
encode_add_blocks $_groups "$_blocks"
encode_finalize $_groups
OLDIFS="$IFS"
IFS=" "
encode_blocks=$(echo $encode_blocks|sed -E "s/.{$_bits}/&$encode_us/g")
IFS="$encode_us"
for _block in $encode_blocks; do
IFS=" "
_block="$(bintodec $_block)"
case $_block in
0) _block="A";;
1) _block="B";;
2) _block="C";;
3) _block="D";;
4) _block="E";;
5) _block="F";;
6) _block="G";;
7) _block="H";;
8) _block="I";;
9) _block="J";;
10) _block="K";;
11) _block="L";;
12) _block="M";;
13) _block="N";;
14) _block="O";;
15) _block="P";;
16) _block="Q";;
17) _block="R";;
18) _block="S";;
19) _block="T";;
20) _block="U";;
21) _block="V";;
22) _block="W";;
23) _block="X";;
24) _block="Y";;
25) _block="Z";;
26) _block="a";;
27) _block="b";;
28) _block="c";;
29) _block="d";;
30) _block="e";;
31) _block="f";;
32) _block="g";;
33) _block="h";;
34) _block="i";;
35) _block="j";;
36) _block="k";;
37) _block="l";;
38) _block="m";;
39) _block="n";;
40) _block="o";;
41) _block="p";;
42) _block="q";;
43) _block="r";;
44) _block="s";;
45) _block="t";;
46) _block="u";;
47) _block="v";;
48) _block="w";;
49) _block="x";;
50) _block="y";;
51) _block="z";;
52) _block="0";;
53) _block="1";;
54) _block="2";;
55) _block="3";;
56) _block="4";;
57) _block="5";;
58) _block="6";;
59) _block="7";;
60) _block="8";;
61) _block="9";;
62) _block="+";;
63) _block="/";;
esac
_outstr="$_outstr$_block"
IFS="$encode_us"
done
IFS="$OLDIFS"
case $encode_padded in
1) echo $_outstr | sed 's/A$/=/';;
2) echo $_outstr | sed 's/AA$/==/';;
*) echo $_outstr;;
esac
encode_cleanup
}
base64url_encode() {
base64_encode "$@" | tr '+/' '-_'
}
base32_encode() {
encode_init
local _groups=5
local _bits=5
local _block=""
local _blocks="$(echo "$1"|sed -E "s/.{$_groups}/&$encode_us/g")"
local _outstr=""
encode_add_blocks $_groups "$_blocks"
encode_finalize $_groups
OLDIFS="$IFS"
IFS=" "
encode_blocks=$(echo $encode_blocks|sed -E "s/.{$_bits}/&$encode_us/g")
IFS="$encode_us"
for _block in $encode_blocks; do
IFS=" "
_block="$(bintodec $_block)"
case $_block in
0) _block="A";;
1) _block="B";;
2) _block="C";;
3) _block="D";;
4) _block="E";;
5) _block="F";;
6) _block="G";;
7) _block="H";;
8) _block="I";;
9) _block="J";;
10) _block="K";;
11) _block="L";;
12) _block="M";;
13) _block="N";;
14) _block="O";;
15) _block="P";;
16) _block="Q";;
17) _block="R";;
18) _block="S";;
19) _block="T";;
20) _block="U";;
21) _block="V";;
22) _block="W";;
23) _block="X";;
24) _block="Y";;
25) _block="Z";;
26) _block="2";;
27) _block="3";;
28) _block="4";;
29) _block="5";;
30) _block="6";;
31) _block="7";;
esac
_outstr="$_outstr$_block"
IFS="$encode_us"
done
IFS="$OLDIFS"
case $encode_padded in
1) echo $_outstr | sed 's/A$/=/';;
2) echo $_outstr | sed 's/AAA$/===/';;
3) echo $_outstr | sed 's/AAAA$/====/';;
4) echo $_outstr | sed 's/AAAAAA$/======/';;
*) echo $_outstr;;
esac
encode_cleanup
}
base32hex_encode() {
encode_init
local _groups=5
local _bits=5
local _block=""
local _blocks="$(echo "$1"|sed -E "s/.{$_groups}/&$encode_us/g")"
local _outstr=""
encode_add_blocks $_groups "$_blocks"
encode_finalize $_groups
OLDIFS="$IFS"
IFS=" "
encode_blocks=$(echo $encode_blocks|sed -E "s/.{$_bits}/&$encode_us/g")
IFS="$encode_us"
for _block in $encode_blocks; do
IFS=" "
_block="$(bintodec $_block)"
case $_block in
0) _block="0";;
1) _block="1";;
2) _block="2";;
3) _block="3";;
4) _block="4";;
5) _block="5";;
6) _block="6";;
7) _block="7";;
8) _block="8";;
9) _block="9";;
10) _block="A";;
11) _block="B";;
12) _block="C";;
13) _block="D";;
14) _block="E";;
15) _block="F";;
16) _block="G";;
17) _block="H";;
18) _block="I";;
19) _block="J";;
20) _block="K";;
21) _block="L";;
22) _block="M";;
23) _block="N";;
24) _block="O";;
25) _block="P";;
26) _block="Q";;
27) _block="R";;
28) _block="S";;
29) _block="T";;
30) _block="U";;
31) _block="V";;
esac
_outstr="$_outstr$_block"
IFS="$encode_us"
done
IFS="$OLDIFS"
case $encode_padded in
1) echo $_outstr | sed 's/0$/=/';;
2) echo $_outstr | sed 's/000$/===/';;
3) echo $_outstr | sed 's/0000$/====/';;
4) echo $_outstr | sed 's/000000$/======/';;
*) echo $_outstr;;
esac
encode_cleanup
}
base16_encode() {
encode_init
local _groups=1
local _bits=4
local _block=""
local _blocks="$(echo "$1"|sed -E "s/.{$_groups}/&$encode_us/g")"
local _outstr=""
encode_add_blocks $_groups "$_blocks"
encode_finalize $_groups
OLDIFS="$IFS"
IFS=" "
encode_blocks=$(echo $encode_blocks|sed -E "s/.{$_bits}/&$encode_us/g")
IFS="$encode_us"
for _block in $encode_blocks; do
IFS=" "
_block="$(bintodec $_block)"
case $_block in
0) _block="0";;
1) _block="1";;
2) _block="2";;
3) _block="3";;
4) _block="4";;
5) _block="5";;
6) _block="6";;
7) _block="7";;
8) _block="8";;
9) _block="9";;
10) _block="A";;
11) _block="B";;
12) _block="C";;
13) _block="D";;
14) _block="E";;
15) _block="F";;
esac
_outstr="$_outstr$_block"
IFS="$encode_us"
done
IFS="$OLDIFS"
echo $_outstr
encode_cleanup
}
hex_encode() {
base16_encode $*
}