简体   繁体   English

验证文件的 SHA-256 总和,如果不符合预期则退出脚本

[英]Verify SHA-256 sum of a file and exit the script if not as intended

I am attempting to make a bash script to auto-install a software I frequently work with, and I've arrived at a small issue.我正在尝试制作一个 bash 脚本来自动安装我经常使用的软件,但我遇到了一个小问题。 I would like my script to verify the validity of the downloaded file's sha256sum and either respond with a message of success or exit with an error message if it isn't what is expected.我希望我的脚本验证下载文件的 sha256sum 的有效性,如果不是预期的,则以成功消息响应或以错误消息退出。

Here's what I've tried so far (It works somewhat but doesn't exit on failure):这是我到目前为止尝试过的(它有点工作但不会在失败时退出):

shasum panel.tar.gz |
awk $1=="acca80528628ad362c5733229203a6c4bb3d648a9c40be318ff9f4f9653d505d"{print"SHA256SUM validated, installation may proceed"}

It's a part of an installation script, I'd like for it to verify the shasum and then if it doesn't match the expected acca80528628ad362c5733229203a6c4bb3d648a9c40be318ff9f4f9653d505d , it should exit the entire script.它是安装脚本的一部分,我希望它验证 shasum,然后如果它与预期的acca80528628ad362c5733229203a6c4bb3d648a9c40be318ff9f4f9653d505d不匹配,它应该退出整个脚本。 The line I gave above keeps the script going as-is without stopping, even if the shasum doesn't match the expected.我上面给出的行保持脚本按原样运行而不会停止,即使 shasum 与预期不匹配。

Use a process substitution to create the checksum file for shasum -c on the fly:使用进程替换来动态创建shasum -c的校验和文件:

checksum=acca80528628ad362c5733229203a6c4bb3d648a9c40be318ff9f4f9653d505d
shasum -c <(echo "$checksum panel.tar.gz")

Or pipe the contents to shasum , using - as the file name:或者将内容通过管道传送到shasum ,使用-作为文件名:

echo "$checksum panel.tar.gz" | shasum -c -

Look at the exit status to determine whether to continue.查看退出状态以确定是否继续。

if ! echo "$checksum panel.tar.gz" | shasum -c -; then
    echo "Checksum failed" >&2
    exit 1
fi

If you don't want to do anything other than exit immediately, you can use如果你不想立即退出以外的任何事情,你可以使用

echo "$checksum panel.tar.gz" | shasum -c - || exit 1

Simple use case, sign and verify a file against a given sum, in the view of proving ownership, and content integrity.简单的用例,在证明所有权和内容完整性的情况下,根据给定的金额对文件进行签名和验证。


The author publish a file, and a sha256 fingerprint sum.作者发布了一个文件,以及一个sha256指纹总和。

To get the sum, the author simply does:为了得到总和,作者简单地做了:

sha256sum file

He gets a sha256 sum and the file name, separated by two spaces .他得到一个 sha256 和和文件名,用两个空格隔开


The reader, or anyone wanting to verify if the file is NOT altered , can do:读者或任何想要验证文件是否未更改的人可以执行以下操作:

if [[ $(sha256sum file) = "75efecda52220be1c97ab2b29cd2880a089dff40b0f7967653535cb8b02e8cc2  file" ]];then echo "Signature OK"; fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM