GPG全名GnuPG,是一个加密软件,用于加密、签名通信内容及管理非对称密码学的密钥。它有很多用途,今天要说的是使用GPG为git提交进行签名,防止他人冒名提交。
目前Github和Gitlab都支持GPG,下面是我Github添加的签名。让我们开始操练起来吧!
安装GPG
官网下载安装比较繁琐,建议使用brew进行安装(MacOS系统)。
1 | $ brew install gpg |
生成密钥
- 生成GPG密钥对
选择RSA 输入Enter,下一步输入密钥长度,这里只是4096。接着选择密钥过期时间,最后输入你的电子邮件。
1 | $ gpg --full-generate-key |
输入密码后,最终一个关于luoji@example.com的密钥对
1 | We need to generate a lot of random bytes. It is a good idea to perform |
- 查看所有密钥对, GPG密钥ID是:
A1A16F22BC02D78C
1
2
3
4
5$ gpg --list-secret-keys --keyid-format LONG
sec rsa4096/A1A16F22BC02D78C 2021-01-06 [SC]
BCC2217487CD7D65C124967BA1A16F22BC02D78C
uid [ultimate] luoji <luoji@example.com>
ssb rsa4096/D5473502EE9CEE25 2021-01-06 [E] - 使用GPG密钥ID是
A1A16F22BC02D78C
导出密钥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$ gpg --armor --export A1A16F22BC02D78C
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBF/11pABEAClSHDndvl8yUVzhVDigc1lgyeiW2N4ayqM//QayC+a6V2dAHJN
u+HOsxzc/LyczWRTFe5ovGGNiDZ4/zClZj1YqRGfZb9HSswmAuOTL/6tkgfyuuP+
ICn9LNuSUK3ZxaEEPfh8hIcqfh3ffBy2nA5kC/Xj/70OS2GeHIJv6xprfAuo+EPd
dLxD84OYh1Vc9mIO2cSnsnHk/JndZD5EEdJ8FYSShJ3DndAb1W76cHAlIDkrmGPn
/qLVBnkq7HvHnxfl2EoQOycCqMnFWyEK+tS7Z5RxxOs1wfDQTyxd1UcqOTRwdNfC
RvUwBcRNZxRXPKyLLNbYv+03Qn11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GVWniSxyaYQjAKdEnlkE3U5k
tbGDfFxtmOv0OLdSvEj2NDUI8VNO5CT00c0ZhsSuhMUmacxpieg6TknT5IWkR9f7
vO+0+BmZ/rqMqJpeIoxuc2jaHDAtSLBUL+jHYNbnye7W3dTpp8TVOuv504mBcm1/
N90px6aKr2lzm1iPVa+IrvutKMFgku19hh45492+zF8EEzfIaNU99AYDkd9okVdQ
ud+5R2xu28BL8XjCItYVqrIb3maC/r2VBmAyFuSwjifoU34/hzQI+3tt808gh526
oiR9P5BtA+9ELQYUPQsHA/Vuet9CYCb6zPMGsdNTJZeyB4YISMmK8+gzOn72uqlH
0d9zMZwbrNEaxG6jqsxrYFILLM+zWQJsRiBgPvI0+l38D+ZtFQ8s1Fqj4rtcWVBw
z3xercisx8hDDK0W7yDWGAVn0uNHGHarwvGXRDyeorRTEgcUu+W0BqDmJpYI/bdx
jzaR61fn9g5aZGVUs2HfQPOi84J42ShfwLB4ia7yaK2MF4DqEQ==
=JYRy
-----END PGP PUBLIC KEY BLOCK-----添加GPG到Github
复制上面的密钥,包括-----BEGIN PGP PUBLIC KEY BLOCK-----
和-----END PGP PUBLIC KEY BLOCK-----
配置密钥到Git
1 | git config --global user.signingkey A1A16F22BC02D78C |
我使用的zsh,命令为echo 'export GPG_TTY=$(tty)' >> ~/.zshrc
签名提交
1 | $ git commit -S -m "your commit message" |
删除GPG
最后删除用于测试的GPG
1 | $ gpg --delete-secret-keys A1A16F22BC02D78C |
1 | $ gpg --list-keys |