Skip to content

Instantly share code, notes, and snippets.

@npow
Forked from noqqe/rot13.bash
Created August 24, 2011 19:07
Show Gist options
  • Save npow/1168898 to your computer and use it in GitHub Desktop.
Save npow/1168898 to your computer and use it in GitHub Desktop.
An implementation of rot13 encryption written in pure bash
#!/bin/bash
# Choose your favorite table
# classic
SIGNS=( a b c d e f g h i j k l m n o p q r s t u v w x y z )
# advanced
#SIGNS=( a b c d f e h g j i l k m n o p q r s t u v w x y z . - ? ! "#" "+" )
# full
#SIGNS=( a b c d e f g h i j k l m n o p q r s t u v w x y z
#A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
#0 1 2 3 4 5 6 7 8 9 0 )
function query() {
nc=0
while [ $nc -lt ${#SIGNS[@]} ]; do
if [ "$1" = "${SIGNS[$nc]}" ]; then
local ORIGNUM=$nc
fi
((nc++))
done
if [ -z $ORIGNUM ]; then
echo -n "$1"
return
fi
ENCRYPTNUM=$(($ORIGNUM + ${#SIGNS[@]} / 2))
if [ $ENCRYPTNUM -ge ${#SIGNS[@]} ]; then
ENCRYPTNUM=$(($ENCRYPTNUM - ${#SIGNS[@]} ))
fi
echo -n ${SIGNS[$ENCRYPTNUM]}
}
function table() {
for x in ${SIGNS[@]}; do
echo -n "$x: "
query $x
echo
done
}
function main() {
if [ $# -eq 0 ]; then
set -- "$(cat /dev/stdin)"
fi
local sc=0
while [ $sc -lt ${#1} ]; do
if [ "${1:$sc:1}" = " " ]; then
echo -n " "
else
query "${1:$sc:1}"
fi
((sc++))
done
echo
}
case $1 in
-t) table ;;
*) main "$@" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment