简体   繁体   English

从 Linux 命令行仅生成 SHA-256 hash

[英]Generating just a SHA-256 hash from the Linux command line

I'm looking for a single command that emits just the sha256 hash, as a hexadecimal number, of the contents of a single supplied file.我正在寻找一个命令,它只发出单个提供文件的内容的 sha256 hash,作为十六进制数。

I am aware of shasum -a 256 , openssl dgst -sha256 , sha256sum et al.我知道shasum -a 256openssl dgst -sha256sha256sum等。 but they all emit other information together with the checksum and I would like to avoid the need for post-processing the result with sed or some such.但它们都会与校验和一起发出其他信息,我想避免使用sed或类似的东西对结果进行后处理。

You may use:您可以使用:

sh -c 'shasum < "$1" | cut -d" " -f1' -- "$file"

The following perl one-liner actually seems to satisfy all my stated requirements:以下 perl 单线实际上似乎满足了我声明的所有要求:

perl -e 'use Digest::SHA;' -e 'use warnings;' -e 'use strict;' -e 'use autodie;' -e 'my $sha = Digest::SHA->new(256); open my $fh, $ARGV[0]; $sha->addfile($fh, "b"); print $sha->hexdigest . "\n";' "$file"

In particular it is safe for "funny" file names and can be used for arbitrary shell scripting, including as a find --exec argument (which is one thing I need it for).特别是它对于“有趣”的文件名是安全的,并且可以用于任意 shell 脚本,包括作为find --exec参数(这是我需要它的一件事)。

(I have not written in Perl for a very long time, so the above can probably be improved.) (我很久没有写Perl了,所以上面大概可以改进一下。)

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

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