The Base64 url safe already supported by basenc command. But it may be not pre-installed on many platfroms.
You can call base64 command but there is a pitfalls:
-
On most Linux distros the
base64command comes fromcoreutilspackage. And it already have abasenccommand -
On OpenWrt the
base64is not installed by default and comes fromcoreutils-base64package and thebaseenccommand fromcoreutils-baseenc -
You can enable the
base64in BusyBox i.e. if you building the OpenWrt image yourself. -
You may use
openssl base64oropenssl enc -base64see docs but it will read by lines so you must also specify-Aparam. -
On MacOS the
-ddecode flag must be in upper casebase64 -Dso most users use--decodeflag. Previously MacOS used the-dfor debug but it's not clear who ever used it. Then Applce just removed the-dparam but again it's not clear why they don't changed it to be compatible with coreutils. Don't use any MacOS products. Just don't. -
Python also can be used
python3 -m base64 -d -
https://www.yuryoparin.com/2014/05/base64url-in-bash.html pure bash scripts: no any requiteremnts. But works too slowly.
-
https://gist.github.com/woniuzfb/717dc5dd19374bf9b6cd38078753a9ac wrapper uppon base64 to decode base64 url safe
Shell functions to convert base64 url safe to regular base64:
base64_padding()
{
local len=$(( ${#1} % 4 ))
local padded_b64=''
if [ ${len} = 2 ]; then
padded_b64="${1}=="
elif [ ${len} = 3 ]; then
padded_b64="${1}="
else
padded_b64="${1}"
fi
echo -n "$padded_b64"
}
base64url_to_b64()
{
base64_padding "${1}" | tr -- '-_' '+/'
}I created tickets to fix the discrapanceies:
See also
https://github.com/acmesh-official/acme.sh/blob/master/acme.sh#L1582