Skip to content

Instantly share code, notes, and snippets.

@ckirsch
Last active October 21, 2022 12:33
Show Gist options
  • Save ckirsch/5169892 to your computer and use it in GitHub Desktop.
Save ckirsch/5169892 to your computer and use it in GitHub Desktop.
Symmetrically encrypt multiple files with GnuPG
#!/bin/bash
###############################################################################
## name : gpgmulti.sh (original by Nick Montpetit)
## purpose : symmetrically encrypts/decrypts multiple files using GnuPG
## usage : gpgmulti.sh [-c] file_pattern
###############################################################################
# encrypt all non-gpg files recursively:
# find . -type f -not -name .DS_Store -not -name "*.gpg" -print0 | xargs -0 -o gpgmulti.sh -c
# decrypt all gpg files recursively:
# find . -type f -name "*.gpg" -print0 | xargs -0 -o gpgmulti.sh
###############################################################################
# This is the sha-2 value for your password.
# Encryption will only work if the user provides the password with that value.
# Replace this with the shasum value for your password - don't use this value!
#
pw_shasum=e4e6da319c78303b1e8721e0bcfdee799ecdd9de37155cf6bc485b340e36045b8a406999da1dfe38462ad0a619ffd697a450615bdb8bd6f001a6daed70ba89b5
###############################################################################
# prompt for password and hide it with -s
read -s -p "Enter password: " pw
echo ""
if [ "$1" = "-c" ]
then
shift
input_pw_shasum=$(echo $pw | shasum -t -a 512)
input_pw_shasum=${input_pw_shasum%% " "-}
if [ "$pw_shasum" = "$input_pw_shasum" ]
then
for file in "$@"
do
extension=${file##*.}
if [ "$extension" != "gpg" ]
then
# garble filenames
#hash_name=$(echo $file | shasum -t)
#hash_name=${hash_name%% " "-}
#gpg_name=$hash_name.$extension.gpg
# or maintain filenames
gpg_name=$file.gpg
echo "gpg -c:" $file "->" $gpg_name
echo $pw | gpg --batch -q -c --passphrase-fd 0 -o "$gpg_name" "$file"
# securely delete original
#shred -u $file
fi
done
else
echo "No encryption: password did not match"
fi
else
for file in "$@"
do
extension=${file##*.}
if [ "$extension" = "gpg" ]
then
original_name=${file%.*}
echo "gpg:" $file "->" $original_name
echo $pw | gpg --batch -q --passphrase-fd 0 "$file"
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment