Last active
August 29, 2015 13:57
-
-
Save alseambusher/9547090 to your computer and use it in GitHub Desktop.
crypt-editor uses mcrypt to encrypt and decrypt text enabling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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