#!/bin/bash set -euo pipefail SCRIPT_PATH=$(dirname "$(readlink -f "$0")";) function help { echo " sign.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: ./sign.sh --import_keys ./sign.sh --decrypt token.txt.asc ./sign.sh --encrypt token.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