gnupg - How to prevent gpg from choosing fallback secret key, when --default-key is not found?
问题描述
I'm calling the following command from a (Perl, bash) script:
gpg --batch --yes --default-key C0FFEEABCDEF0123 --clearsign some_file.txt
But that key (C0FFEEABCDEF0123
) does not exist in my keychain, because there is a typo or the key went missing, so gpg can't sign using that key.
When this happens, gpg looks in keychain, finds the "default default" key (meaning the one it would choose without --default-key
) and tries to sign with that one.
This results in a password prompt, which halts the script, because that key is encrypted. (I'm expecting one that is not encrypted, because this is a toy prototype.)
How do I make gpg give up if it can't find the specified key?
How do I make it give up if the key is encrypted and it can't be used without a password?
解决方案
How do I make gpg give up if it can't find the specified key?
gpg --batch --yes -u C0FFEEABCDEF0123 --clearsign some_file.txt
gpg: skipped "C0FFEEABCDEF0123": No secret key
gpg: some_file.txt: clear-sign failed: No secret key
How do I make it give up if the key is encrypted and it can't be used without a password?
You can use the --passphrase-fd --pinentry-mode loopback
arguments to provide an empty password (or perhaps change the pinentry program?).
echo ""|gpg -q --batch --yes -u C0FFEEABCDEF0123 --textmode --passphrase-fd 0 --pinentry-mode loopback --clearsign some_file.txt
gpg: signing failed: No passphrase given
gpg: some_file.txt: clear-sign failed: No passphrase given
To do this on Windows (no pipes):
@echo off > pass.txt && @echo on && gpg -q --batch --yes -u C0FFEEABCDEF0123 --textmode --passphrase-file pass.txt --pinentry-mode loopback --clearsign some_file.txt & del /q pass.txt
推荐阅读
- c# - 从全屏模式最小化后,AxMSTSCLib 显示在任务栏中消失
- javascript - 子字符串在 Javascript 中无法正常工作,如何在字符串之间添加开始?
- javascript - 在 ReactJS 中映射对象数组的问题
- operating-system - 为什么我得到的物理地址是 56 位而不是 arm64 上的 48 位?这是正确的方法吗?
- pyspark - Pyspark 数据帧拆分并将分隔列值填充到 N 索引数组中
- r - R 中针对大数据集的 Mann-Kendall 检验
- c++ - Eigen 是否有用于从另一个矩阵中提取子矩阵的内置方法?
- python-3.x - 如何将视频作为输入传递给Android Studio中的python代码
- freemarker - Freemarker 内存不足
- python - 如何使用 pytest-django 测试 Django 查询集是否相等