用GPG为commit签名

预计阅读时间: 2 分钟

GPG简介

gpg是什么呢
gpg又称GnuPG,是gnuproject用于支持gpg加密和签名的软件

GPG能干什么呢

当然可以用于签名commit啦

安装gpg

通常Ubuntu/Debian只需apt install gnupg即可
Centos 改为yum install gnupg 即可(未测试)
其他平台自己上网Google

生成密钥

本文所有命令基于Ubuntu 16.04 LTS

gpg --gen-key

然后


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)

选择合适的RSA密钥长度
512位的RSA密钥很不安全
1024位的密钥可以被政府支持的攻击者破解(如CIA)
一般2048位已经可以保证安全了
不过像博主这样的人当然选4096啦!

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)

如果个人使用并可以保证私钥不泄漏的话可以永不过期

接下来就是名字、email等信息啦
注意email要与git设置的email相同

可以用gpg --list-key查看生成的keys

--------------------------------------------        
pub   4096R/FC9FD07D 2017-10-05 [expires: 2033-10-01]       
uid    Sugachi Duan <sugachiduan@gmail.com>   
sub   4096R/932F4196 2017-10-05 [expires: 2033-10-01]

然后记住gpg的userID,一会要用
比如这里的是932F4196

设置Git

设置几个变量,使得git自动在commit中使用gpg签名

#configure global gpg sing for commit
git config --global commit.gpgsign true

#configure global gpg userid (key used to sign)
git config --global user.signingkey YOURUSERID

把gpg公钥在github中添加
正常的提交并push到github就可以看到绿色的verified字样啦

PSX_20180107_163013.jpg
Easton Man
Easton Man
文章: 37

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注