Debian Codegraph @

tools/md5

raw
1 #! /usr/local/bin/perl -w
2 #
3 # $Id$
4 #
5 use strict;
6 my $PP = 0;
7 eval { require Digest::MD5; };
8 require Digest::Perl::MD5, $PP = 1 if $@;
9 my $ctx = $PP ? Digest::Perl::MD5->new() : Digest::MD5->new();
10
11 my $TEST_BLOCK_LEN = 10_000;
12 my $TEST_BLOCK_COUNT = 100_000;
13
14 my ($r,$q,$p) = (0)x3;
15
16 # reading options:
17 while (my $input = shift) {
18 unless ($input =~ s/^-//) { unshift @ARGV, $input; last; }
19 for my $opt (split //, $input) {
20 if ($opt eq 'p') {
21 $p = 1;
22 } elsif ($opt eq 'q') {
23 $q = 1;
24 } elsif ($opt eq 'r') {
25 $r = 1;
26 } elsif ($opt eq 't') {
27 local $|=1;
28 print "MD5 time trial. Digesting $TEST_BLOCK_COUNT $TEST_BLOCK_LEN-byte blocks ... ";
29 my $block;
30 for (0..$TEST_BLOCK_LEN-1) { $block .= chr($_ & 0xff) }
31 my $start = time;
32 for (1..$TEST_BLOCK_COUNT) { $ctx->add($block) }
33 my $stop = time;
34 my $diff = $stop - $start; $diff = 1 if $diff <= 0;
35 print "done\n";
36 print "Digest = ", $ctx->hexdigest(), "\n";
37 print "Time = $diff seconds\n";
38 print "Speed = ", int $TEST_BLOCK_LEN*$TEST_BLOCK_COUNT / $diff, " bytes/second\n";
39 exit;
40 } elsif ($opt eq 'x') {
41 print "MD5 test suite:\n";
42 for (
43 "",
44 "a",
45 "abc",
46 "message digest",
47 "abcdefghijklmnopqrstuvwxyz",
48 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
49 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
50 ) {
51 md5print(qq'"$_"', $ctx->add($_)->hexdigest());
52 }
53 exit;
54 } elsif ($opt eq 's') {
55 unless (@ARGV) {
56 die "md5: option requires an argument -- s\n", usage();
57 }
58 my $text = shift;
59 md5print(qq'"$text"', $ctx->add($text)->hexdigest());
60 } else {
61 die "md5: illegal option -- $opt\n", usage();
62 }
63 }
64 }
65
66
67
68 # files
69 my $file;
70 @ARGV = '-' unless @ARGV;
71 foreach $file (@ARGV) {
72 my $fh;
73 if ($file eq '-') {
74 $fh = \*STDIN;
75 $q = 1;
76 } else {
77 open $fh, $file or die "md5: $file: $!\n";
78 }
79 if ($p) {
80 my ($len,$buf) = (0,'');
81 while ( $len = read STDIN, $buf, 8192 ) {
82 print $buf;
83 $ctx->add($buf);
84 }
85 die "read failed: $!\n" unless defined $buf;
86 md5print('',$ctx->hexdigest());
87 } else {
88 md5print($file, $ctx->addfile($fh)->hexdigest());
89 }
90 }
91
92 sub usage { "usage: md5 [-pqrtx] [-s string] [files ...]\n" }
93 sub md5print {
94 my ($file, $md5) = @_;
95 if ($q) { print "$md5\n"; return }
96 print $r ? "$md5 $file" : "MD5 ($file) = $md5", "\n";
97 }