0

I would like to separate imported pub keys (gpg --import) in groups to organize them.

For example, I have a bunch of pub keys I would like to keep for a long time vs others that I need to use once or twice and eventually delete them (without groups, I have to manually inspect all keys).

0

Figured it out using --keyring and --no-default-keyring.

Added a few functions to .zshrc to make things more efficient (on macOS, default keyring is called pubring.gpg).

# Search keys.openpgp.org
function search-keys() {
  if [ -z "$1" ]; then
    printf "%s\n" "Usage: search-keys [keyring] id"
    return 1
  fi
  if [ -z "$2" ]; then
    2=$1
    1=pubring.gpg
  fi
  if [ $1 != "pubring.gpg" ] && [ $1 != "tmp.gpg" ]; then
    printf "%s\n" "Invalid keyring $1"
    return 1
  fi
  gpg --keyring $1 --no-default-keyring --keyserver hkps://keys.openpgp.org --search-keys $2
}

# Import public key
function import() {
  if [ -z "$1" ]; then
    printf "%s\n" "Usage: import [keyring] /path/to/pubkey.asc"
    return 1
  fi
  if [ -z "$2" ]; then
    2=$1
    1=pubring.gpg
  fi
  if [ $1 != "pubring.gpg" ] && [ $1 != "tmp.gpg" ]; then
    printf "%s\n" "Invalid keyring $1"
    return 1
  fi
  gpg --keyring $1 --no-default-keyring --import $2
}

# List public keys
function list-keys() {
  if [ -z "$1" ]; then
    1=pubring.gpg
  fi
  if [ $1 != "pubring.gpg" ] && [ $1 != "tmp.gpg" ]; then
    printf "%s\n" "Invalid keyring $1"
    return 1
  fi
  gpg --keyring $1 --no-default-keyring --list-keys $2
}

# Encrypt message for id
function encrypt() {
  if [ -z "$1" ]; then
    printf "%s\n" "Usage: encrypt id"
    return 1
  fi
  gpg --keyring "tmp.gpg" --encrypt --sign --armor -r 0xC1323A377DE14C8B -r $1
}

# Decrypt and decode encrypted file
function decrypt() {
  if [ -z "$1" ]; then
    printf "%s\n" "Usage: decrypt /path/to/encrypted.asc"
    return 1
  fi
  gpg --decrypt $1 | perl -MMIME::QuotedPrint -0777 -nle 'print decode_qp($_)'
}

Replace 0xC1323A377DE14C8B with your public key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.