Skip to content

Instantly share code, notes, and snippets.

@greymd
Created November 3, 2016 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greymd/59781d9132cc4f67229896a57638133a to your computer and use it in GitHub Desktop.
Save greymd/59781d9132cc4f67229896a57638133a to your computer and use it in GitHub Desktop.
base64 encoder given by build-in linux commands.
base64sh() {
local input=${1:-$(cat)}
paste <(seq 0 63 | sed 's/^/obase=2;ibase=10;/' | bc | sed 's/^/00000/' | grep -oE '.{6}$') \
<(echo {A..Z} {a..z} {0..9} + / | xargs -n 1) | \
awk '{print "s|"$1"|"$2"|"}' | \
sed -f - <(echo -n $input| perl -ne 'print unpack("B*", $_)."0000"' | \
fold -w 6 | grep -oE '.{6}$') | tr -d '\n' | fold -w 4 | sed 's/$/===/' | grep -oE '^.{4}' | tr -d '\n' | awk 1
}
# Usage: base64sh
#
# $ echo Yes高須クリニック | base64sh
# WWVz6auY6aCI44Kv44Oq44OL44OD44Kv
#
# $ base64sh 'This is a pen'
# VGhpcw==
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment