nix/bin/gpg-restore-key
2025-12-03 20:09:09 -06:00

58 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
# Restore GPG key from backup file
# Usage: gpg-restore-key <private-key-file> [public-key-file]
set -e
if [[ $# -lt 1 ]]; then
echo "Usage: gpg-restore-key <private-key-file> [public-key-file]"
echo ""
echo "Examples:"
echo " gpg-restore-key ~/private-key-backup.asc"
echo " gpg-restore-key ~/private-key.asc ~/public-key.asc"
exit 1
fi
PRIVATE_KEY="$1"
PUBLIC_KEY="${2:-}"
if [[ ! -f $PRIVATE_KEY ]]; then
echo "Error: File not found: $PRIVATE_KEY"
exit 1
fi
echo "Importing private key from $PRIVATE_KEY..."
gpg --import "$PRIVATE_KEY"
if [[ -n $PUBLIC_KEY && -f $PUBLIC_KEY ]]; then
echo ""
echo "Importing public key from $PUBLIC_KEY..."
gpg --import "$PUBLIC_KEY"
fi
# Get the key ID that was just imported
KEY_ID=$(gpg --list-secret-keys --keyid-format LONG 2>/dev/null | grep '^sec' | head -1 | sed 's/.*\/\([A-F0-9]*\) .*/\1/')
if [[ -z $KEY_ID ]]; then
echo "Error: Could not find imported key"
exit 1
fi
echo ""
echo "Key imported successfully!"
echo ""
gpg --list-keys --keyid-format LONG "$KEY_ID"
echo ""
read -p "Do you want to trust this key ultimately? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Setting ultimate trust..."
echo -e "5\ny\n" | gpg --command-fd 0 --edit-key "$KEY_ID" trust 2>/dev/null
echo "Done!"
fi
echo ""
echo "Key ID: $KEY_ID"
echo "Update your neomutt config with:"
echo " set pgp_sign_as = 0x$KEY_ID"