Windows下保持GPG的passphrase长期有效

引言

在开启了Git的GPG签署能力后,发现经常需要进行GPG私钥的passphrase输入,在解决这个问题过程中,得出了一些解决方案,记录一下。

安装Gpg4win

安装Gpg4win的目的是替换git-for-windows默认的pinentry程序,Gpg4win 会提供一个 pinentry-qt 程序来代替默认的 pinentry,主要是界面好看一点。

其次,Gpg4win 提供了一个二进制程序 Kleopatra 他是 gpg-agent 的一个封装代理程序,可以更好的帮助配置 gpg-agent.conf 文件,而不需要用户手动在文件夹中进行文件的编辑。

官网地址:https://gpg4win.org/download.html

Kleopatra配置gpg

Settings -> Configure Kleopatra -> GnuPG System -> Private Keys

通过Kleopatra来指定Private Keys的缓存时间,他有个缺点就是每次重启,缓存都会刷新,需要重新数据密码,这是最简单也是最终的配置方案。

windows配置GPG-Agent

该配置目前对我不生效

Windows 上 GnuPG 的主目录是 %APPDATA%\gnupg,所以gpg-agent.conf配置文件的路径需要配置在 %APPDATA%\gnupg\gpg-agent.conf 下面。

vim gpg-agent.conf

default-cache-ttl 360000000
max-cache-ttl 8640000000

几个常用GPG相关命令

查看系统是配置

PS C:\Users\xuthus> gpgconf
gpg:OpenPGP:/usr/bin/gpg
gpgsm:S/MIME:/usr/bin/gpgsm
gpg-agent:Private Keys:/usr/bin/gpg-agent
scdaemon:Smartcards:/usr/lib/gnupg/scdaemon
dirmngr:Network:/usr/bin/dirmngr
pinentry:Passphrase Entry:/usr/bin/pinentry

查看系统提供的所有gpg

xuthus@x MINGW64 ~
$ where gpg
C:\Program Files\Git\usr\bin\gpg.exe
C:\msys64\usr\bin\gpg.exe
C:\Program Files (x86)\GnuPG\bin\gpg.exe

Git配置GPG程序

$ git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

重载GPG-Agent配置文件

使用命令:

gpgconf --reload gpg-agent