Github Gitee 使用 GPG Key 签名

Github 的 commit 的绿色 Verified 挺好看的,整一个。使用 GPG Key 验证 commit 即可在 Commit 中显示绿色 Verified,文中以 Github 为例,Gitee 平台的操作也是类似的。

环境

git version 2.35.1.windows.2

gpg (GnuPG) 2.2.29-unknown

windows 的 git 自带了 gpg, 无需单独下载安装。其他平台可能需要手动下载安装。

生成 GPG Key

  1. 输入指令开始生成 gpg key
1
gpg --full-generate-key
  1. 选择 key 类型,根据 Github 文档推荐选择默认,直接 enter
1
2
3
4
5
6
7
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 长度,根据 Github 文档推荐选择 4096
1
2
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072) 4096
  1. 选择有效期,我这里选择一年
1
2
3
4
5
6
7
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) 1y
  1. 输入 id 和邮箱,注意必须使用 github 绑定的邮箱,也可以使用 github 的noreply地址,否则提交 commit 是会验证失败
1
2
3
4
GnuPG needs to construct a user ID to identify your key.
Real name: lixx.org
Email address: [email protected]
Comment:
  1. 信息无误则输入 o 确认
1
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
  1. 输入安全密码

    建议设置密码,以保护 GPG Key

  2. gpg key 生成结果

    记录 16 位的 key id 71C5EA31976DCC31

1
2
3
4
5
6
7
8
9
10
11
12
13
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 '71C5EA31976DCC31' marked as ultimately trusted
gpg: revocation certificate stored as '/c/Users/lixx/.gnupg/openpgp-revocs.d/681E61312333B6D9CC0A219771C5EA31976DCC31.rev'
public and secret key created and signed.

pub rsa4096 2022-02-10 [SC] [expires: 2023-02-10]
681E61312333B6D9CC0A219771C5EA31976DCC31
uid lixx.org <[email protected]>
sub rsa4096 2022-02-10 [E] [expires: 2023-02-10]

输出 GPG 公钥

使用 gpg --armor --export key-id 输出公钥,我的 key-id 为71C5EA31976DCC31

1
gpg --armor --export 71C5EA31976DCC31

然后得到公钥

1
2
3
4
5
-----BEGIN PGP PUBLIC KEY BLOCK-----
.
.
.
-----END PGP PUBLIC KEY BLOCK-----

添加公钥到 Github

将上面得到的公钥添加到 Github, Gitee 同理。

设置 Git

  • 在 git 中设置要使用哪一个 GPG Key 的 id,我这里是71C5EA31976DCC31
1
git config --global user.signingkey 71C5EA31976DCC31
  • 设置 Git 用户名和邮箱,必须和 GPG Key 填写的信息以及 Github 绑定的邮箱相同,三者一致才能通过验证
1
2
git config --global user.name lixx.org
git config --global user.email [email protected]
  • 设置 Git 默认使用 GPG Key(可选)
1
git config --global commit.gpgsign true

提交 Commit

  • 若未开启 Git 默认使用 GPG Key, 需要使用-S参数
1
git commit -S -m "lixx.org GPG test"
  • 若已开启 Git 默认使用 GPG Key,直接 commit 即可
1
git commit -m "lixx.org GPG test"

然后需要输入生成 GPG Key 时设置的安全密码

git push后,即可在网页 commit history 看到绿色Verified

额外

  • 查看已存在的私钥 gpg --list-secret-keys --keyid-format=long

  • 查看已存在的公钥 gpg --list-keys --keyid-format=long

  • 删除私钥 gpg --delete-secret-keys id

  • 删除公钥 gpg --delete-keys id

  • 输出公钥 gpg --armor --export id

  • 文中提到的 key id 既可以使用 16 位的短 id,也可以使用 40 位的长 id

  • 建议设置密码和有效期,降低 GPG key 丢失的危害