feature: add gpg-pubkey
[webi-installers/.git] / gpg-pubkey / gpg-pubkey.sh
1 #!/bin/bash
2 set -e
3 set -u
4
5 function __get_git_email() {
6     grep 'email\s*=.*@' ~/.gitconfig |
7         tr -d '\t ' | head -n 1 |
8         cut -d'=' -f2
9 }
10
11 function __get_pubkey_id() {
12     gpg --list-secret-keys --keyid-format LONG |
13         grep sec |
14         cut -d'/' -f2 |
15         cut -d' ' -f1
16 }
17
18 function _create_gpg_key() {
19     if [[ ! -e ~/.gitconfig ]]; then
20         return 1
21     fi
22
23     MY_NAME="$(
24         grep 'name\s*=' ~/.gitconfig |
25             head -n 1 |
26             cut -d'=' -f2 |
27             sed -e 's/^[\t ]*//'
28     )"
29     if [[ -z ${MY_NAME} ]]; then
30         return 1
31     fi
32
33     MY_EMAIL="$(
34         __get_git_email
35     )"
36     if [[ -z ${MY_EMAIL} ]]; then
37         return 1
38     fi
39
40     MY_HOST="$(hostname)"
41
42     # Without passphrase:
43     #gpg --batch --generate-key --pinentry=loopback --passphrase=''
44
45     # With passphrase via macOS Keychain
46     gpg_opts="
47      %echo Generating RSA 3072 key...
48      %echo Warning: It may take several minutes to gather enough entropy,
49      %echo          especially on a linux VPS if haveged isn't installed.
50      %echo          (try moving the mouse, downloading large files, etc)
51      Key-Type: RSA
52      Key-Length: 3072
53      Subkey-Type: RSA
54      Subkey-Length: 3072
55      Name-Real: ${MY_NAME}
56      Name-Comment: ${MY_HOST}
57      Name-Email: ${MY_EMAIL}
58      Expire-Date: 0
59      %commit
60     "
61     if ! echo "$gpg_opts" | gpg --batch --generate-key 2> /dev/null; then
62         echo >&2 ""
63         echo >&2 ""
64         echo >&2 ""
65         echo >&2 "== STOP! CHOOSE A PASSPHRASE =="
66         echo >&2 ""
67         echo >&2 "Choose a passphrase for this GPG Key."
68         echo >&2 "(the passphrase will not be shown as you type)"
69         echo >&2 ""
70         echo >&2 -n "Passphrase: "
71         read -r -s
72         echo >&2 ""
73         echo "
74          %echo Generating RSA 3072 key...
75          %echo Warning: It may take several minutes to gather enough entropy,
76          %echo          especially on a linux VPS if haveged isn't installed.
77          %echo          (try moving the mouse, downloading large files, etc)
78          Key-Type: RSA
79          Key-Length: 3072
80          Subkey-Type: RSA
81          Subkey-Length: 3072
82          Name-Real: ${MY_NAME}
83          Name-Comment: ${MY_HOST}
84          Name-Email: ${MY_EMAIL}
85          Passphrase: ${REPLY}
86          Expire-Date: 0
87          %commit
88         " --batch --generate-key > gpg
89     fi
90     echo >&2 "Done"
91 }
92
93 # (maybe) Create first key
94 if ! gpg --list-secret-keys | grep -q sec; then
95     if ! _create_gpg_key; then
96         echo >&2 ""
97         echo >&2 "Please set your name and email, and then try again:"
98         echo >&2 ""
99         echo >&2 "    git config --global user.name 'John Doe'"
100         echo >&2 "    git config --global user.email johndoe@example.com"
101         echo >&2 "    gpg-pubkey"
102         echo >&2 ""
103         echo >&2 "(or manually create a private key first)"
104         echo >&2 ""
105         exit 1
106     fi
107 fi
108
109 MY_KEY_ID="$(
110     __get_pubkey_id
111 )"
112
113 MY_EMAIL="$(
114     __get_git_email
115 )"
116
117 #gpg --send-keys "${MY_KEY_ID}"
118
119 MY_ASC_RELPATH="Downloads/${MY_EMAIL}.${MY_KEY_ID}.gpg.asc"
120 mkdir -p ~/Downloads/
121 rm -f ~/"${MY_ASC_RELPATH}"
122 gpg --armor --export "${MY_KEY_ID}" > ~/"${MY_ASC_RELPATH}"
123
124 echo >&2 ""
125 echo >&2 "GnuPG Public Key ID: ${MY_KEY_ID}"
126 echo >&2 ""
127 #shellcheck disable=SC2088
128 echo >&2 "~/${MY_ASC_RELPATH}":
129 echo >&2 ""
130 cat ~/"${MY_ASC_RELPATH}"
131 echo >&2 ""