Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get public key from private in gpg without using local storage (under ~/.gpg)?

Tags:

gnupg

pgp

Look to Subj: How to get public key from private in gpg without using local storage (under ~/.gpg)?

This solution does not satisfy requirements:

  $ gpg --import priv.key
  $ gpg --export $KEYID >pub.key
  $ gpg --delete-secret-and-public-key $KEYID
like image 775
gavenkoa Avatar asked Jan 19 '23 14:01

gavenkoa


1 Answers

I don't understand why you aren't happy with the solution you have already come up with, but if for some reason you really want to avoid messing with your personal keyrings, I can offer something else:

gtmp=$(mktemp -d)
gpg --homedir $gtmp --import key
gpg --homedir $gtmp --export key > pub.gpg
rm -rf $gtmp

Or as a convenient BASH function:

# Requires keyfile as 1st argument; optional 2nd argument is output file
gpg_priv_to_pub(){
  g=$(mktemp -d)
  infile=$1
  [[ $# > 1 ]] && outfile=$2 || outfile=${1%.*}_pub.gpg
  gpg --homedir $g --import "$infile" 2>/dev/null
  KEYID=$(gpg --homedir $g -k --with-colons | awk -F: '/^pub/{print $5}')
  gpg --homedir $g --export $KEYID > "$outfile"
  rm -rf $g
  echo "Public key $KEYID extracted from '$infile' and saved to '$outfile'"
  }
like image 183
rsaw Avatar answered May 11 '23 06:05

rsaw