Created
August 12, 2024 15:55
-
-
Save dschreck/38e12e484dd435803a5588577dddbf82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Set variables for your name and email | |
FullName="" | |
EmailAddress="" | |
# Parse command line options | |
while getopts ":n:e:c:" opt; do | |
case $opt in | |
n) | |
FullName=$OPTARG | |
;; | |
e) | |
EmailAddress=$OPTARG | |
;; | |
c) | |
KeyComment=$OPTARG | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
;; | |
esac | |
done | |
# Check if variables are still empty and show help message | |
if [[ -z $FullName || -z $EmailAddress ]]; then | |
echo "Usage: gen_gpg_key -n <name> -e <email> -c [comment]" | |
echo "Comment is optional." | |
exit 1 | |
fi | |
if [[ -z $KeyComment ]]; then | |
KeyComment="Code Signing Key for $FullName <$EmailAddress>" | |
fi | |
# Generate the GPG key with ECC and Curve 25519 | |
gpg --batch --generate-key <<EOF | |
Key-Type: RSA | |
Key-Length: 4096 | |
Subkey-Type: RSA | |
Subkey-Length: 2048 | |
Name-Real: $FullName | |
Name-Email: $EmailAddress | |
Expire-Date: 0 | |
%no-protection | |
%commit | |
EOF | |
# List the generated keys | |
GPG_KEY_DATA=$(gpg --list-secret-keys --keyid-format LONG "$EmailAddress") | |
FOUND_KEYS=$(echo "$GPG_KEY_DATA" | grep -E 'sec rsa4096/[^ ]+' | awk -F 'rsa4096/' '{ print $2; }' | awk '{ print $1; }' ) | |
TOTAL_KEYS_FOUND=$(echo "$FOUND_KEYS" | wc -l) | |
if [[ $TOTAL_KEYS_FOUND -gt 1 ]]; then | |
echo "Found more than one key. Please delete the keys and try again." | |
echo "$GPG_KEY_DATA" | |
exit 1 | |
fi | |
GPG_KEY_ID=$(echo "$FOUND_KEYS" | awk '{ print $1; }') | |
echo "Your GPG key ID is: $GPG_KEY_ID" | |
echo | |
# Export the GPG public key | |
gpg --armor --export "$GPG_KEY_ID" | |
echo | |
echo "Copy the above GPG public key and add it to your GitHub account." | |
# Configuring Git to use the GPG key | |
git config --global user.signingkey "$GPG_KEY_ID" | |
git config --global commit.gpgsign true | |
echo "Git is now configured to sign commits using this GPG key." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment