使用 GPG 加密 Github Commits

玩技站长 经验总结91,561字数 3301阅读模式

使用 GPG 加密 Github Commits-图片1

GnuPG(简称 GPG),它是目前最流行、最好用的开源加密工具之一。
GPG 有许多用途,比如对文件,邮件的加密。而本文要说的是,如何使用 GPG 来加密 Github Commits。
在 Github 上查看一些项目的 Commits 时,偶尔会发现「This commit was signed with a verified signature.」字样,具体来说,就是下图示例那样。文章源自玩技e族-https://www.playezu.com/16102.html

文章源自玩技e族-https://www.playezu.com/16102.html

一、安装 Git 和 TortoiseGit

关于如何在 Windows 下安装 Git 和 TortoiseGit,请参考官方网站:
https://git-scm.com
https://tortoisegit.org文章源自玩技e族-https://www.playezu.com/16102.html

二、生成密钥

在安装完 Git 之后,开始菜单项里会有个 Git Bash,打开,如下图所示:
使用 GPG 加密 Github Commits-图片3文章源自玩技e族-https://www.playezu.com/16102.html

Git 自带了 gpg 命令,因此可以直接在 Git Bash 里生成密钥。命令如下:文章源自玩技e族-https://www.playezu.com/16102.html

gpg --gen-key

回车,提示信息如下:文章源自玩技e族-https://www.playezu.com/16102.html

gpg (GnuPG) 1.4.21; 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.

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?

选择加密算法,默认选择第一个选项即可,表示加密和签名都使用 RSA 算法。
选 1,回车。文章源自玩技e族-https://www.playezu.com/16102.html

选择密钥长度,默认为 2048,建议输入 4096。文章源自玩技e族-https://www.playezu.com/16102.html

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

输入 4096,回车。文章源自玩技e族-https://www.playezu.com/16102.html

设定密钥的有效期。文章源自玩技e族-https://www.playezu.com/16102.html

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)

密钥只是个人使用的话,建议选择第一个选项,即永不过期。
输入 0,回车。

系统会问你上述设置是否正确。

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>"

Real name:

「Real name」填入你的名字,需是英文。回车。

Email address:

「Email address」填入你的邮箱地址。回车。

Comment:

「Comment」可以空着不填。回车。
系统会再次让你确认填入的信息。

You selected this USER-ID:
    "Teddysun <i@teddysun.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?

输入 O,回车。
系统会让你设定一个私钥的密码。

You need a Passphrase to protect your secret key.
Enter passphrase:
Repeat passphrase:

注意这里要留空不填,直接回车即可。这是因为 TortoiseGit 不支持 1.4.x 含有私钥密码。
系统这时开始生成密钥,这时会要求你做一些随机的举动,以生成一个随机数。你拿起鼠标随便晃晃,直到完成密钥生成。

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 0AD6C46D marked as ultimately trusted
public and secret key created and signed.

三、列出密钥

命令如下:

gpg --list-keys

输出结果如下:

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
/c/Users/teddysun/.gnupg/pubring.gpg
------------------------------------
pub   4096R/0AD6C46D 2017-02-22
uid                  Teddysun <i@teddysun.com>
sub   4096R/5BB7BB4E 2017-02-22

第一行显示公钥文件名(pubring.gpg)
第二行显示公钥特征(4096 位,Hash 字符串和生成时间)
第三行显示用户信息
第四行显示私钥特征

四、输出密钥

公钥文件(.gnupg/pubring.gpg)以二进制形式储存,armor 参数可以将其转换为 ASCII 码显示。

gpg --armor --output public-key.txt --export [密钥ID]

[密钥ID]指定用户的公钥,如 0AD6C46D,output 参数指定输出文件名,如 public-key.txt

同时,export-secret-keys 参数可以转换私钥。

gpg --armor --output private-key.txt --export-secret-keys

public-key.txt 和 private-key.txt 默认会导出至用户目录 C:\Users\<用户名>\ 下。

五、上传公钥至 Github 帐号

点击用户头像,打开 Settings,左侧菜单点击 SSH and GPG keys,在 GPG keys 那里,点击 New GPG key。
在输入框里填入刚刚导出的 public-key.txt 内容。
点击 Add GPG key,完成上传。

六、设置 Git

回到 Git Bash 窗口,根据刚才 gpg –list-keys 显示的结果,此时已经知道密钥 ID 为 0AD6C46D。
设置 Git 使用该密钥 ID 加密:

git config --global user.signingkey 0AD6C46D

设置 Git 全局使用该密钥加密:

git config --global commit.gpgsign true

最后,再输入以下命令查看 Git 配置情况:

git config -l

包含以下信息。

user.signingkey=0AD6C46D
commit.gpgsign=true

至此,使用 GPG 加密 Github Commits 就正式完成了。
以后再 Git Commit,同步到 Github 上之后,就会发现该 Commit 已显示 Verified。

参考链接:

https://help.github.com/articles/generating-a-new-gpg-key/
https://help.github.com/articles/adding-a-new-gpg-key-to-your-github-account/
https://help.github.com/articles/signing-commits-using-gpg/
http://www.ruanyifeng.com/blog/2013/07/gpg.html

 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/16102.html
    转载说明: 点我前往阅读>>>
评论  9  访客  9
    • 阿喵
      阿喵 1

      阿喵博客 www.catbiu.com 与改为 爱皮哇 ipwa.cn 友链请更改谢谢 :grin:

      • 沙扬娜拉
        沙扬娜拉 0

        小白看不懂哦

        • 楚狂人博客
          楚狂人博客 0

          很有用的分享,学习了,谢谢

          • 米扑博客
            米扑博客 1

            非常赞的分享,正在研究学习 谢谢。

            • 久伴博客
              久伴博客 2

              为啥你的代码不高亮,真丑 :mrgreen:

            匿名

            发表评论

            匿名网友
            确定