Skip to content

Instantly share code, notes, and snippets.

@awkward-ninja
Last active June 28, 2022 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awkward-ninja/ef73bc224ee596ecbd96934a34f3199c to your computer and use it in GitHub Desktop.
Save awkward-ninja/ef73bc224ee596ecbd96934a34f3199c to your computer and use it in GitHub Desktop.
GPG Import / Export / Clone
# Based on the following articles
# https://blog.programster.org/generating-gpg-master-and-subkeys
# https://risanb.com/code/backup-restore-gpg-key/
# https://gist.github.com/oseme-techguy/bae2e309c084d93b75a9b25f49718f85
gpg_keyid() {
gpg2 --list-secret-keys \
| sed -n 's/^ \(\w*\)/\1/p'
}
gpg_keygrip() {
gpg2 --with-keygrip --list-key $(gpg_keyid) \
| sed -n 's/^ Keygrip = \(\w*\)/\1/p' \
| head -n 1
}
gpg_export() {
gpg2 --export-secret-keys --armor $(gpg_keyid) > gpg-keys.txt
gpg2 --export-ownertrust > gpg-trust.txt
}
gpg_import() {
gpg2 --import gpg-keys.txt
gpg2 --import-ownertrust < gpg-trust.txt
}
gpg_clone() {
gpg_export
mkdir gpg-home
chmod 700 gpg-home
GNUPGHOME=gpg-home gpg_import
rm gpg-home/private-keys-v1.d/$(gpg_keygrip).key
}
command=$1
shift
gpg_$command $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment