If you want to have a file crypted, so that noone can see its contents unless they have the correct password, you can use "gpg" to cypher or decypher it. However, its use is a bit complicated.
So, I decided to write a small shell script, called "gpg-vi", which asks for a password, and lets you edit a file, symmetrically crypted using GnuPG with that password.
The script will not let anyone else in that machine to see the contents of the file, but warning: the script writes the contents of the file in plain in a file in /tmp, so that your user id, or root, can see that file until the edition is finished (or even later, because the contents may still be there in the disk after deleting the file).
## The product of my hobby, for your entertainment:
# If no filename given, or -h or --help: show usage, exit
[[ "$1" = '-h' ]] || [[ "$1" = '--help' ]] || ! [[ "$1" ]] && { echo "Usage: $0 <filename>" 1>&2; exit 1;}
# Create temporary file, if tempfile is not available: use mktemp
tmp=$(tempfile -p egpg- -s .tmp -d "$HOME" 2>/dev/null || mktemp "$HOME/egpg-XXXXXX.tmp") ||\
{ echo 'temporary file could not be created'; exit 1; }
read -s -u 0 -p 'Please enter passphrase: ' pw
echo -n ' ' # Countering 3 backspacess...
Md5(){
echo $(md5sum "$tmp" 2>/dev/null || md5 "$tmp" 2>/dev/nul) && return
echo "Neither md5sum nor md5 is present"
exit 1
}
if [[ -f "$1" ]]
then # File exists; get password, decrypt if possible, take md5
if ! gpg -q --force-mdc --passphrase-fd 0 -o - "$1" >"$tmp" 2>/dev/null <<<"$pw"
then
rm -f -- "$tmp"
echo "File $1 is not gpg encrypted"
exit 1
fi
MD5OLD=$(Md5)
else # File doesn't exist, see if it can be created
>"$1" || { echo "Can't create file $1"; exit 1;}
fi
# Edit the file (whether it pre-existed or is new), take the md5
"${VISUAL:-${EDITOR:-vi}}" "$tmp"
MD5NEW=$(Md5)
# If no error and the file is changed: encrypt, remove temporary file
! (($?)) && ! [[ "$MD5OLD" = $MD5NEW ]] \
&& gpg -q --force-mdc --passphrase-fd 0 -c -o - "$tmp" >"$1" 2>/dev/null <<<"$pw"
shred -u "$tmp" 2>/dev/null || srm -ll "$tmp" 2>/dev/null || rm -f -- "$tmp"
exit 0
New comment
Please, write down your name and what you want to say :-)