bash 脚本检查 GPG 签名是否有效并且属于密钥

发布于 2024-10-30 13:31:28 字数 712 浏览 0 评论 0原文

我正在尝试编写一个 bash 脚本来检查给定的签名是否有效。我有两个可能的输出:

$ gpg --no-default-keyring --keyring /etc/pubring.gpg  --verify file.tgz.sig file.tgz

WRONG

gpg: Signature made Tue 05 Apr 2011 11:01:19 CEST using RSA key ID E32804F0
gpg: Can't check signature: public key not found

RIGHT

gpg: Signature made Tue 05 Apr 2011 11:01:19 CEST using RSA key ID E32804F0
gpg: Good signature from "Test key <test@localhost>"

我如何能够检测检查是否正确而无需解析结果。

这个问题类似于 Verify GPG file signature with Perl 但我想在 bash 中执行此操作(或者如果非常需要 Python。)

I am trying to write a bash script that checks if a given signature is valid or not. I have two possible outputs from:

$ gpg --no-default-keyring --keyring /etc/pubring.gpg  --verify file.tgz.sig file.tgz

WRONG

gpg: Signature made Tue 05 Apr 2011 11:01:19 CEST using RSA key ID E32804F0
gpg: Can't check signature: public key not found

RIGHT

gpg: Signature made Tue 05 Apr 2011 11:01:19 CEST using RSA key ID E32804F0
gpg: Good signature from "Test key <test@localhost>"

How I can detect if the checking was right without having to parse the result.

This question is similar to Verify GPG file signature with Perl but II would like to do that in bash (or if very needed Python.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-11-06 13:31:28

我不知道 gpg 命令,但它是否会为“错误”和“正确”结果返回不同的退出值?运行命令后检查这一点的最简单方法是:

echo $?

如果一切正常,我希望它返回 0,否则返回其他值。所以你的 bash 脚本看起来像:

gpg --no-default-keyring --keyring /etc/pubring.gpg --verify file.tgz.sig file.tgz

if [ $? -eq 0 ]
then
    echo All is well.
else
    echo Problem with signature.
fi

I don't know the gpg command but does it return a different exit value for the "wrong" and "right" results? The easiest way to check this after running the command would be:

echo $?

I would expect it to return 0 if everything is OK and something else if not. So your bash script would look like:

gpg --no-default-keyring --keyring /etc/pubring.gpg --verify file.tgz.sig file.tgz

if [ $? -eq 0 ]
then
    echo All is well.
else
    echo Problem with signature.
fi
帅气尐潴 2024-11-06 13:31:28

来自 GnuPG 手册页:

如果一切正常,程序返回 0,如果至少有一个签​​名错误,则返回 1,如果出现致命错误,则返回其他错误代码。

所以你可以使用 http://docs.python.org/library/subprocess.html获取gpg的返回码。

From the GnuPG man page:

The program returns 0 if everything was fine, 1 if at least a signature was bad, and other error codes for fatal errors.

So you can use http://docs.python.org/library/subprocess.html to get the return code of gpg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文