GPG 开发者速查表
1. 密钥管理 (Key Management)
| 命令 | 说明 |
|---|---|
gpg -k | 列出所有公钥 |
gpg -K | 列出所有私钥(含 Keygrip) |
gpg -K --with-keygrip | 最常用:查看密钥 ID 及对应的 Keygrip |
gpg --import <file.asc> | 导入公钥/私钥 |
gpg -a --export <ID> | 导出 ASCII 格式公钥 |
2. 签名与验证 (Signing & Verification)
| 命令 | 说明 |
|---|---|
gpg -s file | 对文件进行签名(二进制) |
gpg -a -b file | 最常用:生成 ASCII 格式的独立签名文件(.asc) |
gpg --verify file.asc file | 验证签名 |
gpg --detach-sign -a file | 对文件进行分离式 ASCII 签名 |
3. 加密与解密 (Encryption & Decryption)
| 命令 | 说明 |
|---|---|
gpg -c file | 对称加密(仅密码,无需密钥对) |
gpg -e -r <ID> file | 使用特定公钥加密 |
gpg -d file.gpg | 解密文件 |
4. Agent 维护 (Troubleshooting)
这是解决 Magit/IDE 卡死的关键:
- 查看缓存状态:
gpg-connect-agent 'keyinfo --list' /bye - 重载 Agent (清缓存):
gpg-connect-agent reloadagent /bye - 手动预缓存密钥:
gpg-preset-passphrase --preset <KEYGRIP>(需在gpg-agent.conf中开启allow-preset-passphrase)
💡 核心配置文件参考
~/.gnupg/gpg-agent.conf:
# 确保使用 pinentry-mac (macOS) 或 pinentry-curses (Linux)
pinentry-program /opt/homebrew/bin/pinentry-mac
# 缓存设置 (秒)
default-cache-ttl 3600
max-cache-ttl 86400
# 允许预缓存
allow-preset-passphrase
🛠️ 建议 Alias (加入 .zshrc/.bashrc)
alias gpg-keys='gpg -K --with-keygrip'
alias gpg-flush='gpg-connect-agent reloadagent /bye'
注:对于 Magit 用户,如果依然卡死,请在 init.el 中添加 (setq epa-pinentry-mode 'loopback)。