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

Hi Monks I have the following snippet of code which creates an MD5 hash of a folder using the Digest::MD5::File module. Ideally I just want to use the Digest::MD5 module but from what I've read it can't MD5 hash a folder. I'm unable to get IT to install the Digest::MD5::File module (slow process of installing and testing software) so how do I go about fixing the bottom piece of code to MD5 hash a folder successfully or can you suggest another way to create a hash of a folders contents using the standard MD5 module.

use Digest::MD5; use Digest::MD5::File; ..... $filemd5->addfile($MD5); my $md5value = $filemd5->hexdigest(); push @md5list,"$md5value,$file"; } my $targetmd5 = $p00path . $p00 . '.md5'; my $MD5 = undef; if (not open ($MD5,'>',$targetmd5)) { print "Could not open $targetmd5 for write,Error: $!\n"; next P00; } my $qcmd5 = Digest::MD5->new(); # Make MD5 Object for checksum print $MD5 "# MD5 File for Optical Media ID $p00\n"; print $MD5 "# ISO created by $name $datestring\n"; foreach my $line (@md5list) { $qcmd5->add($line); print $MD5 "$line\n"; } my $folderMD5 = Digest::MD5::File->new; $folderMD5->adddir($p00path); my $finalmd5 = $folderMD5->hexdigest; print $MD5 "CHECKSUM: $finalmd5\n"; close $MD5; print "Made MD5 file for $p00\n";
My previous code below wasn't creating an MD5 hash of the folder it was creating a hash of the path and the item number...
$filemd5->addfile($MD5); my $md5value = $filemd5->hexdigest(); push @md5list,"$md5value,$file"; } my $targetmd5 = $p00path . $p00 . '.md5'; my $MD5 = undef; if (not open ($MD5,'>',$targetmd5)) { print "Could not open $targetmd5 for write,Error: $!\n"; next P00; } # Make MD5 Object for checksum my $qcmd5 = Digest::MD5->new(); print $MD5 "# MD5 File for Optical Media ID $p00\n"; print $MD5 "#\n"; foreach my $line (@md5list) { $qcmd5->add($line); print $MD5 "$line\n"; } my $finalmd5 = $qcmd5->hexdigest(); print $MD5 "CHECKSUM: $finalmd5\n";

Thanks

Replies are listed 'Best First'.
Re: MD5 Hash of a folder query
by davido (Cardinal) on Jul 09, 2015 at 14:45 UTC

    Digest::MD5::File is about 237 lines of fairly legible code, of which probably 100 lines aren't relevant to your task. That leaves you with not too much code to grok. And it relies on Digest::MD5 to do the hashing. You could just read that source code and learn the technique used there.


    Dave