Skip to content

Instantly share code, notes, and snippets.

@sbstp
Last active October 25, 2016 21:50
Show Gist options
  • Save sbstp/31ddf0b1f293d9b7f2d03ab64896bd6d to your computer and use it in GitHub Desktop.
Save sbstp/31ddf0b1f293d9b7f2d03ab64896bd6d to your computer and use it in GitHub Desktop.
#!/bin/bash
FILE="$1"
TEMP=`mktemp`
help() {
echo "This programs allows you to edit an encrypted file and"
echo "re-encrypt it as soon as you're done editing."
}
read_password() {
echo -n "Password: "
read -s PASSWORD
echo
}
edit_file() {
"${EDITOR:-nano}" "$TEMP"
}
decrypt_file() {
echo "$PASSWORD" | gpg -o "$TEMP" --yes --batch --passphrase-fd 0 -d "$FILE" &> /dev/null
chmod 600 "$TEMP"
}
encrypt_file() {
echo "$PASSWORD" | gpg -o "$FILE" --yes --batch --passphrase-fd 0 -c "$TEMP" &> /dev/null
}
if [ -f "$FILE" ] ; then
# file exists, decrypt it first
read_password
if decrypt_file ; then
edit_file
encrypt_file
echo "File modified and encrypted."
else
echo "Bad password."
fi
else
# file does not exist
echo "The file does not exist."
read_password
edit_file
encrypt_file
echo "New file created and encrypted."
fi
rm "$TEMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment