GPG密钥生成
一、使用Kleopatra(Windows)
官网:https://www.gpg4win.org/
- 使用Kleopatra生成密钥后要导入到Git中。
二、使用Git bash中的GPG
生成密钥
gpg --full-generate-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]
二、GPG密钥导入导出
查看私钥
gpg -K --keyid-format=long
gpg --list-secret-keys --keyid-format=long
查看公钥
gpg -k --keyid-format=long
gpg --list-public-keys --keyid-format=long
导出公钥
gpg --armor --export <keyId>
导出公钥文件
gpg -a -o public-file.key --export <keyId>
导出私钥文件
gpg -a -o private-file.key --export-secret-keys <keyId>
删除公钥
gpg --delete-keys <keyId>
删除私钥
gpg --delete-secret-keys <keyId>
密钥导入
gpg --import public-file.key
信任密钥
gpg --edit-key <keyId>
在GPG界面输入
trust
选择适当的信任级别(通常选择 “5 = I trust ultimately”)
Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)
1 = I don't know or won't say
2 = I do NOT trust
3 = I trust marginally
4 = I trust fully
5 = I trust ultimately
m = back to the main menu
Your decision?
最后保存
save
三、设置GPG密钥
密钥格式(openpgp
,ssh
,x509
)
git config gpg.format openpgp
git config --global gpg.format openpgp
全局设置(私钥)
git config --global user.signingkey <keyId>
当前仓库设置(私钥)
git config --local user.signingkey <keyId>
全局签名
git config --global commit.gpgsign true
当前仓库签名
git config commit.gpgsign true
提交签名(commit.gpgsign为true自动签名)
git commit -S -m "YOUR COMMIT MESSAGE"
git log --show-signature # 查看签名状态
四、Github或Gitee使用GPG
- 使用
gpg --armor --export
将导出的GPG公钥设置到账户
- Kleopatra:选择证书->细节->导出
导入平台的公钥并信任
- Gitee:https://gitee.com/help/articles/4248#article-header6
- Github:
curl https://github.com/web-flow.gpg | gpg --import