Check MD5 md5sum for pl

出自sebug security vulnerability(SSV) DB
跳转到: 导航, 搜索

两个使用了不同的函数,在执行效率上checkmd5sum.pl 比checkmd5sum_find.pl要更快

  1. #!/usr/bin/perl   
  2. #   
  3. # File: checkmd5sum.pl   
  4. # Author: luoq.net   
  5. # Date: 2008-12-02   
  6. # License: GPL-2   
  7.  
  8. use Digest::MD5;   
  9. use IO::File;   
  10. use POSIX qw(strftime);   
  11.  
  12. if( @ARGV != 1 ){   
  13.     print "check files md5sum\nusage:<rootdir>\nexample:c:\\n";   
  14.     exit;   
  15. }   
  16.  
  17. $now_time=strftime "%Y-%m-%d %H:%M:%S", localtime;   
  18. $md5sumfile_name=strftime "%Y_%m_%d_%H_%M_%S", localtime;   
  19.  
  20. open MD5FILE,">$md5sumfile_name.txt" or die "$!\n\n";   
  21. print MD5FILE "# check files md5sum\n\n";   
  22. print MD5FILE "# Created on $now_time\n";   
  23. print MD5FILE "# amxku_at_msn.com\n\n";   
  24. my $root = $ARGV[0];   
  25. sub lsr_s($) {   
  26.     my $cwd = shift;   
  27.     my @dirs = ($cwd.'/');   
  28.  
  29.     my ($dir, $file);   
  30.     while ($dir = pop(@dirs)) {   
  31.         local *DH;   
  32.         if (!opendir(DH, $dir)) {   
  33.             warn "Cannot opendir $dir: $! $^E";   
  34.             next;   
  35.         }   
  36.         foreach (readdir(DH)) {   
  37.             if ($_ eq '.' || $_ eq '..') {   
  38.                 next;   
  39.             }   
  40.             $file = $dir.$_;            
  41.             if (!-l $file && -d _) {   
  42.                 $file .= '/';   
  43.                 push(@dirs, $file);   
  44.             }   
  45.             process($file, $dir);   
  46.         }   
  47.         closedir(DH);   
  48.     }   
  49. }   
  50.  
  51. my ($size, $dircnt, $filecnt) = (0, 0, 0);   
  52.  
  53. sub process($$) {   
  54.     my $file = shift;   
  55.     print $file, "\n";   
  56.     if (substr($file, length($file)-1, 1) eq '/') {   
  57.         $dircnt++;   
  58.     }   
  59.     else {   
  60.         $filecnt++;   
  61.         $size += -s $file;   
  62.         my $fileinfo = genchksuminfo($file);   
  63.         print MD5FILE "$fileinfo\n";   
  64.     }   
  65. }   
  66.  
  67. sub genchksuminfo   
  68. {   
  69.     my ($file) = @_;   
  70.     my $chk = Digest::MD5->new();   
  71.     my (@statinfo) = stat($file);   
  72.     $chk->add(@statinfo[0,1,2,3,4,5,7,9,10]);   
  73.     $chk->addfile(IO::File->new($file));   
  74.     return sprintf("md5:%s\t%s",$chk->hexdigest,$file);   
  75. }   
  76.  
  77. lsr_s("$root");   
  78. print "\n$filecnt files, $dircnt directory. $size bytes.\n";   
  79. my $dateinfo ="\n$filecnt files, $dircnt directory. $size bytes.\n";   
  80. print MD5FILE "$dateinfo\n";   
  81. close(MD5FILE);


  1. #!/usr/bin/perl   
  2. #   
  3. # File: checkmd5sum_find.pl  
  4. # Author: p0150 & amxku   
  5. # License: GPL-2   
  6.  
  7. use File::Find;   
  8. use Digest::MD5;   
  9. use IO::File;   
  10. use POSIX qw(strftime);   
  11.  
  12. if( @ARGV != 1 ){   
  13.     print "usage:   <rootdir>  \n";   
  14.     print "example:   c:\     \n";   
  15.      exit;   
  16. }   
  17. $now_time=strftime "%Y-%m-%d %H:%M:%S", localtime;   
  18. $now_string=strftime "%Y_%m_%d_%H_%M_%S", localtime;   
  19.  
  20. open MD5FILE,">$now_string.txt" or die "$!\n";   
  21. print MD5FILE "#\n";   
  22. print MD5FILE "# Created on $now_time\n";   
  23. print MD5FILE "#\n";   
  24. print MD5FILE "\n";   
  25. chomp(my $dir=$ARGV[0]);   
  26. find(\&wanted, $dir);          
  27. sub wanted    
  28. {   
  29. unless (-d $_)   
  30. {   
  31. print $File::Find::dir.'/'.$_."\n";   
  32. my $name=$File::Find::dir.'/'.$_;   
  33. my $fileinfo = genchksuminfo($name);   
  34. print MD5FILE "$fileinfo\n";   
  35. }   
  36. }   
  37.  
  38. sub genchksuminfo   
  39. {   
  40.     my ($file) = @_;   
  41.     my $chk = Digest::MD5->new();   
  42.     my (@statinfo) = stat($file);   
  43.     $chk->add(@statinfo[0,1,2,3,4,5,7,9,10]);   
  44.     $chk->addfile(IO::File->new($file));   
  45.     return sprintf("md5:%s\t%s",$chk->hexdigest,$file);   
  46. }   
  47. close(MD5FILE);
个人工具
名字空间
变换
导航
工具箱