为什么看似空的文件和字符串会产生md5sums? [英] Why do seemingly empty files and strings produce md5sums?
问题描述
请考虑以下内容:
% md5sum /dev/null
d41d8cd98f00b204e9800998ecf8427e /dev/null
% touch empty; md5sum empty
d41d8cd98f00b204e9800998ecf8427e empty
% echo '' | md5sum
68b329da9893e34099c7d8ad5cb9c940 -
% perl -e 'print chr(0)' | md5sum
93b885adfe0da089cdf634904fd59f71 -
% md5sum ''
md5sum: : No such file or directory
首先,我对所有这些命令的输出感到惊讶.如果有的话,我希望它们的总和是相同的.
First of all, I'm surprised by the output of all these commands. If anything, I would expect the sum to be the same for all of them.
推荐答案
无"(长度为零的字符流)的md5sum是d41d8cd98f00b204e9800998ecf8427e,您在前两个示例中会看到它.
The md5sum of "nothing" (a zero-length stream of characters) is d41d8cd98f00b204e9800998ecf8427e, which you're seeing in your first two examples.
第三个和第四个示例正在处理单个字符.在"echo"情况下,这是换行符,即
The third and fourth examples are processing a single character. In the "echo" case, it's a newline, i.e.
$ echo -ne '\n' | md5sum
68b329da9893e34099c7d8ad5cb9c940 -
在perl示例中,它是一个值为0x00的单个字节,即
In the perl example, it's a single byte with value 0x00, i.e.
$ echo -ne '\x00' | md5sum
93b885adfe0da089cdf634904fd59f71 -
您可以使用"echo"重现空的校验和,如下所示:
You can reproduce the empty checksum using "echo" as follows:
$ echo -n '' | md5sum
d41d8cd98f00b204e9800998ecf8427e -
...并按以下方式使用Perl:
...and using Perl as follows:
$ perl -e 'print ""' | md5sum
d41d8cd98f00b204e9800998ecf8427e -
在所有四种情况下,您都应该期望对相同数据进行校验和得到相同的输出,但是不同的数据应该产生截然不同的校验和(这很重要,即使只是一个不同的字符也是如此).
In all four cases, you should expect the same output from checksumming the same data, but different data should produce a wildly different checksum (that's the whole point -- even if it's only a single character that differs.)
这篇关于为什么看似空的文件和字符串会产生md5sums?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!