git commit 签名


877
3 mins.

1.1 创建自己的 GPG 密钥

执行一下命令选择加密算法,并完善相关信息。

步骤简介

  1. 选择加密算法 (RSA 签名)
  2. 设置密钥长度 (4096)
  3. 设置密钥有效期 (1y),这是确保安全的最后防线
  4. 设置用户姓名、邮箱、注释(最好不要用常用邮箱,避免垃圾邮件骚扰)

1.1.1 运行 gpg 开始生成密钥

1
gpg --full-generate-key

输出如下,这里选择 4

1
2
3
4
5
6
7
8
9
10
gpg (GnuPG) 2.2.27; Copyright (C) 2021 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

这里我们生成的是主密钥,只负责分发子密钥,因此只需要具有签名功能即可
这里我选择 4. RSA,有如下几个原因

  • RSA 久经验证,是行业标准。
  • 相关研究认为更长的 RSA,参考此论文: A RIDDLE WRAPPED IN AN ENIGMA
  • RSA 在各个平台上都有很好的支持。

1.1.2 设置密钥长度

现在计算机性能都不差,可以选择 4096 位的密钥长度,这样可以更好的保护你的密钥。

1
2
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072)

1.1.3 设置过期时常

这里非常推荐设置一个过期时长,避免密钥被盗用,一般设置为 1 年。

这里我们输入 1y,表示 1 年后过期。
如果有更高的安全需求,可以设置更短的过期时间。
即将过期的时候,可以通过 gpg --edit-key <key-id> 命令来延长密钥的有效期。

1
2
3
4
5
6
7
8
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)

1.1.4 接下来输入身份信息

从略

1.1.5 为主密钥设定密码

这一步应该设定一个强度高的密码。

1
2
3
4
5
6
7
8
lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x Please enter the passphrase to x
x protect your new key x
x x
x Passphrase: ________________________________________ x
x x
x <OK> <Cancel> x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj

1.1.6 完成创建

主密钥成就解锁

恭喜你,走到这一步已经获得了一个主密钥。

1
2
3
4
生成一个用于此用途的子密钥。
pub rsa4096 2023-08-07 [SC] [expires:2024-08-06]
AC27EFC2205EE9272D6B87F8DD6E204C56810DCB
uid tqcq <[email protected]>

1.2 签发子密钥

1.2.1 首先进入 gpg 的交互模式

1
gpg --edit-key tqcq

1.2.2 添加子密钥

1
2
# 这里的 gpg> 是 gpg 的交互模式提示符,不需要输入
gpg> addkey

1.2.3 选择子密钥类型

这里选择 10 即可,ECC 兼顾速度和安全性

1
2
3
4
5
6
7
8
9
10
11
12
13
Please select what kind of key you want:
(3) DSA (sign only)
(4) RSA (sign only)
(5) Elgamal (encrypt only)
(6) RSA (encrypt only)
(7) DSA (set your own capabilities)
(8) RSA (set your own capabilities)
(10) ECC (sign only)
(11) ECC (set your own capabilities)
(12) ECC (encrypt only)
(13) Existing key
Your selection? 10

1.2.4 选择子密钥算法

注意

NIST、Brainpool、secp256k1 具有一些安全隐患,不推荐使用。

1
2
3
4
5
6
7
8
9
10
11
Please select which elliptic curve you want:
(1) Curve 25519 *default*
(2) Curve 448
(3) NIST P-256
(4) NIST P-384
(5) NIST P-521
(6) Brainpool P-256
(7) Brainpool P-384
(8) Brainpool P-512
(9) secp256k1
Your selection? 1

设定过期时间即可生成子密钥

1.3 设置 git 签名

1.3.1 本地 git 签名设置

这里我们用子密钥来签名

1
2
3
4
5
6
7
## 查看子密钥的 keyid
gpg --list-keys --keyid-format=long

## git 设置签名密钥
git config --global user.signingkey <sub-key-id>
## 设置每次commit自动签名
git config --global commit.gpgsign true

1.3.2 Github 公钥设置

首先导出你的公钥,用于验证签名

这里 --armor 导出公钥为 ASCII 格式

1
gpg --armor --export <sub-key-id>

添加完成后,你的提交就会被标记为 Verified

注意

如果签名失败,请在环境变量中指定 GPG_TTY, 具体原因参考 [2]

export GPG_TTY=$(tty)

1.4 常用 GPG 命令

列出所有密钥: gpg --list-keys

1.5 参考资料

您正在使用不支持或禁用了 JavaScript 的浏览器访问我们的网站。

请考虑启用 JavaScript 以获得更好的浏览体验。

要访问真正的网站 "uocat.com",请手动复制以下网址并在您喜欢的浏览器中打开:

https://uocat.com