Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Being Hexed by Reading MD5 Sums

by arunhorne (Pilgrim)
on Jul 05, 2002 at 11:42 UTC ( [id://179608]=perlquestion: print w/replies, xml ) Need Help??

arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have this strange problem... :)

I have an array of data and for each element in the array I create a 128-bit MD5 Sum and write it to a file followed by a newline, thus:

foreach $data (@elements) { print OUTSUMFILE md5($data), "\n"; }

This works fine and results in a binary file (when viewed with Ultraedit and friends) as would be expected. However, it is necessary for me to re-read these MD5 sums from disk at an arbitrary point in the future... i.e. I can't just keep them in memory. I though the following would work:

while (<INSUMFILE>) { chomp; push @md5sums, $_; }

For the most part it does, but one or two MD5 sums are not coming through correctly. I speculate that it may be because I need to adopt a different approach to file reading for binary files? It is not inconceivable that an MD5 sum could contain the \n character thus causing the MD5 sum to be misread.

I'd be interested in two things, firstly, people's opinions on if this is/could be the case but secondly I anyone knows how I could achieve successfully loading the MD5 sums from disk without breaking compatability with the way I write them in the first place.

Thanks...

____________
Arun

Replies are listed 'Best First'.
Re: Being Hexed by Reading MD5 Sums
by Abigail-II (Bishop) on Jul 05, 2002 at 11:49 UTC
    Since you know the size of each MD5 hash (128 bits aka 16 bytes), you could just read in 17 bytes at a time (to include the newline). You can do this by either using read (or sysread), or to set $/ to a reference to 17. (On a DOSish system you may actually have to read in 18 bytes, to deal with the end of line sillyness.)

    Abigail

Re: Being Hexed by Reading MD5 Sums
by strat (Canon) on Jul 05, 2002 at 11:53 UTC
    If you are writing binary data to a file, try to set binmode (OUTSUMFILE), and also to the file you are reading from. This is necessary under operating systems like Windows that treat binary and ascii-files in a different way. IIRC, under Linux or Unix there won't be any difference.

    Md5: is it possible for you to use the hex digests? They are rather easy to deal with, e.g.

    use Digest::MD5; sub GetMD5Digest { return (&Digest::MD5::md5_hex($_[0])); } # GetMD5Digest
    or, if you want to get it in a more "human" readable form, you could enhance it about like in the following example:
    use Digest::MD5; sub GetMD5Digest { # former known as md5Hash my $digest = &Digest::MD5::md5_hex($_[0]); # split up by pairs, eg. aab23f => aa b2 3f $digest =~ s/([A-Za-z0-f][A-Za-z0-f])/$1 /g; chop($digest); return ($digest); } # GetMD5Digest

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Being Hexed by Reading MD5 Sums
by crazyinsomniac (Prior) on Jul 05, 2002 at 11:54 UTC
Re: Being Hexed by Reading MD5 Sums
by yodabjorn (Monk) on Jul 05, 2002 at 11:53 UTC
    try using binmode
    heres an example:
    #!/usr/bin/perl use Digest::MD5 ; use warnings ; use strict ; foreach (@ARGV) { open(FILE, "$_") or die "Can't open '$_': $!"; binmode(FILE); print Digest::MD5->new->addfile(*FILE)->hexdigest, " $_\n"; }

Re: Being Hexed by Reading MD5 Sums
by Anonymous Monk on Jul 05, 2002 at 11:58 UTC
    Wouldn't it be much easier to use md5_hex() to generate nice easy to read/write digests?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://179608]
Approved by dwiz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found