88 lines
1.5 KiB
Bash
Executable File
88 lines
1.5 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
SCRIPT_PATH=$(dirname "$(readlink -f "$0")";)
|
|
|
|
function help {
|
|
echo "
|
|
gpg-tool.sh [args] file
|
|
|
|
Commands:
|
|
-d, --decrypt decrypt file
|
|
-e, --encrypt encrypt file
|
|
-v, --view view encrypted file
|
|
-i, --import_keys import keys
|
|
-h, --help display help
|
|
|
|
Example:
|
|
./gpg-tool.sh --import_keys
|
|
./gpg-tool.sh --view secrets.txt.asc
|
|
./gpg-tool.sh --decrypt secrets.txt.asc
|
|
./gpg-tool.sh --encrypt secrets.txt
|
|
"
|
|
}
|
|
|
|
function decrypt {
|
|
FILE="$1"
|
|
gpg --decrypt-files "$SCRIPT_PATH/$FILE"
|
|
rm -r "$SCRIPT_PATH/$FILE"
|
|
}
|
|
|
|
function view {
|
|
FILE="$1"
|
|
gpg --decrypt "$SCRIPT_PATH"/"$FILE" 2>/dev/null
|
|
}
|
|
|
|
function import_keys {
|
|
for i in ls "$SCRIPT_PATH"/keys/*.pub
|
|
do
|
|
gpg --import "$i" 2>&1 | head -1 | awk '{print $3}' | sed 's/.$//' > /dev/null
|
|
done
|
|
echo 'All keys have been imported'
|
|
}
|
|
|
|
function get_recipients {
|
|
for i in "$SCRIPT_PATH"/keys/*.pub
|
|
do
|
|
allKeys+="--recipient $(gpg --import "$i" 2>&1 | head -1 | awk '{print $3}' | sed 's/.$//') "
|
|
done
|
|
echo "$allKeys"
|
|
}
|
|
|
|
function encrypt {
|
|
PUBKEYS=$(get_recipients)
|
|
FILE="$1"
|
|
gpg --encrypt-files --trust-model always $PUBKEYS --armor "$FILE"
|
|
}
|
|
|
|
if [ $# = 0 ]; then
|
|
help
|
|
exit
|
|
fi
|
|
|
|
case $1 in
|
|
-i| --import_keys)
|
|
import_keys && exit 0
|
|
;;
|
|
|
|
-d| --decrypt)
|
|
decrypt "$2"
|
|
;;
|
|
|
|
-e| --encrypt)
|
|
encrypt "$2"
|
|
;;
|
|
|
|
-v| --view)
|
|
view "$2"
|
|
;;
|
|
|
|
-h| --help)
|
|
help
|
|
exit
|
|
;;
|
|
|
|
*)
|
|
help
|
|
exit 0
|
|
;;
|
|
esac |