| 1 |
#!/usr/bin/env sh |
| 2 |
|
| 3 |
# Description: Encrypts selected files using gpg. Can encrypt |
| 4 |
# asymmetrically (key) or symmetrically (passphrase). |
| 5 |
# If asymmetric encryption is chosen a key can be |
| 6 |
# chosen from the list of capable public keys using fzf. |
| 7 |
# |
| 8 |
# Note: Symmetric encryption only works for a single (current) file as per gpg limitations |
| 9 |
# |
| 10 |
# Shell: POSIX compliant |
| 11 |
# Author: KlzXS |
| 12 |
|
| 13 |
# shellcheck disable=SC1090,SC1091 |
| 14 |
. "$(dirname "$0")"/.nnn-plugin-helper |
| 15 |
|
| 16 |
printf "(s)ymmetric, (a)symmetric? [default=a] " |
| 17 |
read -r symmetry |
| 18 |
|
| 19 |
if [ "$symmetry" = "s" ]; then |
| 20 |
gpg --symmetric "$1" |
| 21 |
else |
| 22 |
if nnn_use_selection; then |
| 23 |
clear_sel=1 |
| 24 |
# shellcheck disable=SC2154 |
| 25 |
files=$(tr '\0' '\n' < "$selection") |
| 26 |
else |
| 27 |
clear_sel=0 |
| 28 |
files=$1 |
| 29 |
fi |
| 30 |
|
| 31 |
keyids=$(gpg --list-public-keys --with-colons | grep -E "pub:(.*:){10}.*[eE].*:" | awk -F ":" '{print $5}') |
| 32 |
|
| 33 |
#awk needs literal $10 |
| 34 |
#shellcheck disable=SC2016 |
| 35 |
keyuids=$(printf "%s" "$keyids" | xargs -n1 -I{} sh -c 'gpg --list-key --with-colons "{}" | grep "uid" | awk -F ":" '\''{printf "%s %s\n", "{}", $10}'\''') |
| 36 |
|
| 37 |
recipient=$(printf "%s" "$keyuids" | fzf | awk '{print $1}') |
| 38 |
|
| 39 |
printf "%s" "$files" | xargs -n1 gpg --encrypt --recipient "$recipient" |
| 40 |
|
| 41 |
# Clear selection |
| 42 |
if [ "$clear_sel" -eq 1 ] && [ -p "$NNN_PIPE" ]; then |
| 43 |
printf "-" > "$NNN_PIPE" |
| 44 |
fi |
| 45 |
fi |