refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / gpg-pubkey / README.md
1 ---
2 title: GnuPG Pub Key
3 homepage: https://webinstall.dev/gpg-pubkey
4 tagline: |
5   Get your GnuPG Public Key.
6 ---
7
8 ## Cheat Sheet
9
10 > Your GnuPG Public Key can be used for signing git commits and email, among
11 > other things. The file public key ends in `.asc`.
12
13 This installs two commands.
14
15 - `gpg-pubkey` will:
16   1.  Create a new gpg keypair if you don’t already have one \
17       (uses `~/.gitconfig` for name and email)
18   2.  Copy your new or existing GnuPG Public Key to your `Downloads` folder
19   3.  Print the location of the copied key, and its contents, to the screen
20 - `gpg-pubkey-id` will output the id of your public key.
21
22 The easiest way to get your GnuPG Public Key:
23
24 ```bash
25 curl https://webinstall.dev/gpg-pubkey | bash
26 ```
27
28 This is what the output of `gpg-pubkey` looks like (except much longer):
29
30 ```txt
31 GnuPG Public Key ID: CA025BC42F00BBBE
32
33 ~/Downloads/john@example.com.gpg.asc:
34
35 -----BEGIN PGP PUBLIC KEY BLOCK-----
36
37 mQINBGGLrUIBEAC+k1rHvi4xbCiN/cnh3Zi4rbKeJdPIWDP0wDhZcYzIN4/ZWVAm
38 ... (several lines omitted for brevity)
39 nZH7UhxDx6Gu4w1+uef0E/cjz2BuEn/LN9UBGWwI5dLp5p03FeXYzzAwt6sh
40 =rRiF
41 -----END PGP PUBLIC KEY BLOCK-----
42 ```
43
44 Note: Your public key is the _entire_ section starting with and including
45 `-----BEGIN` all the way to and including `BLOCK-----`
46
47 ### Files
48
49 These are the files / directories that are created and/or modified with this
50 install:
51
52 ```txt
53 ~/.config/envman/PATH.env
54 ~/.local/bin/gpg-pubkey
55 ~/.local/bin/gpg-pubkey-id
56 ~/.gnupg/
57 ~/Downloads/YOU.KEY_ID.gpg.asc
58 ```
59
60 ## How to add your GPG Public Key to GitHub
61
62 1. Go to your GitHub Profile (<https://github.com/settings/profile>)
63 2. Go to the SSH and GPG Keys (<https://github.com/settings/keys>)
64 3. Add GPG Key (<https://github.com/settings/gpg/new>)
65 4. Paste the output of `gpg-pubkey` into the form
66
67 ## How to automatically sign your git commits
68
69 Run `gpg-pubkey-id` to get your GnuPG Public Key ID and then update your
70 `~/.gitconfig` to sign with it by default:
71
72 ```bash
73 #!/bin/bash
74
75 MY_KEY_ID="$(
76   gpg-pubkey-id
77 )"
78
79 git config --global user.signingkey "${MY_KEY_ID}"
80 git config --global commit.gpgsign true
81 git config --global log.showSignature true
82 ```
83
84 Or, for Windows users:
85
86 ```bash
87 #!/usr/bin/env pwsh
88
89 $my_key_id = gpg-pubkey-id
90
91 git config --global user.signingkey "$my_key_id"
92 git config --global commit.gpgsign true
93 git config --global log.showSignature true
94 ```
95
96 ## How to use `gpg` manually
97
98 - How to get your Public Key ID
99 - How to export your Public Key
100 - How to create a Private Key
101
102 ### How to get your GnuPG Public Key ID
103
104 All _Secret Keys_ have _Public IDs_ (and corresponding _Public Keys_).
105
106 Here's a command to list your secret key(s) and get the Public ID (of the first
107 one, if you have many):
108
109 ```bash
110 #!/bin/bash
111
112 MY_KEY_ID="$(
113     gpg --list-secret-keys --keyid-format LONG |
114         grep sec |
115         cut -d'/' -f2 |
116         cut -d' ' -f1
117 )"
118 echo "$MY_KEY_ID"
119 ```
120
121 Or, for Windows users:
122
123 ```pwsh
124 #!/usr/bin/env pwsh
125
126 $my_key_id = (
127     gpg --list-secret-keys --keyid-format LONG |
128         Select-String -Pattern '\.*sec.*\/' |
129         Select-Object Line |
130         ForEach-Object {
131             $_.Line.split('/')[1].split(' ')[0]
132         }
133 )
134 echo "$my_key_id"
135 ```
136
137 Let's break that down, for good measure:
138
139 All secret keys have a Public Key and a Public ID, which can be viewed in _LONG_
140 format:
141
142 ```bash
143 gpg --list-secret-keys --keyid-format LONG
144 ```
145
146 ```txt
147 /Users/me/.gnupg/pubring.kbx
148 ----------------------------
149 sec   rsa3072/CA025BC42F00BBBE 2021-11-10 [SCEA]
150       6F848282295B19123748D36BCA025BC42F00BBBE
151 uid                 [ultimate] John Doe (mac.local) <john@example.com>
152 ssb   rsa3072/674124162BF19A32 2021-11-10 [SEA]
153 ```
154
155 The line with the Public Key ID is the one that starts with `sec`:
156
157 ```txt
158 sec   rsa3072/CA025BC42F00BBBE 2021-11-10 [SCEA]
159 ```
160
161 Specifically, it's the part just after the `/` - **CA025BC42F00BBBE**, in this
162 case.
163
164 Note: It's important that you list the Secret Keys, because listing Public Keys
165 will show all keys that you trust in your gpg keychain (co-workers, for
166 example), not just keys that you own.
167
168 ### How to export your GnuPG Public Key:
169
170 Here's how to copy your Public Key into your Downloads folder:
171
172 ```bash
173 gpg --armor --export "${MY_KEY_ID}" > ~/Downloads/"${MY_EMAIL}".gpg.asc
174 ```
175
176 Or, if you just want to print it to your console, run this:
177
178 ```bash
179 gpg --armor --export "${MY_KEY_ID}"
180 ```
181
182 ### How to create an GnuPG Private Key:
183
184 Generally speaking you'll want to use the same name and email for `git` and
185 `gpg`.
186
187 Here's how you can automate creating a key using the same info as what's in your
188 `~/.gitconfig`:
189
190 ```bash
191 #!/bin/bash
192
193 MY_NAME="$( git config --global user.name )"
194 MY_HOST="$( hostname )"
195 MY_EMAIL="$( git config --global user.email )"
196
197 gpg --batch --generate-key << EOF
198  %echo Generating RSA 3072 key
199  Key-Type: RSA
200  Key-Length: 3072
201  Subkey-Type: RSA
202  Subkey-Length: 3072
203  Name-Real: ${MY_NAME}
204  Name-Comment: ${MY_HOST}
205  Name-Email: ${MY_EMAIL}
206  Expire-Date: 0
207  %commit
208 EOF
209 ```
210
211 Or, for the Windows folk...
212
213 ```bash
214 #!/usr/bin/env pwsh
215
216 $my_name = git config --global user.name
217 $my_host = hostname
218 $my_email = git config --global user.email
219
220 echo "
221  %echo Generating RSA 3072 key
222  Key-Type: RSA
223  Key-Length: 3072
224  Subkey-Type: RSA
225  Subkey-Length: 3072
226  Name-Real: $my_name
227  Name-Comment: $my_host
228  Name-Email: $my_email
229  Expire-Date: 0
230  %commit
231 " | gpg --batch --generate-key
232 ```
233
234 Note: if you want to create a key without a passphrase, add
235 `--pinentry=loopback --passphrase=''` to the arguments.
236
237 (though typically it's better to create a random passphrase and just let macOS
238 store it in your user Keychain and forget it - just so it doesn't get backed up
239 unencrypted, etc)