Skip to content

Instantly share code, notes, and snippets.

@alvis
Created Oct 17, 2019
Embed
What would you like to do?
bash: base64url encode and decode
# base64url encode
function base64url_encode {
(if [ -z "$1" ]; then cat -; else echo -n "$1"; fi) |
openssl base64 -e -A |
sed s/\\+/-/g |
sed s/\\//_/g |
sed -E s/=+$//
}
# base64url decode
function base64url_decode {
INPUT=$(if [ -z "$1" ]; then echo -n $(cat -); else echo -n "$1"; fi)
MOD=$(($(echo -n "$INPUT" | wc -c) % 4))
PADDING=$(if [ $MOD -eq 2 ]; then echo -n '=='; elif [ $MOD -eq 3 ]; then echo -n '=' ; fi)
echo -n "$INPUT$PADDING" |
sed s/-/+/g |
sed s/_/\\//g |
openssl base64 -d -A
}
@mramanujan

This comment has been minimized.

Copy link

@mramanujan mramanujan commented May 11, 2021

can you help me please
manilalramanujan@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment