配置 GPG

安装

我这里使用 scoop 来管理常用的软件。

1
scoop install gpg

查看安装的版本

1
gpg --version

结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
gpg (GnuPG) 2.4.3
libgcrypt 1.10.2
Copyright (C) 2023 g10 Code GmbH
License GNU GPL-3.0-or-later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: D:\Development\Scoop\apps\gpg\current\home
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

生成新的 GPG 密钥

  1. 输入如下命令:

    1
    
    gpg --full-generate-key
    
  2. 上面的命令执行之后,会要求选用对应的加密算法。我们在这里直接回车选择默认的 9 使用 ECC 椭圆曲线加密算法用于加密和解密。提示内容如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    gpg (GnuPG) 2.4.3; Copyright (C) 2023 g10 Code GmbH
    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
       (2) DSA and Elgamal
       (3) DSA (sign only)
       (4) RSA (sign only)
       (9) ECC (sign and encrypt) *default*
      (10) ECC (sign only)
      (14) Existing key from card
    Your selection?
    
  3. 选择完成之后,会提示选用对应的椭圆曲线。我们选择默认的 Curve 25519 椭圆曲线。提示内容如下:

    1
    2
    3
    4
    
    Please select which elliptic curve you want:
    (1) Curve 25519 *default*
    (4) NIST P-384
    (6) Brainpool P-256
    
  4. 设置过期时间,根据需要设置,我这里选的是 0,设置不过期。

    1
    2
    3
    4
    5
    6
    7
    
    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) 
    
  5. 过期时间选择之后,会提示确认上述的操作,直接输入 y 确认。

  6. 设置相关的用户标识。上一步确认之后,会提示输入用户名,邮箱和注释这三个内容,按自己的情况输入即可。输入并确认后 GPG 密钥则生成。

    1
    2
    3
    4
    5
    6
    7
    
    GnuPG needs to construct a user ID to identify your key.
    
    Real name: name
    Email address: name@example.com
    Comment:
    You selected this USER-ID:
        "name <name@example.com>"
    

查看已经生成的密钥

1
gpg --list-secret-keys --keyid-format=long

显示如下(相关内容已经过处理):

1
2
3
4
sec   ed25519/5D4C3FXXXXXX2034EF xxxx-xx-xx [SC]
      D27B94066FFDEDXXXXXXXD4885D4C3F97ASD034EF
uid                 [ultimate] name <name@example.com>
ssb   cv25519/901FXXXAXX204X8C xxxx-xx-xx [E]

导出公钥

这里公钥的导出以上面的 5D4C3FXXXXXX2034EF 为例,命令如下:

1
gpg --armor --export 5D4C3FXXXXXX2034EF

显示的公钥格式如下(中间的具体内容以 ··· 代替):

1
2
3
4
-----BEGIN PGP PUBLIC KEY BLOCK-----

...
-----END PGP PUBLIC KEY BLOCK-----

配置 Git 提交时签名

Github 配置密钥

  1. 我们先导出当前要使用的公钥

  2. 点击 Github 右上角的头像,点击 Settings 选项。

  3. 在左侧的页面单击 SSH and GPG keys 选项。

  4. GPG keys 旁边,点击 New GPG key 按钮。

  5. 在新页中的 Title 输入自己的备注信息,在 Key 中填入第一步中导出的公钥。

  6. 点击 Add key 按钮,公钥添加成功。

本地配置

  1. 配置本地 Git 签名使用的密钥 ID,我们这里使用的密钥 ID 为:5D4C3FXXXXXX2034EF (与配置在 Github 里面的保持一致)。

    1
    2
    
    # 这里添加 --global 表示设置为全局
    git config --global user.signingkey 5D4C3FXXXXXX2034EF
    
  2. 配置默认对所有提交进行签名,命令如下:

    1
    
    git config --global commit.gpgsign true
    
  3. 可以使用 --unset 取消指定的签名 ID。如下:

    1
    
    git config --global --unset user.signingkey