Skip to content

Instantly share code, notes, and snippets.

@alseambusher
Last active August 29, 2015 13:57
Show Gist options
  • Save alseambusher/9547090 to your computer and use it in GitHub Desktop.
Save alseambusher/9547090 to your computer and use it in GitHub Desktop.
crypt-editor uses mcrypt to encrypt and decrypt text enabling
#!/bin/bash
usage()
{
cat <<EOF
Usage $0:
crypt-editor -help
crypt-editor [filename]
crypt-editor [editor] [filename]
crypt-editor [-k key] [filename]
crypt-editor [-k key] [editor] [filename]
EOF
}
while getopts "hk:" OPTION
do
case $OPTION in
h)
usage
exit 0
;;
k)
pass=$OPTARG
editor=$3
file=$4
;;
esac
done
# take password
if [ -z $pass ]
then
echo -n "Passphrase: "
stty -echo
read pass
editor=$1
file=$2
fi
# set editor
# TODO bug: opening .nc file in another directory.
if [ ! `type -P $editor` ] || [ -f $editor ]
then
echo "Setting editor as vi.."
file=$editor
editor="vi"
fi
# check for .nc extension
if [ `echo $file| grep ".*\.nc"` ]
then
file=`expr "$file" : "\(.*\)\.nc"`
fi
# decrypt it
if [ -f $file.nc ]
then
mcrypt -q -k $pass -d $file.nc
# if password is invalid
if [ $? -ne 0 ]
then
stty sane
exit 1
fi
fi
# open decrypted file
$editor $file
# encrypt it again
cat $file|mcrypt -q -k $pass >$file.nc
# remove temporary files
rm $file $file~ *$file.swp 2>/dev/null
stty sane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment