string 为什么看似空的文件和字符串会产生 md5sums?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10909976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do seemingly empty files and strings produce md5sums?
提问by Daniel
Consider the following:
考虑以下:
% 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.
首先,我对所有这些命令的输出感到惊讶。如果有的话,我希望他们所有人的总和都是一样的。
回答by Graeme
The md5sum of "nothing" (a zero-length stream of characters) is d41d8cd98f00b204e9800998ecf8427e, which you're seeing in your first two examples.
“无”(零长度字符流)的 md5sum 是 d41d8cd98f00b204e9800998ecf8427e,您在前两个示例中看到了它。
The third and fourth examples are processing a single character. In the "echo" case, it's a newline, i.e.
第三个和第四个例子是处理单个字符。在“echo”的情况下,它是一个换行符,即
$ echo -ne '\n' | md5sum
68b329da9893e34099c7d8ad5cb9c940 -
In the perl example, it's a single byte with value 0x00, i.e.
在 perl 示例中,它是一个值为 0x00 的单个字节,即
$ echo -ne '\x00' | md5sum
93b885adfe0da089cdf634904fd59f71 -
You can reproduce the empty checksum using "echo" as follows:
您可以使用“echo”重现空校验和,如下所示:
$ echo -n '' | md5sum
d41d8cd98f00b204e9800998ecf8427e -
...and using Perl as follows:
...并使用 Perl 如下:
$ 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.)
在所有四种情况下,您应该期望对相同数据进行校验和得到相同的输出,但不同的数据应该产生截然不同的校验和(这就是重点——即使只有一个字符不同。)
回答by mykhal
Why do seemingly empty files and strings produce md5sums?
为什么看似空的文件和字符串会产生 md5sums?
Because the "sum" in the md5sumis somewhat misleading. It's not like e.g. CRC32 checksum, that is zero for the empty file.
因为md5sum 中的“ sum”有点误导。它不像CRC32校验和,空文件为零。
MD5 is one of message digest algorithms. You can imagine it as a box that produces fixed-length random-looking value (hash) depending on its internal state. You change the internal state by feeding in the data.
MD5 是消息摘要算法之一。你可以把它想象成一个盒子,它根据其内部状态产生固定长度的随机值(散列)。您可以通过输入数据来更改内部状态。
And that box internal state is predefined, such that that it yields randomly looking hash value even before any data is fed in. For MD5, it happens to be d41d8cd98f00b204e9800998ecf8427e
.
并且该框内部状态是预定义的,因此即使在输入任何数据之前,它也会产生随机外观的哈希值。对于 MD5,它恰好是d41d8cd98f00b204e9800998ecf8427e
.
回答by Gene
No need for surprise. The first two produce true empty inputs to md5sum. The echo produces a newline (echo -n ''
should produce an empty output; I don't have a linux machine here to check). The perl produces a single zero byte (not to be confused with C where a zero byte marks end of string). The last command is looking for a file with the empty string as its file name.
无需惊讶。前两个为 md5sum 生成真正的空输入。echo 产生一个换行符(echo -n ''
应该产生一个空的输出;我这里没有 linux 机器来检查)。perl 生成单个零字节(不要与 C 混淆,其中零字节标记字符串的结尾)。最后一个命令正在寻找一个以空字符串作为文件名的文件。