1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#!perl -w
# shasum: filter for computing SHA digests (analogous to md5sum)
#
# Copyright (C) 2003-2005 Mark Shelor, All Rights Reserved
#
# Version: 5.32
# Fri Dec 2 02:32:20 MST 2005
=head1 NAME
shasum - Print or Check SHA Checksums
=head1 SYNOPSIS
Usage: shasum [OPTION] [FILE]...
or: shasum [OPTION] --check [FILE]
Print or check SHA checksums.
With no FILE, or when FILE is -, read standard input.
-a, --algorithm 1 (default), 224, 256, 384, 512
-b, --binary read files in binary mode (default on DOS/Windows)
-c, --check check SHA sums against given list
-t, --text read files in text mode (default)
The following two options are useful only when verifying checksums:
--status don't output anything, status code shows success
-w, --warn warn about improperly formatted SHA checksum lines
--help display this help and exit
--version output version information and exit
The sums are computed as described in FIPS PUB 180-2. When checking,
the input should be a former output of this program. The default
mode is to print a line with checksum, a character indicating type
(`*' for binary, ` ' for text), and name for each FILE.
=head1 AUTHOR
Copyright (c) 2003-2005 Mark Shelor <mshelor@cpan.org>.
=head1 SEE ALSO
Shasum is implemented using the Perl module L<Digest::SHA> or
L<Digest::SHA::PurePerl>.
=cut
use strict;
use Getopt::Long;
my $VERSION = "5.32";
# Try to use Digest::SHA, since it's faster. If not installed,
# use Digest::SHA::PurePerl instead.
my $MOD_PREFER = "Digest::SHA";
my $MOD_SECOND = "Digest::SHA::PurePerl";
my $module = $MOD_PREFER;
eval "require $module";
if ($@) {
$module = $MOD_SECOND;
eval "require $module";
die "Unable to find $MOD_PREFER or $MOD_SECOND\n" if $@;
}
# Usage statement adapted from Ulrich Drepper's md5sum.
# Include an "-a" option for algorithm selection.
sub usage {
my($err) = @_;
my $stream = $err ? *STDERR : *STDOUT;
print $stream <<'END_OF_USAGE';
Usage: shasum [OPTION] [FILE]...
or: shasum [OPTION] --check [FILE]
Print or check SHA checksums.
With no FILE, or when FILE is -, read standard input.
-a, --algorithm 1 (default), 224, 256, 384, 512
-b, --binary read files in binary mode (default on DOS/Windows)
-c, --check check SHA sums against given list
-t, --text read files in text mode (default)
The following two options are useful only when verifying checksums:
--status don't output anything, status code shows success
-w, --warn warn about improperly formatted SHA checksum lines
--help display this help and exit
--version output version information and exit
The sums are computed as described in FIPS PUB 180-2. When checking,
the input should be a former output of this program. The default
mode is to print a line with checksum, a character indicating type
(`*' for binary, ` ' for text), and name for each FILE.
Report bugs to <mshelor@cpan.org>.
END_OF_USAGE
exit($err);
}
# Collect options from command line
my ($alg, $binary, $check, $text, $status, $warn, $help, $version);
GetOptions(
'binary' => \$binary, 'check' => \$check,
'text' => \$text, 'algorithm=i' => \$alg,
'status' => \$status, 'warn' => \$warn,
'help' => \$help, 'version' => \$version
) or usage(1);
# Deal with help requests and incorrect uses
usage(0) if $help;
usage(1) if $binary and $text;
usage(1) if $warn and not $check;
usage(1) if $status and not $check;
# Default to SHA-1 unless overriden by command line option
$alg = 1 unless $alg;
grep { $_ == $alg } (1, 224, 256, 384, 512) or usage(1);
# Display version information if requested
if ($version) {
print "$VERSION\n";
exit(0);
}
# Try to figure out if the OS is DOS-like. If it is,
# default to binary mode when reading files, unless
# explicitly overriden by command line "text" option.
my $isDOSish = ($^O =~ /^(MSWin\d\d|os2|dos|mint|cygwin)$/);
if ($isDOSish) { $binary = 1 unless $text }
# Read from STDIN (-) if no files listed on command line
@ARGV = ("-") unless @ARGV;
# sumfile($file): computes SHA digest of $file
sub sumfile {
my($file) = @_;
my($fh, $digest);
unless (open($fh, "<$file")) {
print STDERR "shasum: $file: No such file or directory\n";
return;
}
binmode($fh) if $binary;
$digest = $module->new($alg)->addfile($fh)->hexdigest;
close($fh);
return($digest);
}
# %len2alg: maps hex digest length to SHA algorithm
my %len2alg = (40 => 1, 56 => 224, 64 => 256, 96 => 384, 128 => 512);
# Verify checksums if requested
if ($check) {
my $checkfile = shift(@ARGV);
my $err = 0;
my ($fh, $sum, $fname, $rsp);
die "shasum: $checkfile: No such file or directory\n"
unless open($fh, "<$checkfile");
while (<$fh>) {
s/\s+$//;
($sum, $binary, $fname) = /^(\S+)\s+(\*?)(.*)$/;
unless ($alg = $len2alg{length($sum)}) {
print STDERR "shasum: $checkfile: $.: improperly ",
"formatted SHA checksum line\n" if $warn;
next;
}
$rsp = "$fname: ";
if (lc($sum) eq sumfile($fname)) { $rsp .= "OK\n" }
else { $rsp .= "FAILED\n"; $err = 1 }
print $rsp unless $status;
}
close($fh);
exit($err);
}
# Compute and display SHA checksums of requested files
for (@ARGV) {
if (-d $_) {
print STDERR "shasum: $_: Is a directory\n";
next;
}
next unless my $digest = sumfile($_);
print "$digest ", $binary ? "\*" : " ", "$_\n";
}
|