Skip to content

Instantly share code, notes, and snippets.

@DeivAstra
Last active November 29, 2019 23:35
Show Gist options
  • Save DeivAstra/1127924bd9bde2e10e8d31f3e646223a to your computer and use it in GitHub Desktop.
Save DeivAstra/1127924bd9bde2e10e8d31f3e646223a to your computer and use it in GitHub Desktop.
Create / edit GPG encoded files by selected editor.
#!/bin/bash
if [ $# != 1 ]; then
printf "Usage: \n ./gpged file\n"
exit 1
fi
echo "Tip: run this script under separate user or root to make tmpfile unreadable for others"
FILE=$1
COUNT=197900000
GPG_ARGS="--s2k-mode 3 --s2k-count $COUNT --s2k-digest-algo SHA512 --s2k-cipher-algo AES256"
TMPFILE="$(mktemp -p /dev/shm/)"
EDITOR=nano
trap "shred -z -u $TMPFILE" EXIT
read -s -p "Enter password: " PASS
if [ -f $FILE ]; then
echo "Decrypt file: $FILE"
echo $TMPFILE
gpg -d --batch --passphrase $PASS $FILE > $TMPFILE
exitstatus=$?
if [ $exitstatus == 0 ]; then
echo "Decoded successful"
file_hash=`md5sum $TMPFILE`
$EDITOR $TMPFILE
file_hash_new=`md5sum $TMPFILE`
if [ "$file_hash" = "$file_hash_new" ]; then
echo "File not changed. Nothing to encrypt."
exit 0
fi
gpg $GPG_ARGS --output $FILE --batch --passphrase $PASS --yes -c $TMPFILE
exitstatus=$?
if [ $exitstatus == 0 ]; then echo "Encoded successful"; fi
else
echo "Decode failed"
fi
else
echo "Create file: $FILE"
echo "Created: `date`" | gpg $GPG_ARGS --batch --passphrase $PASS -c --output $FILE -
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment