下面是递归生成目录或子目录下单个文件的 MD5 / SHA2 和的代码。
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;
use IO::File;
use Digest::MD5;
use Digest::SHA qw(sha256_hex);
find({ wanted => \&process_file, no_chdir => 1 }, @ARGV);
sub process_file {
#my $md5 = Digest::MD5->new;
my $sha2 = Digest::SHA->new(256);
if (-f $_) {
#print "This is a file: $_\n";
open(FILE, $_) or die "Can not open $_";
binmode(FILE);
#my $md5sum = $md5->addfile(*FILE)->hexdigest;
my $sha2sum = $sha2->addfile(*FILE)->hexdigest;
#print sha256_hex(*FILE), " $_\n";
close FILE;
print "$sha2sum $_\n";
}
}
上面代码的输出如下所示。
~$ perl list.pl src
f21e1caa364eaad195d968d28187d5cf1a58c0b7b1f21a8ebcb9ca2539dde175 src/test1.pl
4b3277ec41ba0ff8ed6f9f2593c42e08c2f4e9b66df0d63de7c91559ff7e86fa src/random.py
076231fcbe5887a163278b757f99fb05b27163775ec4706cb2365de3be0906ac src/test.pl
8806c9f58fc91b2e1d6453a7af7e4f9f8b94e2d0f67a84a89b35bfbf517399be src/size.pl
5a1b2080ecc53ced45ed3aa13e47118a9ca2f8505b1e89485b6b681d8e1d264c src/test2.py
5f7c1ff9c7b3dd32f75558dd30324ec085c45a0d0c62190b9a96f211cdf216ea src/java/test3.class
3728ee1a86443fffe9eafd84db82ce68c9640a0a984958f579b0da1a74283d7c src/java/test4.wav
d7169ffbb231e93f47d1c54fddf2144b459bba228de48c30b4bc5a4d297be6fb src/java/test5.java
更新了代码以支持 sha256sum 生成。
现在我想从这些 MD5 / SHA2 和作为输入生成一个组合的 MD5 / SHA2 和。