5 function __get_git_email() {
6 git config --global user.email
7 # grep 'email\s*=.*@' ~/.gitconfig | tr -d '\t ' | head -n 1 | cut -d'=' -f2
10 function __get_pubkey_id() {
11 gpg --list-secret-keys --keyid-format LONG |
17 function _create_gpg_key() {
18 if [[ ! -e ~/.gitconfig ]]; then
22 #grep 'name\s*=' ~/.gitconfig | head -n 1 | cut -d'=' -f2 | sed -e 's/^[\t ]*//'
23 MY_NAME="$(git config --global user.name)"
24 if [[ -z ${MY_NAME} ]]; then
31 if [[ -z ${MY_EMAIL} ]]; then
38 #gpg --batch --generate-key --pinentry=loopback --passphrase=''
40 # With passphrase via macOS Keychain
42 %echo Generating RSA 3072 key...
43 %echo Warning: It may take several minutes to gather enough entropy,
44 %echo especially on a linux VPS if haveged isn't installed.
45 %echo (try moving the mouse, downloading large files, etc)
51 Name-Comment: ${MY_HOST}
52 Name-Email: ${MY_EMAIL}
56 if ! echo "$gpg_opts" | gpg --batch --generate-key 2> /dev/null; then
60 echo >&2 "== STOP! CHOOSE A PASSPHRASE =="
62 echo >&2 "Choose a passphrase for this GPG Key."
63 echo >&2 "(the passphrase will not be shown as you type)"
65 echo >&2 -n "Passphrase: "
69 %echo Generating RSA 3072 key...
70 %echo Warning: It may take several minutes to gather enough entropy,
71 %echo especially on a linux VPS if haveged isn't installed.
72 %echo (try moving the mouse, downloading large files, etc)
78 Name-Comment: ${MY_HOST}
79 Name-Email: ${MY_EMAIL}
83 " --batch --generate-key > gpg
88 # (maybe) Create first key
89 if ! gpg --list-secret-keys | grep -q sec; then
90 if ! _create_gpg_key; then
92 echo >&2 "Please set your name and email, and then try again:"
94 echo >&2 " git config --global user.name 'John Doe'"
95 echo >&2 " git config --global user.email johndoe@example.com"
96 echo >&2 " gpg-pubkey"
98 echo >&2 "(or manually create a private key first)"
112 #gpg --send-keys "${MY_KEY_ID}"
114 MY_ASC_RELPATH="Downloads/${MY_EMAIL}.${MY_KEY_ID}.gpg.asc"
115 mkdir -p ~/Downloads/
116 rm -f ~/"${MY_ASC_RELPATH}"
117 gpg --armor --export "${MY_KEY_ID}" > ~/"${MY_ASC_RELPATH}"
120 echo >&2 "GnuPG Public Key ID: ${MY_KEY_ID}"
122 #shellcheck disable=SC2088
123 echo >&2 "~/${MY_ASC_RELPATH}":
125 cat ~/"${MY_ASC_RELPATH}"