开发者

MD5 checksum of the whole file is different from checksum of content

I created a file a.txt containing one word - 'dog'.

Here is a MD5 checksum:

$md5sum a.txt

c52605f607459b2b80e0395a8976234d  a.txt

Here is MD5 checksum of the word dog:

$perl -e "use Digest::MD5 qw(md5_base64 md5_hex); print(md5_hex('dog'));"

06d80eb0c50b49a509b49f开发者_运维知识库2424e8c805

Why are checksums different?

Thank you,

Martin


Presumably you have a newline at the end of the file. Try using echo -n:

$ perl -e "use Digest::MD5 qw(md5_base64 md5_hex); print(md5_hex('dog'));"
06d80eb0c50b49a509b49f2424e8c805
$ echo 'dog' >a.txt
$ md5sum a.txt
362842c5bb3847ec3fbdecb7a84a8692  a.txt
$ echo -n 'dog' >a.txt
$ md5sum a.txt
06d80eb0c50b49a509b49f2424e8c805  a.txt

This is quite a common question:

  • Why does Perl and /bin/sha1 give different results?
  • Why PHP's md5 is different from OpenSSL's md5?
  • Why is the same input returning two different MD5 hashes?


md5_base64 is just a function declaration.

use Digest::MD5 qw(md5_base64 md5_hex)

means that I can use functions md5_base64() or md5_hex() from the library Digest::MD5

Basically you can use some other tools than Perl to compute MD5 hash of the word...

I'm wondering why the checksum of the file (using md5sum) is different from the checksum of the content itself...

Does the md5sum append some information about the file to the content before computing MD5? Or is there some character "end of file"?

Thank you for your time...


When You perform and MD5 Calculation of a file (txt in your situation) the whole content of the file is taken in consideration even control chars (EOF, SOH, LF, CR) they are non printable chars but have some HEXA values which changes the corresponding MD5 result, which make different than the result of just passing a string to the MD5 function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜