如何自动比较大量文件的md5sum哈希值

我可以在终端中检查文件的md5sum哈希值,方法如下:
$ md5sum my_sensitive_file
8dad53cfc973c59864b8318263737462 my_sensitive_file

但是困难的部分是将哈希值与确切的值进行比较。

对于大量文件,任何人都很难将32个字符的输出与原始/确切的哈希值进行比较。首先,这项工作将非常单调,而且存在很大的错误范围。

是否可能自动化比较过程,最好在CLI中?


有关一个不需要命令行界面的解决方案,需要一些工作但提供了一些优势,请参阅https://raywoodcockslatest.wordpress.com/2021/12/08/ransomware-hash/。 - Ray Woodcock
3个回答

例如,我有一个名为test_binary的文件。
文件test的MD5校验和为ef7ab26f9a3b2cbd35aa3e7e69aad86c
要自动测试它,请运行以下命令:
$ md5sum -c <<<"ef7ab26f9a3b2cbd35aa3e7e69aad86c *path/to/file/test_binary"
test_binary: OK

或者

$ echo "595f44fec1e92a71d3e9e77456ba80d1  filetohashA.txt" | md5sum -c -

男人的引言

   -c, --check
          read MD5 sums from the FILEs and check them

来自维基的引用

注意:每个md5sum值和要比较的文件名之间必须有两个空格。否则,将出现以下错误:"找不到正确格式的MD5校验和行"。

链接到维基

另外,你也可以直接从文件中读取md5哈希值

$ md5sum -c md5sum_formatted_file.txt

它期望的文件格式为:
<md5sum_checksum><space><space><file_name>

关于 MD5 校验和后的 *<space>。手册中有一小段注释:
 When  checking,  the
       input  should  be a former output of this program.  The default mode is
       to print a line with checksum, a character indicating input  mode  ('*'
       for binary, space for text), and name for each FILE.

这里是链接到我在stackoverflow上找到的关于为什么有时候我们应该区分二进制文件和文本文件的问题的答案。

3星号(*)是否必要? - jobin
有趣的问题。我一直使用*,但维基说应该是两个空格。我会去搜索一下... - c0rp
@souravc好的,我找到了关于*的信息,很快就会更新。 - c0rp
@Jobin 我添加了关于 * 的信息来回答你的问题。 - c0rp
有道理。点赞。 - jobin
至少在命令行上,一个空格似乎同样有效。 - O. R. Mapper

一种可能性是使用实用工具cfv
sudo apt-get install cfv

CFV支持多种类型的哈希算法,既可以进行测试,也可以创建哈希文件。
# List the files
$ ls
test.c
# Create a hash file
$ cfv -tmd5 -C
temp.md5: 1 files, 1 OK.  0.001 seconds, 302.7K/s
# Test the hash file
$ cfv -tmd5 -T
temp.md5: 1 files, 1 OK.  0.001 seconds, 345.1K/s
# Display the hash file
$ cat *.md5
636564b0b10b153219d6e0dfa917d1e3 *test.c

1谢谢你的回答,但是需要安装另一个工具。同时它也支持其他格式。了解这一点很好。但在目前的情况下,我会选择另一个答案。不管怎样,我给你加1分。 - sourav c.
1cfv在20.04版本中已从软件仓库中移除。一个替代方案是rhash。该程序可能最终会迁移到Python 3。详见https://github.com/cfv-project/cfv/issues/8。 - LiveWireBT

是的,星号 * 在这个命令中是必需的。看一下这个例子。
这是一个二进制文件,假设正确的 md5sum 值是 exampleofcorrectmd5value00000000(32个十六进制字符)。
[root@Linux update]# ls -lh
total 137M
-rw-r--r-- 1 root root 137M Nov  5 13:01 binary-file.run.tgz
[root@Linux update]# 

-c,--check 从文件中读取MD5值并进行校验
如果MD5值与二进制文件匹配,则会得到此输出。
[root@Linux ~]# md5sum -c <<< "exampleofcorrectmd5value00000000" *binary-file.run.tgz"
binary-file.run.tgz: OK
[root@Linux ~]# 

当md5sum值不匹配时。
[root@Linux update]# md5sum -c <<< "exampleofwrongmd5value0000000000 *binary-file.run.tgz"
binary-file.run.tgz: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
[root@Linux update]# 

没有星号*,即使md5值是正确的,您也会收到以下错误消息
[root@Linux ~]# md5sum -c <<< "exampleofcorrectmd5value00000000 binary-file.run.tgz" 
md5sum: standard input: no properly formatted MD5 checksum lines found
[root@Linux ~]# 

如果md5sum中没有32个十六进制字符,你也会得到相同的错误信息。在这个例子中,它只有31个字符。
[root@Linux ~]# md5sum -c <<< "exampleofmd5valuelessthan32char *binary-file.run.tgz" 
md5sum: standard input: no properly formatted MD5 checksum lines found
[root@Linux ~]# 

处理多个文件的解决方案

如果您有很多文件并且希望自动化处理过程,可以按照以下步骤进行操作:

user@Ubuntu:~$ ls -lh
total 12K
-rw-rw-r-- 1 user user 4 Nov  5 14:54 file-a
-rw-rw-r-- 1 user user 4 Nov  5 14:54 file-b
-rw-rw-r-- 1 user user 4 Nov  5 14:54 file-c
user@Ubuntu:~$ 

为每个文件生成md5sum并将其保存到md5sum.txt中

user@Ubuntu:~$ md5sum * | tee md5sum.txt
0bee89b07a24ae27c83fc3d5951213c1  file-a
1b2297c171a9a450d184871ccf6c9ad4  file-b
7f4d13d9b0b6ac086fd68637067435c5  file-c
user@Ubuntu:~$ 

要检查所有文件的md5sum,请使用以下命令。
user@Ubuntu:~$ md5sum -c md5sum.txt 
file-a: OK
file-b: OK
file-c: OK
user@Ubuntu:~$ 

如果md5sum值与文件不匹配,这是一个例子。在这种情况下,我将修改file-b的内容。
user@Ubuntu:~$ echo "new data" > file-b 
user@Ubuntu:~$ 

看,这是错误信息。希望能帮到你。
user@Ubuntu:~$ md5sum -c md5sum.txt 
file-a: OK
file-b: FAILED
file-c: OK
md5sum: WARNING: 1 computed checksum did NOT match
user@Ubuntu:~$