Git 支持使用SSH签名,也支持GPG签名提交记录

SSH签名

SSH 签名的工具是 ssh-keygen

使用 ssh-keygen 生成 SSH Key

1
ssh-keygen -t ed25519 -C "Gitee SSH Key"
  • -t key 类型
  • -C 注释

执行后,在~/.ssh目录下生成SSH私钥id_ed25519和公钥id_ed25519.pub

签名与验证

通过 ssh-keygen 工具,-f参数指定私钥,给指定文件签名

1
ssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n file /tmp/a.txt
  • -Y sign表示计算签名
  • -f指定私钥
  • -n file是给签名指定类型
  • file是我们自己定的,不同类型的签名不会产生冲突

签名后会生成一个签名文件/tmp/a.txt.sig

配置公钥列表,或者将公钥上传到git服务器。

1
www@gmail.com ssh-ed25519 AAAAxxx 
  • 第一列是公钥的标识(邮箱)。
  • 第二列是公钥类型,
  • 第三列是公钥内容,也就是~/.ssh/id_ed25519.pub的内容

验证签名

1
ssh-keygen -Y verify -f allowed_signers -I www@gmail.com -n file -s /tmp/a.txt.sig < /tmp/a.txt
  • -Y verify表示要验证签名
  • -f 用来指定公钥列表文件
  • -I 指定使用公钥标识
  • -n file 需要跟签名的时候保持一致
  • -s 指定签名所在文件

Git命令添加SSH签名

1
2
3
4
5
6
7
8
9
# 使用 SSH 签名
git config gpg.format ssh
# 指定 SSH 私钥文件
git config user.signingKey ~/.ssh/id_ed25519.pub
# 指定可信公钥列表文件
git config gpg.ssh.allowedSignersFile "$HOME/.config/git/allowed_signers"
# 开启自动签名(可选)
git config commit.gpgsign true
git config tag.gpgsign true

GPG签名

GPG安装

安装完成后,在命令行生成密钥对

 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
> gpg --gen-key
请选择您要使用的密钥种类:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (仅用于签名)
   (4) RSA (仅用于签名)
您的选择?1                                                   <- 选择密钥类型
RSA 密钥长度应在 1024 位与 4096 位之间。
您想要用多大的密钥尺寸?(3072) 3072
您所要求的密钥尺寸是 3072 位
请设定这把密钥的有效期限。
         0 = 密钥永不过期
      <n>  = 密钥在 n 天后过期
      <n>w = 密钥在 n 周后过期
      <n>m = 密钥在 n 月后过期
      <n>y = 密钥在 n 年后过期
密钥的有效期限是?(0) 1y                                       <- 有效期
密钥于 2020 年 05 月 04 日 星期一 14 时 38 分 48 秒 CST 过期
以上正确吗?(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>"

真实姓名:YOUR_NAME                                          <- 用户名
电子邮件地址:gitee@gitee.com                                 <- 邮箱,需要与 Gitee 提交邮箱保持一致
注释:Gitee GPG Key                                          <- 注释
您选定了这个用户标识:
    “YOUR_NAME (Gitee GPG Key) <gitee@gitee.com>”

更改姓名 (N)、注释 (C)、电子邮件地址 (E) 或确定 (O)/退出 (Q)?O
gpg: 密钥 B0A02972E266DD6D 被标记为绝对信任
gpg: revocation certificate stored as 'xxx'
公钥和私钥已经生成并经签名。

pub   rsa3072 2019-05-05 [SC] [有效至:2020-05-04]
      8086B4D21B3118A83CC16CEBB0A02972E266DD6D                 <- Key ID
uid                      likui (Gitee GPG Key) <gitee@gitee.com>
sub   rsa3072 2019-05-05 [E] [有效至:2020-05-04]

执行命令后,提示输入选择要生成的密钥类型,

选择类型后,比如默认RSA,会提示选择RSA Keys大小,默认2048。

然后是密钥过期时间,再选择用户名和邮箱。

导出 GPG 公钥

1
gpg --armor --export 8086B4D21B3118A83CC16CEBB0A02972E266DD6D

查看本地私钥信息

1
gpg --list-secret-keys

配置Git

1
git config --global user.signingkey 8086B4D21B3118A83CC16CEBB0A02972E266DD6D

将公钥添加到git服务器,或者使用命令发送到指定服务器

1
gpg --keyserver hkp://pgp.mit.edu --send-keys 8086B4D2

如果没有配置gpg,查看到的记录会显示未签名

1
2
3
> git log --show-signature -n 1
fatal: cannot run gpg: No such file or directory
No signature

使用参数-S参数签名进行提交,会提示输入密码

1
git commit -S -m "xxx" 
1
2
3
4
5
6
7
git log --show-signature -n 1
commit f042dc0da63e192c3ffd89b6c5cdb560d3706ab3 (HEAD -> master)
gpg: Signature made 2023年06月27日 15:53:50
gpg:                using RSA key 533F5540BE351AADFEF3C588AFBF216B5F802252
gpg: Good signature from "piaohua <wangpiaohua@163.com>" [ultimate]
Author: piao <wangpiaohua@163.com>
Date:   Tue Jun 27 15:53:44 2023 +0800

参考