Last active
November 7, 2023 17:25
-
-
Save toonetown/90b65f630ff51f570ff893b3637c5b17 to your computer and use it in GitHub Desktop.
Adds an option to encode/decode url-safe base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# Shell script which adds the ability to encode/decode web-safe URLs in base64. Do this by specifying | |
# | |
#!/bin/bash | |
ARGS=() | |
BAD_CHARS="+/=" | |
SAFE_CHARS="-_ " | |
URL="n"; CONVERT="n"; DECODE="n"; IN="-"; OUT="-"; HELP="n" | |
while (($#)); do | |
case "${1}" in | |
-h | --help) | |
HELP="y" | |
ARGS+=("${1}") | |
;; | |
-D | -d | --decode) | |
DECODE="y" | |
ARGS+=("${1}") | |
;; | |
-i | --input=*) | |
if [ "${1}" == "-i" ]; then shift; fi | |
IN="${1#--input=}" | |
;; | |
-o | --output=*) | |
if [ "${1}" == "-o" ]; then shift; fi | |
OUT="${1#--output=}" | |
;; | |
-u | --url) | |
URL="y" | |
;; | |
-c | --convert) | |
CONVERT="y" | |
;; | |
*) | |
ARGS+=("${1}") | |
;; | |
esac | |
shift | |
done | |
# Converts the stream (of unsafe characters) to safe characters | |
function to_safe { | |
tr -- "${BAD_CHARS}" "${SAFE_CHARS}" | sed -e 's/ *$//g' | |
} | |
# Converts the stream (of safe characters) to unsafe characters | |
function from_safe { | |
awk '{ L=length($1)/4; L=int((L==int(L))?L:int(L)+1)*4; printf "%-*s\n", L, $1; }' \ | |
| tr -- "${SAFE_CHARS}" "${BAD_CHARS}" | |
} | |
# Handle convert-only | |
if [ "${CONVERT}" == "y" ]; then | |
if [ "${OUT}" == "-" ]; then OUT="/dev/stdout"; fi | |
if [ "${DECODE}" == "y" ]; then | |
cat "${IN}" | from_safe > "${OUT}" | |
else | |
cat "${IN}" | to_safe > "${OUT}" | |
fi | |
exit $? | |
fi | |
# Handle if we specified url-safe | |
if [ "${URL}" == "y" ]; then | |
if [ "${DECODE}" == "y" ]; then | |
cat "${IN}" | from_safe | /usr/bin/base64 "${ARGS[@]}" -o "${OUT}" | |
else | |
if [ "${OUT}" == "-" ]; then OUT="/dev/stdout"; fi | |
/usr/bin/base64 "${ARGS[@]}" -i "${IN}" | to_safe > "${OUT}" | |
fi | |
exit $? | |
fi | |
# Just pass it through - but add our help | |
/usr/bin/base64 "${ARGS[@]}" -i "${IN}" -o "${OUT}" | |
RET=$? | |
if [ "${HELP}" == "y" ]; then | |
echo " -u, --url encode/decode url-safe strings" >&2 | |
echo " -c, --convert without -D, converts to url-safe strings" >&2 | |
echo " with -D, converts from url-safe strings" >&2 | |
fi | |
exit ${RET} |
The tr
is used for doing "url-safe" base64 output instead of standard output...it is the way to actually choose the alphabet that is used. See https://datatracker.ietf.org/doc/html/rfc4648#section-5. That is all this script does - it adds the -u
option to select "url-safe" operation, and then translates the output from the system-provided base64 command. All other command line options are passed through to the underlying base64 command.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't using tr on the base64 encoded output breaking the encoding? Acording to the RFC, https://tools.ietf.org/html/rfc3548.html#page-3, the alphabet needs to be chosen in the tool.