CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Digest::MD5::digest woes: Edit Log



1arryb
User

Feb 23, 2009, 9:28 AM


Views: 8255
Digest::MD5::digest woes

Hi all,

I have a data warehousing application that identifies unique files by the MD5 of their content. I am using Digest::MD5 to calculate the digests. The database uses the binary digest (as returned by Digest::MD5::digest(), but I'd like the hex string too for logging.

I think I have discovered a bug in Digest::MD5. If I call its digest() method, subsequent calls to hexdigest() return a bogus value. The test program below illustrates this.

Please check my work before I file a bug report. In the meantime, I suppose I can workaround the problem by getting the hex string first.

On a related note, It would be great if I could independently check the values returned by Digest::MD5::digest(). I have some old code that purports to convert digests to strings, but it just does 'sprintf("%x", $digest)'. This doesn't seem to work anymore, if it ever did. I've been looking around at various techniques and haven't found an answer that produces the same string as md5sum. Now I'm getting worried that the hash itself may be wrong. What's the right way?

I have Perl 5.10 and the latest (2.38) version of Digest::MD5.
I've tried this on an x86_64 machine running Linux FC9 and on an x86 box running Windows XP and cygwin.

UPDATE
*deleted*

UPDATE2
I thought the problem transcended Digest::MD5 instances but I forgot to reinitialize the file handle. Never mind.

Thanks,

Larry


Code
#!/usr/bin/perl 

use Digest::MD5;

my $file = $ARGV[0];
open (IN, "<$file") or die "can't open $file";
# get a reference digest string.
my $refhex = `md5sum $file`;
chomp($refhex);
print "md5sum: $refhex\n";
my $md5obj = Digest::MD5->new;
$md5obj->addfile(*IN);
# Get the digest hex string from Digest::MD5.
my $hexdigest = $md5obj->hexdigest;
print "hexdig1: $hexdigest\n";
# Get the binary digest from Digest::MD5
print "[calling Digest::MD5::digest]\n";
my $digest1 = $md5obj->digest;
$hexdigest = $md5obj->hexdigest;
print "hexdig2: $hexdigest\n";
# but the digest method seems to return the same value each time.
my $digest2 = $md5obj->digest;
print "[internal digest is stable call to call]\n" if $digest1 == $digest2;



(This post was edited by 1arryb on Feb 23, 2009, 10:00 AM)


Edit Log:
Post edited by 1arryb (User) on Feb 23, 2009, 9:50 AM
Post edited by 1arryb (User) on Feb 23, 2009, 10:00 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives