Daniel Miron
Daniel Miron

Reputation: 460

perl and php md5 file checksum doesn't match

i'm trying to get the checksum fore a binary file running php's

$checksum =md5_file($fname)

and for perl

 use Digest::MD5;
 open FILE, "$fname";
 $ctx = Digest::MD5->new;
 $ctx->addfile(*FILE);
 $checksum = $ctx->hexdigest;

I get different results interesting enough running it on a small text file returned the same checksum

Upvotes: 1

Views: 993

Answers (2)

ikegami
ikegami

Reputation: 385789

You should use binmode(FILE) after opening the file. If you still have differences with that change, then look at your PHP code because the updated Perl code produces the correct output:

$ perl dm.pl .bashrc
f5bb0773a3346814d978f9a155176d8e

$ md5sum .bashrc
f5bb0773a3346814d978f9a155176d8e *.bashrc

Upvotes: 2

gpojd
gpojd

Reputation: 23065

With Perl, did you make sure that the file handle was in binmode as the documentation suggests?

In most cases you want to make sure that the $io_handle is in binmode before you pass it as argument to the addfile() method.

I modified the example from the documentation to match your example:

use Digest::MD5;
open (my $fh, '<', $fname) or die "Can't open '$fname': $!";
binmode ($fh);
my $checksum = Digest::MD5->new->addfile($fh)->hexdigest;

Upvotes: 3

Related Questions