gpg signing github commits

With git it is easy to overwrite someone as author or commit as someone else. Then how do you know it was someone who committed the changes? That’s where the gpg signing comes into picture. See for ssh signing commits

this is my first gpg signed post


One thing to note is that once you have generated gpg keys from git bash then you can’t use them from windows command prompt. I found it rather handy to install gnugpg and then generating keys with gpg. One thing i had to setup explicitly is to tell git where to find gpg so it asks me passcode . Anyway the command is:

1
git config --global gpg.program "C:\GnuPG\bin\gpg.exe"


A few links of interest around this topic:

  1. Why-does-GitHub-let-me-commit-as-other-people
  2. GPG-signing-for-git-commit
  3. A git horror story: Repository Integrity With Signed Commits


Anyway the most interesting is this:

/images/internet-full-of-chimps.png


Anyway, here is how to generate a gpg signature on git bash

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$ gpg --gen-key
gpg (GnuPG) 1.4.22; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory `/c/Users/sarang/.gnupg' created
gpg: new configuration file `/c/Users/sarang/.gnupg/gpg.conf' created
gpg: WARNING: options in `/c/Users/sarang/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/c/Users/sarang/.gnupg/secring.gpg' created
gpg: keyring `/c/Users/sarang/.gnupg/pubring.gpg' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"


Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.+++++
..............+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
+++++
...+++++
gpg: /c/Users/sarang/.gnupg/trustdb.gpg: trustdb created
gpg: key 67756F47 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   4096R/67756F47 2018-02-16
      Key fingerprint = 0300 88C3 84F8 D046 3214  A715 2B2A 845D 6775 6F47
uid                  Sarang <*@*.*>
sub   4096R/05A5640E 2018-02-16


setting up gpg on linux is comparatively easier, first generate the gpg-key:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
gpg --full-generate-key 
gpg (GnuPG) 2.2.19; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
  (14) Existing key from card
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 1m
Key expires at Wed 30 Dec 2020 04:19:02 PM PST
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: Sarang Baheti
Email address: ***@**.*
Comment: github email
You selected this USER-ID:
    "Sarang Baheti (github email) <***@**.*>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key **************** marked as ultimately trusted
gpg: revocation certificate stored as '/home/sarang/.gnupg/openpgp-revocs.d/***************************************.rev'
public and secret key created and signed.

pub   rsa4096 2020-07-04 [SC] [expires: 2020-07-31]
      ***************************************
uid                      Sarang Baheti (github email) <***@**.*>
sub   rsa4096 2020-07-04 [E] [expires: 2020-07-31]


once you have key generated, export public key, gpg --armor --export <KEY> > gpg.key.txt does the job. Paste the key as it is in github gpg key.

1
gpg --armor --export *************************************** > gpg.key.txt


then just setup the git config to sign on each commit as below:

1
2
git config --global commit.gpgSign true
git config --global user.signingKey ***************************************


Note: if you have chosen to hide email from commits in github, they you would have a cryptic email <something>@users.noreply.github.com, that is the email you need to generate key with.

In case you run into issues with not able to sign thru gpg, this stackoverflow answer is quite helpful.

 
git