
RockE70
Novice
Sep 29, 2013, 6:42 AM
Post #5 of 6
(24812 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) #!/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
|