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: Win32 Programming Help:
Win32 binmode MD5 hash problem

 



RockE70
Novice

Sep 27, 2013, 6:08 AM

Post #1 of 6 (11127 views)
Win32 binmode MD5 hash problem Can't Post

Hi Gurus

I'm having an issue trying to get a correct MD5 hash in Win32 with a Perl script.

Could someone explain to me how I get the following to produce a correct MD5 hash


Code
my $md5zip = ($target_zip);               											 
my $md5list = ($list);
my $md5pdf = ($target_pdf);
my $md5file1 = Digest::MD5->new;
my $md5file2 = Digest::MD5->new;
my $md5file3 = Digest::MD5->new;
$md5file1->add($md5zip);
$md5file2->add($md5list);
$md5file3->add($md5pdf);
my $digest1 = $md5file1->hexdigest;
my $digest2 = $md5file2->hexdigest;
my $digest3 = $md5file3->hexdigest;
print "Zip file hash is $digest1\n";
print "List file hash is $digest2\n";
print "PDF file hash is $digest3\n";

my $md5hash = undef;
open($md5hash,'>',$md5digest) or die "$package: Could not open $md5hash or write, Error: $!\n"; # Make an MD5 file
print $md5hash "#MD5 hash listing of files for hard drive $P00\n";
print $md5hash "$P00.zip $digest1\n";
print $md5hash "$P00.lst $digest2\n";
print $md5hash "$P00.pdf $digest3\n";
close $md5hash;
print "$package: Wrote MD5 File $md5digest\n";


The output I get is

**********Writing MD5**********
Zip file hash is 41acb32b14fc62c92508c3662357adf9
List file hash is a5dfcbe09714ff9d19b0a403d438349f
PDf file hash is acb85a864c80a540d9b18ecd6bd44cd0

Using an external program I get this

P12345678.zip | af0aedecf28db46544adbd5e423197df
P12345678.lst | 96edfc8a689ae5dd34261d30b7947d2c
P12345678.pdf | df60f4d5032c2c2c5ea31b813b786cc5

Cheers


BillKSmith
Veteran

Sep 28, 2013, 11:49 AM

Post #2 of 6 (11116 views)
Re: [RockE70] Win32 binmode MD5 hash problem [In reply to] Can't Post

You are much more likely to get the right answer to your problem if you post a COMPLETE working program that demonstrates your problem. In this case, it would be better to do only one of the three files. Even better, use a test file and attach a copy of that file. Print only to the screen. The code to print to a file is clearly not related to your problem. It only clutters your post.

I do not see anything wrong with the code fragment that you posted. (Except for unneeded parenthesis in the first three statements.) From this fragment, we cannot tell if you want the hash of the name or of the contents of the three files. (Your title suggests the later.) Did you correctly specify that they are binary files? Are you importing the module correctly? Are you certain that you are using your external program correctly?
Good Luck,
Bill


FishMonger
Veteran / Moderator

Sep 28, 2013, 1:41 PM

Post #3 of 6 (11113 views)
Re: [RockE70] Win32 binmode MD5 hash problem [In reply to] Can't Post

What external program did you use and how did you run it?


FishMonger
Veteran / Moderator

Sep 28, 2013, 1:45 PM

Post #4 of 6 (11111 views)
Re: [RockE70] Win32 binmode MD5 hash problem [In reply to] Can't Post

My windows box is not currently setup to run a test, but here's one from my Linux box.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Digest::MD5;

my $file = 'named.conf';
open my $fh, $file or die $!;

my $digest = `openssl dgst -md5 $file`;
$digest =~ s/^.+?= //;

my $dgst = Digest::MD5->new;
$dgst->addfile($fh);

print $digest;
print $dgst->hexdigest, "\n";


Output:

Quote
[root@009-91-RKB-2 ~]# ./md5.pl
f328b32331c655f1e00a22692a4b8cbc
f328b32331c655f1e00a22692a4b8cbc



RockE70
Novice

Sep 29, 2013, 6:42 AM

Post #5 of 6 (11101 views)
Re: [RockE70] Win32 binmode MD5 hash problem [In reply to] Can't Post

Thanks for the replies, sorry for the delay I was interstate.

Here is my code for reading a usb hard drive, zipping it up, moving a scan of the hard drive label (PDF) into the directory and creating an MD5 file (which isn't producing the correct MD5 hash)


Code
#!/usr/bin/perl 
#
# usbzip
#
# Copies all files on a drive $sourcedrive eg: E: to a zip file $target_zip
# in a directory made from the $P00, A PDF file is then copied to this DIR
# An MD5 and a list file are also created for the zip file
#
package usbzip;
our $package = 'usbzip';
our $version = 0.7;

use strict;
use warnings;

use Digest::MD5 qw (md5 md5_hex md5_base64);
use File::Copy qw(copy);
use File::Copy qw(move);
# Issue with zip modules in Windows zipped files cannot be opened, until fixed use
#system installed Info-Zip
#use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
#use IO::Compress::Zip qw(zip $ZipError) ;
#
#----------------------------- Global Variables --------------------------------
#
our $sourcepath = 'C:\\Temp\\hddzip\\';
our $finalpath = 'C:\\Temp\\harddrives\\';
#-------------------------------------------------------------------------------

@ARGV or die "$package: no parameters given\n";

our $P00=shift;
chomp $P00;
$P00 =~qr{^[LMP]\d{8}$} or die "$package: $P00 is not a valid Media Identifier\n";

our $sourcedrive = shift;
chomp $sourcedrive;
$sourcedrive =~ qr{^[A-Z]:$} or die "$sourcedrive is not a valid Drive\n";

print "\n**********Starting USB Zip - Let's GO!**********\n";

my $destpath = $sourcepath . $P00 . "\\";
print "\n$package: Destination path is $destpath\n";
#-d $destpath or die "$destpath does not exist\n";

print "\n**********Forming the file names**********\n";
# Form the pdf file name
my $pdf_file = $sourcepath . 'PDF\\' . $P00 . '.pdf';
print "$package: Source PDF file is $pdf_file\n";
# Form the zip file name
my $target_zip = $destpath . $P00 . '.zip';
print "$package: Target zip file is $target_zip\n";
# Form the list file name
my $list = $destpath . $P00 . '.lst';
print "$package: List file is $list\n";
# Form the target PDF file
my $target_pdf = $destpath . $P00 . '.pdf';
print "$package: Target PDF file is $target_pdf\n";
# Form the target MD5 file
my $md5digest = $destpath . $P00 . '.md5';
print "$package: Target MD5 file is $md5digest\n";

# If the target directory is there
if (-e $destpath) {
# If the target zip file is there
if (-e $target_zip) {
# Remove it
unlink $target_zip or die "$package: Could not remove existing $target_zip, Error: $!\n";
print "\n**********Cleaning up files**********\n";
print "$package: Removed Existing ZIP file $target_zip\n";
}
}
else { # If the target directory is not there then make it
print "\n**********Making directory**********\n";
mkdir $destpath or die "$package: Could not make $destpath, Error: $!\n";
print "$package: Made Directory $destpath\n";
}

#Read the source directory
our $DIR = undef;
opendir($DIR,$sourcedrive) or die "Could not open $sourcedrive, Error :$!\n";
our @dirlist = sort readdir $DIR;
close $DIR;
undef $DIR;
@dirlist or die "No Directories found in $destpath\n";

# If the target pdf file is there
if (-e $target_pdf) {
print "\n**********Cleaning up**********\n";
print "$package: $target_pdf already exists - removing it\n";
# Remove it
unlink $target_pdf or die "$package: Could not remove existing $target_pdf, Error: $!\n";
# Copy the PDF file from the PDF directory
-e $pdf_file or die "$package: No PDF File found $pdf_file\n";
copy("$pdf_file","$destpath");
print "$package: Copied $pdf_file to $destpath\n";
}
else {
# Copy the PDF file from the PDF directory
print "\n**********Copying**********\n";
print "$package: Copying $pdf_file to $target_pdf\n";
-e $pdf_file or die "$package: No PDF File $pdf_file\n";
copy("$pdf_file","$destpath");
print "$package: Copied $pdf_file to $destpath\n";
}

print "\n**********Zipping $sourcedrive**********\n";
my $zipCmd = "zip -r $target_zip $sourcedrive";
#print "DEBUG: " . $zipCmd . "\n";
system($zipCmd);

#Issue with 7zip, it doesn't like duplicate file names, apparently a feature...
#system '"C:\\Program Files\\7-Zip\\7z.exe" a -tzip target.zip @listing.txt"';\
#move("target.zip","$P00.zip");
#move("$P00.zip","$destpath");
#print "$package: Copied $P00.zip to $destpath\n";

my $drivelisting = `dir /B /S /ONG $sourcedrive`;
#print "DEBUG: " . $drivelisting . "\n";
#system($drivelisting);

#Make source drive file and folder listing
my $ZLIST = undef;
open($ZLIST,'>',$list) or die "$package: Could not open $list for write, Error: $!\n";
print $ZLIST "#List of files inside $P00 zip file\n"; # Add some header bumpf
print $ZLIST "#\n";
print $ZLIST "$drivelisting\n";
#foreach my $f (@dirlist) { # Write each file to the list file
# print $ZLIST "$f\n";
#}
close $ZLIST;

print "\n**********Writing list and MD5 files**********\n";
print "$package: Wrote List File $list\n";

#Small Windows MD5 command line program Author: Jem Berkes
#my $md5command = `md5sums -b -e $destpath`;

#my $md5hash = undef;
#open($md5hash,'>',$md5digest) or die "$package: Could not open $md5hash or write, Error: $!\n"; # Make an MD5 file
#print $md5hash "#MD5 hash listing of files for hard drive $P00\n";
#print $md5hash "$md5command\n";
#close $md5hash;
#print "$package: Wrote MD5 File $md5digest\n";

#Making the MD5 hashes and the MD5 file
my $md5zip = ($target_zip);
my $md5list = ($list);
my $md5pdf = ($target_pdf);
my $md5file1 = Digest::MD5->new;
my $md5file2 = Digest::MD5->new;
my $md5file3 = Digest::MD5->new;
my $md5all = Digest::MD5->new;
$md5file1->add($md5zip);
$md5file2->add($md5list);
$md5file3->add($md5pdf);
$md5all->add($md5zip,$md5list,$md5pdf);
my $digest1 = $md5file1->hexdigest;
my $digest2 = $md5file2->hexdigest;
my $digest3 = $md5file3->hexdigest;
my $digest4 = $md5all->hexdigest;
print "Zip file hash is $digest1\n";
print "List file hash is $digest2\n";
print "PDF file hash is $digest3\n";
print "Hash for all files is $digest4\n";

my $md5hash = undef;
open($md5hash,'>',$md5digest) or die "$package: Could not open $md5hash or write, Error: $!\n"; # Make an MD5 file
print $md5hash "#MD5 hash listing of files for hard drive $P00\n";
print $md5hash "$P00.zip $digest1\n";
print $md5hash "$P00.lst $digest2\n";
print $md5hash "$P00.pdf $digest3\n";
print $md5hash "$digest4\n";
close $md5hash;
print "$package: Wrote MD5 File $md5digest\n";

# Copying the folder to the destination folder
our $endpath = $finalpath . $P00 . "\\";
print "\n**********Moving things around**********\n";
print "Copying $destpath to $endpath\n";
my $robocopyCmd = "robocopy.exe $destpath $endpath *.* /V /TEE /S /E /NP /COPY:DAT /R:60 /W:60 /LOG:$P00.log\n";
#print "DEBUG: " . $robocopyCmd . "\n";
system($robocopyCmd);
print "\n$package: Copied $destpath to $endpath\n";

# Removing old files and directory
print "\n**********Cleaning up old directory**********\n";
unlink $pdf_file or die "$package: Could not remove existing $pdf_file, Error: $!\n";
print "Removed $pdf_file\n";
unlink $target_pdf or die "$package: Could not remove existing $target_pdf, Error: $!\n";
print "Removed $target_pdf\n";
unlink $target_zip or die "$package: Could not remove existing $target_zip, Error: $!\n";
print "Removed $target_zip\n";
unlink $md5digest or die "$package: Could not remove existing $md5digest, Error: $!\n";
print "Removed $md5digest\n";
unlink $list or die "$package: Could not remove existing $list, Error: $!\n";
print "Removed $list\n";
rmdir $destpath or die "$package: Could not remove existing $destpath, Error: $!\n";
print "Removed $destpath\n";

print "$package: Archiving of the USB drive $sourcedrive to $endpath completed successfully\n";
exit 0;


Command prompt output

c:\Temp\hddzip>perl usbzip.pl P12345678 E:

**********Starting USB Zip - Let's GO!**********

usbzip: Destination path is C:\Temp\hddzip\P12345678\

**********Forming the file names**********
usbzip: Source PDF file is C:\Temp\hddzip\PDF\P12345678.pdf
usbzip: Target zip file is C:\Temp\hddzip\P12345678\P12345678.zip
usbzip: List file is C:\Temp\hddzip\P12345678\P12345678.lst
usbzip: Target PDF file is C:\Temp\hddzip\P12345678\P12345678.pdf
usbzip: Target MD5 file is C:\Temp\hddzip\P12345678\P12345678.md5

**********Copying**********
usbzip: Copying C:\Temp\hddzip\PDF\P12345678.pdf to C:\Temp\hddzip\P12345678\P12345678.pdf
usbzip: Copied C:\Temp\hddzip\PDF\P12345678.pdf to C:\Temp\hddzip\P12345678\

**********Zipping E:**********
adding: HPSF_Rep.txt (stored 0%)
adding: Hewlett-Packard/ (stored 0%)
adding: Hewlett-Packard/SystemDiags/ (stored 0%)
adding: Hewlett-Packard/SystemDiags/SystemDiags.efi (deflated 80%)
adding: Hewlett-Packard/SystemDiags/SystemDiags.sig (stored 0%)
adding: Hewlett-Packard/SystemDiags/SystemDiags.s09 (stored 0%)
adding: Hewlett-Packard/SystemDiags/CryptRSA.efi (deflated 60%)
adding: Hewlett-Packard/SystemDiags/SystemDiags32.efi (deflated 81%)
adding: Hewlett-Packard/SystemDiags/SystemDiags32.sig (stored 0%)
adding: Hewlett-Packard/SystemDiags/SystemDiags32.s09 (stored 0%)
adding: Hewlett-Packard/SystemDiags/CryptRSA32.efi (deflated 50%)
adding: Hewlett-Packard/SystemDiags/VideoMem.udm (deflated 78%)
adding: Hewlett-Packard/SystemDiags/VideoMem32.udm (deflated 76%)

**********Writing list and MD5 files**********
usbzip: Wrote List File C:\Temp\hddzip\P12345678\P12345678.lst
Zip file hash is 41acb32b14fc62c92508c3662357adf9
List file hash is a5dfcbe09714ff9d19b0a403d438349f
PDF file hash is acb85a864c80a540d9b18ecd6bd44cd0
Hash for all files is 61891e45862d0c6ffc6ee4d5da85fb61
usbzip: Wrote MD5 File C:\Temp\hddzip\P12345678\P12345678.md5

**********Moving things around**********
Copying C:\Temp\hddzip\P12345678\ to C:\Temp\harddrives\P12345678\

Log File : c:\Temp\hddzip\P12345678.log

-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------

Started : Sun Sep 29 23:35:31 2013

Source : C:\Temp\hddzip\P12345678\
Dest : C:\Temp\harddrives\P12345678\

Files : *.*

Options : *.* /V /TEE /S /E /COPY:DAT /NP /R:60 /W:60

------------------------------------------------------------------------------

4 C:\Temp\hddzip\P12345678\
*EXTRA File 333 FCMD5-sums.MD5
same 72005 P12345678.pdf
Newer 625 P12345678.lst
Newer 231 P12345678.md5
Newer 969222 P12345678.zip

------------------------------------------------------------------------------

Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 0 1 0 0 0
Files : 4 3 1 0 0 1
Bytes : 1017.6 k 947.3 k 70.3 k 0 0 333
Times : 0:00:00 0:00:00 0:00:00 0:00:00


Speed : 64671866 Bytes/sec.
Speed : 3700.553 MegaBytes/min.

Ended : Sun Sep 29 23:35:31 2013

usbzip: Copied C:\Temp\hddzip\P12345678\ to C:\Temp\harddrives\P12345678\

**********Cleaning up old directory**********
Removed C:\Temp\hddzip\PDF\P12345678.pdf
Removed C:\Temp\hddzip\P12345678\P12345678.pdf
Removed C:\Temp\hddzip\P12345678\P12345678.zip
Removed C:\Temp\hddzip\P12345678\P12345678.md5
Removed C:\Temp\hddzip\P12345678\P12345678.lst
Removed C:\Temp\hddzip\P12345678\
usbzip: Archiving of the USB drive E: to C:\Temp\harddrives\P12345678\ completed successfully


RockE70
Novice

Oct 18, 2013, 4:14 AM

Post #6 of 6 (10985 views)
Re: [RockE70] Win32 binmode MD5 hash problem [In reply to] Can't Post

Eventual solution for the MD5 creation


Code
#Making the MD5 hashes and the MD5 file 
my @files = ();
push @files,$target_zip;
push @files,$list;
push @files,$target_pdf;
my @all=();
foreach my $f (@files) {
my $md5file = Digest::MD5->new;
my $FH1=undef;
if (not sysopen($FH1, $f, O_BINARY)) {
print "Could not open file $f, Error=$!\n";
exit 0;
}
$md5file->addfile($FH1);
my $digest1 = $md5file->hexdigest;
push @all, "$digest1,$f";
print "$f hash is $digest1\n";
}

my $md5hash = undef;
open($md5hash,'>',$md5digest) or die "$package: Could not open $md5hash or write, Error: $!\n"; # Make an MD5 file
print $md5hash "#MD5 hash listing of files for hard drive $P00\n";
my $md5all = Digest::MD5->new;
foreach my $l (@all) {
print $md5hash "$l\n";
$md5all->add($l);
}

my $alldigest=$md5all->hexdigest;
print $md5hash "CHECKSUM: $alldigest\n";
close $md5hash;
print "$package: Wrote MD5 File $md5digest\n";

# Copying the folder to the destination folder
print "\n**********Moving things around**********\n";
our $endpath = $finalpath . $P00 . "\\";
print "Copying $destpath to $endpath\n";
my $robocopyCmd = "robocopy.exe $destpath $endpath *.* /V /TEE /S /E /NP /COPY:DAT /R:60 /W:60 /LOG:$P00.log\n";
#print "DEBUG: " . $robocopyCmd . "\n";
system($robocopyCmd);
print "\n$package: Copied $destpath to $endpath\n";

#Comparing Source and Target directory zip file MD5 hashes
print "\n**********Checking source and target MD5 hashes**********\n";
my $md5dest = ($destpath . $P00 . '.zip');
my $md5end = ($endpath . $P00 . '.zip');

#Create MD5 hash of Source Zip
my $md5filedest = Digest::MD5->new;
my $FHDest=undef;
if (not sysopen($FHDest, $md5dest, O_BINARY)) {
print "Could not open file $md5dest, Error=$!\n";
exit 0;
}
$md5filedest->addfile($FHDest);
my $digestdest = $md5filedest->hexdigest;
print "$md5dest hash is $digestdest\n";

#Create MD5 hash of Target Zip
my $md5filend = Digest::MD5->new;
my $FHEnd=undef;
if (not sysopen($FHEnd, $md5end, O_BINARY)) {
print "Could not open file $md5end, Error=$!\n";
exit 0;
}
$md5filend->addfile($FHEnd);
my $digestend = $md5filend->hexdigest;
print "$md5end hash is $digestend\n";

#Compare zip file MD5 hashes
if (not $digestdest eq $digestend) {
print "MD5 Checksum for $md5dest and $md5end do not match!\n";
exit 0;
}
else {
print "MD5 hashes match - continuing\n";
}


 
 


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

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