4 use Digest::MD5 qw(md5 md5_hex md5_base64);
6 # To update the EBCDIC section even on a Latin 1 platform,
7 # run this script with $ENV{EBCDIC_MD5SUM} set to a true value.
8 # (You'll need to have Perl 5.7.3 or later, to have the Encode installed.)
9 # (And remember that under the Perl core distribution you should
10 # also have the $ENV{PERL_CORE} set to a true value.)
11 # Similarly, to update MacOS section, run with $ENV{MAC_MD5SUM} set.
14 if (ord "A" == 193) { # EBCDIC
16 11e8028ee426273db6b6db270a8bb38c README
17 6e556382813f67120863f4f91b7fcdc2 MD5.xs
18 276da0aa4e9a08b7fe09430c9c5690aa rfc1321.txt
20 } elsif ("\n" eq "\015") { # MacOS
22 c95549c6c5e1e1c078b27042f1dc850f README
23 7aa380c810bc7c1a0bec22cf32bc50d4 MD5.xs
24 754b9db19f79dbc4992f7166eb0f37ce rfc1321.txt
27 # This is the output of: 'md5sum README MD5.xs rfc1321.txt'
29 c95549c6c5e1e1c078b27042f1dc850f README
30 7aa380c810bc7c1a0bec22cf32bc50d4 MD5.xs
31 754b9db19f79dbc4992f7166eb0f37ce rfc1321.txt
35 if (!(-f "README") && -f "../README") {
36 chdir("..") or die "Can't chdir: $!";
42 eval { require MIME::Base64; };
44 print "# $@: Will not test base64 methods\n";
48 for (split /^/, $EXPECT) {
49 my($md5hex, $file) = split ' ';
52 if ($ENV{PERL_CORE}) {
53 if ($file eq 'rfc1321.txt') { # Don't have it in core.
54 print "ok ", ++$testno, " # Skip: PERL_CORE\n";
58 # print "# file = $file\n";
60 warn "No such file: $file\n";
63 if ($ENV{EBCDIC_MD5SUM}) {
65 my $data = cat_file($file);
66 Encode::from_to($data, 'latin1', 'cp1047');
67 print md5_hex($data), " $base\n";
70 if ($ENV{MAC_MD5SUM}) {
72 my $data = cat_file($file);
73 Encode::from_to($data, 'latin1', 'MacRoman');
74 print md5_hex($data), " $base\n";
77 my $md5bin = pack("H*", $md5hex);
80 $md5b64 = MIME::Base64::encode($md5bin, "");
81 chop($md5b64); chop($md5b64); # remove padding
86 if (digest_file($file, 'digest') ne $md5bin) {
87 print "$file: Bad digest\n";
91 if (($got = digest_file($file, 'hexdigest')) ne $md5hex) {
92 print "$file: Bad hexdigest: got $got expected $md5hex\n";
96 if ($B64 && digest_file($file, 'b64digest') ne $md5b64) {
97 print "$file: Bad b64digest\n";
101 my $data = cat_file($file);
102 if (md5($data) ne $md5bin) {
103 print "$file: md5() failed\n";
106 if (md5_hex($data) ne $md5hex) {
107 print "$file: md5_hex() failed\n";
110 if ($B64 && md5_base64($data) ne $md5b64) {
111 print "$file: md5_base64() failed\n";
115 if (Digest::MD5->new->add($data)->digest ne $md5bin) {
116 print "$file: MD5->new->add(...)->digest failed\n";
119 if (Digest::MD5->new->add($data)->hexdigest ne $md5hex) {
120 print "$file: MD5->new->add(...)->hexdigest failed\n";
123 if ($B64 && Digest::MD5->new->add($data)->b64digest ne $md5b64) {
124 print "$file: MD5->new->add(...)->b64digest failed\n";
128 my @data = split //, $data;
129 if (md5(@data) ne $md5bin) {
130 print "$file: md5(\@data) failed\n";
133 if (Digest::MD5->new->add(@data)->digest ne $md5bin) {
134 print "$file: MD5->new->add(\@data)->digest failed\n";
137 my $md5 = Digest::MD5->new;
141 if ($md5->digest ne $md5bin) {
142 print "$file: $md5->add()-loop failed\n";
146 print "not " if $failed;
147 print "ok ", ++$testno, "\n";
153 my($file, $method) = @_;
154 $method ||= "digest";
155 #print "$file $method\n";
157 open(FILE, $file) or die "Can't open $file: $!";
158 my $digest = Digest::MD5->new->addfile(*FILE)->$method();
168 open(FILE, $file) or die "Can't open $file: $!";
170 # For PerlIO in case of UTF-8 locales.
171 eval 'binmode(FILE, ":bytes")' if $] >= 5.008;