Reputation: 564
I use the following script to get md5 of files and it works a treat as expected:
use warnings;
use strict;
use Digest::MD5;
my $file = "$ARGV[0]";
open (my $fh, '<', $file) or die "Can't open '$file': $!";
binmode ($fh);
my $md5 = Digest::MD5->new->addfile($fh)->hexdigest;
print $file, "\n", $md5, "\n";
What i want to do is use the script above, and then compare that $md5
with another already known md5.
EDIT: The idea is to check certain predefined files for validity. I already know the md5's of the "good" files, I just need to compare the known md5's with the ones obtained from this script above. I am just unsure of what to do next.
EDIT 2:
I have figured out how to push objects into the array with push(@array, $filehandle);
now i just need to create another array with the predefined md5s, which raises my next question, is there a way to see if one entry in array A
is the same as ANY entry in array B
?
EDIT 3: I have figured out how to actually compare the two md5's doing this:
use warnings;
use strict;
use Digest::MD5;
my $file = "$ARGV[0]";
my $file1 = "$ARGV[1]";
open (my $fh, '<', $file) or die "Can't open '$file': $!";
binmode ($fh);
open (my $fh1, '<', $file1) or die "Can't open '$file': $!";
binmode ($fh1);
my $md5 = Digest::MD5->new->addfile($fh)->hexdigest;
my $md51 = Digest::MD5->new->addfile($fh1)->hexdigest;
print "\n", $file, "\n", $md5, "\n", "\n", $file1, "\n", $md51, "\n";
if ($md5 eq $md51) {
print "\n", "The MD5's match perfectly", "\n";
} else {
print "\n", "The MD5's do NOT match", "\n";
}
But this compares the md5 of the two args. What i need to figure out is how to compare the argv0 md5 against an entire array of predefined md5's.
Upvotes: 0
Views: 787
Reputation: 564
This little script right here is exactly what i was after. This code will accept any file as an argument, then it will get the md5 digest of that file. Once it has the md5, it compares it against the entire array. My only question now would be... If i have 1200 md5's, will that slow the script down at all? The md5's i am checking are gonna be on filenames that never changes. would it be quicker to add multiple arrays, one for each filename, Then it would read the filename and search only that array. Thank you all for the help, and i am sure ill be back sooner or later. lol
use warnings;
use strict;
use Digest::MD5;
my $file = "$ARGV[0]";
open (my $fh, '<', $file) or die "Can't open '$file': $!";
binmode ($fh);
my $md5 = Digest::MD5->new->addfile($fh)->hexdigest;
print "\n", $file, "\n", $md5, "\n";
my @array = ('d41d8cd98f00b204e9800998ecf8427e', 'd6721344ed0cdc2e8a054a68b7ebc365', 'cee8eb94fd83f8d534bc44bf136ebaa0');
if( $md5 ~~ @array ) {
print "matches array\n";
} else {
print "does not match array\n";
}
system ( 'pause' );
Upvotes: 0
Reputation: 9256
You could take the expected MD5 value as an argument to the script:
#!/usr/bin/perl
use warnings;
use strict;
use Digest::MD5;
use Getopt::Std;
our $opt_e;
getopt( 'e:' );
my $file = "$ARGV[0]";
open( my $fh, '<', $file ) or die "Can't open '$file': $!";
binmode( $fh );
my $md5 = Digest::MD5->new->addfile( $fh )->hexdigest;
print $file, "\n", $md5, "\n";
if ( defined $opt_e ) {
if ( $md5 eq $opt_e ) {
print "MD5 value matches. Rejoice!\n";
}
else {
print "MD5 value does not match expected\n";
}
}
However, this has all been done before with the md5sum
command, so it might be better to look into that.
Upvotes: 1