Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 7.51 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl5.10.0
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.         if $running_under_some_shell;
  4. $0 =~ s/\d+\.\d+\.\d$//;
  5. #!perl -w
  6.  
  7.         # shasum: filter for computing SHA digests (analogous to sha1sum)
  8.         #
  9.         # Copyright (C) 2003-2007 Mark Shelor, All Rights Reserved
  10.         #
  11.         # Version: 5.45
  12.         # Tue Jun 26 02:36:00 MST 2007
  13.  
  14. =head1 NAME
  15.  
  16. shasum - Print or Check SHA Checksums
  17.  
  18. =head1 SYNOPSIS
  19.  
  20.  Usage: shasum [OPTION] [FILE]...
  21.     or: shasum [OPTION] --check [FILE]
  22.  Print or check SHA checksums.
  23.  With no FILE, or when FILE is -, read standard input.
  24.  
  25.   -a, --algorithm    1 (default), 224, 256, 384, 512
  26.   -b, --binary       read files in binary mode (default on DOS/Windows)
  27.   -c, --check        check SHA sums against given list
  28.   -p, --portable     read files in portable mode
  29.                          produces same digest on Windows/Unix/Mac
  30.   -t, --text         read files in text mode (default)
  31.  
  32.  The following two options are useful only when verifying checksums:
  33.  
  34.   -s, --status       don't output anything, status code shows success
  35.   -w, --warn         warn about improperly formatted SHA checksum lines
  36.  
  37.   -h, --help         display this help and exit
  38.   -v, --version      output version information and exit
  39.  
  40.  The sums are computed as described in FIPS PUB 180-2.  When checking,
  41.  the input should be a former output of this program.  The default mode
  42.  is to print a line with checksum, a character indicating type (`*'
  43.  for binary, `?' for portable, ` ' for text), and name for each FILE.
  44.  
  45. =head1 DESCRIPTION
  46.  
  47. The I<shasum> script provides the easiest and most convenient way to
  48. compute SHA message digests.  Rather than writing a program, the user
  49. simply feeds data to the script via the command line, and waits for
  50. the results to be printed on standard output.  Data can be fed to
  51. I<shasum> through files, standard input, or both.
  52.  
  53. The following command shows how easy it is to compute digests for typical
  54. inputs such as the NIST test vector "abc":
  55.  
  56.         perl -e "print qw(abc)" | shasum
  57.  
  58. Or, if you want to use SHA-256 instead of the default SHA-1, simply say:
  59.  
  60.         perl -e "print qw(abc)" | shasum -a 256
  61.  
  62. Since I<shasum> uses the same interface employed by the familiar
  63. I<sha1sum> program (and its somewhat outmoded anscestor I<md5sum>),
  64. you can install this script as a convenient drop-in replacement.
  65.  
  66. =head1 AUTHOR
  67.  
  68. Copyright (c) 2003-2007 Mark Shelor <mshelor@cpan.org>.
  69.  
  70. =head1 SEE ALSO
  71.  
  72. shasum is implemented using the Perl module L<Digest::SHA> or
  73. L<Digest::SHA::PurePerl>.
  74.  
  75. =cut
  76.  
  77. use strict;
  78. use FileHandle;
  79. use Getopt::Long;
  80.  
  81. my $VERSION = "5.45";
  82.  
  83.  
  84.         # Try to use Digest::SHA, since it's faster.  If not installed,
  85.         # use Digest::SHA::PurePerl instead.
  86.  
  87. my $MOD_PREFER = "Digest::SHA";
  88. my $MOD_SECOND = "Digest::SHA::PurePerl";
  89.  
  90. my $module = $MOD_PREFER;
  91. eval "require $module";
  92. if ($@) {
  93.         $module = $MOD_SECOND;
  94.         eval "require $module";
  95.         die "Unable to find $MOD_PREFER or $MOD_SECOND\n" if $@;
  96. }
  97.  
  98.  
  99.         # Usage statement adapted from Ulrich Drepper's md5sum.
  100.         # Include an "-a" option for algorithm selection,
  101.         # and a "-p" option for portable digest computation.
  102.  
  103. sub usage {
  104.         my($err, $msg) = @_;
  105.  
  106.         $msg = "" unless defined $msg;
  107.         if ($err) {
  108.                 warn($msg . "Type shasum -h for help\n");
  109.                 exit($err);
  110.         }
  111.         print <<'END_OF_USAGE';
  112. Usage: shasum [OPTION] [FILE]...
  113.    or: shasum [OPTION] --check [FILE]
  114. Print or check SHA checksums.
  115. With no FILE, or when FILE is -, read standard input.
  116.  
  117.   -a, --algorithm    1 (default), 224, 256, 384, 512
  118.   -b, --binary       read files in binary mode (default on DOS/Windows)
  119.   -c, --check        check SHA sums against given list
  120.   -p, --portable     read files in portable mode
  121.                          produces same digest on Windows/Unix/Mac
  122.   -t, --text         read files in text mode (default)
  123.  
  124. The following two options are useful only when verifying checksums:
  125.   -s, --status       don't output anything, status code shows success
  126.   -w, --warn         warn about improperly formatted SHA checksum lines
  127.  
  128.   -h, --help         display this help and exit
  129.   -v, --version      output version information and exit
  130.  
  131. The sums are computed as described in FIPS PUB 180-2.  When checking, the
  132. input should be a former output of this program.  The default mode is to
  133. print a line with checksum, a character indicating type (`*' for binary,
  134. `?' for portable, ` ' for text), and name for each FILE.
  135.  
  136. Report bugs to <mshelor@cpan.org>.
  137. END_OF_USAGE
  138.         exit($err);
  139. }
  140.  
  141.  
  142.         # Collect options from command line
  143.  
  144. my ($alg, $binary, $check, $text, $status, $warn, $help, $version);
  145. my ($portable);
  146.  
  147. eval { Getopt::Long::Configure ("bundling") };
  148. GetOptions(
  149.         'b|binary' => \$binary, 'c|check' => \$check,
  150.         't|text' => \$text, 'a|algorithm=i' => \$alg,
  151.         's|status' => \$status, 'w|warn' => \$warn,
  152.         'h|help' => \$help, 'v|version' => \$version,
  153.         'p|portable' => \$portable
  154. ) or usage(1, "");
  155.  
  156.  
  157.         # Deal with help requests and incorrect uses
  158.  
  159. usage(0)
  160.         if $help;
  161. usage(1, "shasum: Ambiguous file mode\n")
  162.         if scalar(grep { defined $_ } ($binary, $portable, $text)) > 1;
  163. usage(1, "shasum: --warn option used only when verifying checksums\n")
  164.         if $warn && !$check;
  165. usage(1, "shasum: --status option used only when verifying checksums\n")
  166.         if $status && !$check;
  167.  
  168.  
  169.         # Default to SHA-1 unless overriden by command line option
  170.  
  171. $alg = 1 unless $alg;
  172. grep { $_ == $alg } (1, 224, 256, 384, 512)
  173.         or usage(1, "shasum: Unrecognized algorithm\n");
  174.  
  175.  
  176.         # Display version information if requested
  177.  
  178. if ($version) {
  179.         print "$VERSION\n";
  180.         exit(0);
  181. }
  182.  
  183.  
  184.         # Try to figure out if the OS is DOS-like.  If it is,
  185.         # default to binary mode when reading files, unless
  186.         # explicitly overriden by command line "--text" or
  187.         # "--portable" options.
  188.  
  189. my $isDOSish = ($^O =~ /^(MSWin\d\d|os2|dos|mint|cygwin)$/);
  190. if ($isDOSish) { $binary = 1 unless $text || $portable }
  191.  
  192. my $modesym = $binary ? '*' : ($portable ? '?' : ' ');
  193.  
  194.  
  195.         # Read from STDIN (-) if no files listed on command line
  196.  
  197. @ARGV = ("-") unless @ARGV;
  198.  
  199.  
  200.         # sumfile($file): computes SHA digest of $file
  201.  
  202. sub sumfile {
  203.         my $file = shift;
  204.  
  205.         my $mode = $portable ? 'p' : ($binary ? 'b' : '');
  206.         my $digest = eval { $module->new($alg)->addfile($file, $mode) };
  207.         if ($@) {
  208.                 warn "shasum: $file: $!\n";
  209.                 return;
  210.         }
  211.  
  212.         $digest->hexdigest;
  213. }
  214.  
  215.  
  216.         # %len2alg: maps hex digest length to SHA algorithm
  217.  
  218. my %len2alg = (40 => 1, 56 => 224, 64 => 256, 96 => 384, 128 => 512);
  219.  
  220.  
  221.         # Verify checksums if requested
  222.  
  223. if ($check) {
  224.         my $checkfile = shift(@ARGV);
  225.         my ($err, $read_errs, $match_errs) = (0, 0, 0);
  226.         my ($num_files, $num_checksums) = (0, 0);
  227.         my ($fh, $sum, $fname, $rsp, $digest);
  228.  
  229.         die "shasum: $checkfile: $!\n"
  230.                 unless $fh = FileHandle->new($checkfile, "r");
  231.         while (<$fh>) {
  232.                 s/\s+$//;
  233.                 ($sum, $modesym, $fname) = /^(\S+) (.)(.*)$/;
  234.                 ($binary, $portable, $text) =
  235.                         map { $_ eq $modesym } ('*', '?', ' ');
  236.                 unless ($alg = $len2alg{length($sum)}) {
  237.                         warn("shasum: $checkfile: $.: improperly " .
  238.                                 "formatted SHA checksum line\n") if $warn;
  239.                         next;
  240.                 }
  241.                 $rsp = "$fname: "; $num_files++;
  242.                 unless ($digest = sumfile($fname)) {
  243.                         $rsp .= "FAILED open or read\n";
  244.                         $err = 1; $read_errs++;
  245.                 }
  246.                 else {
  247.                         $num_checksums++;
  248.                         if (lc($sum) eq $digest) { $rsp .= "OK\n" }
  249.                         else { $rsp .= "FAILED\n"; $err = 1; $match_errs++ }
  250.                 }
  251.                 print $rsp unless $status;
  252.         }
  253.         $fh->close;
  254.         unless ($status) {
  255.                 warn("shasum: WARNING: $read_errs of $num_files listed " .
  256.                         "files could not be read\n") if $read_errs;
  257.                 warn("shasum: WARNING: $match_errs of $num_checksums " .
  258.                         "computed checksums did NOT match\n") if $match_errs;
  259.         }
  260.         exit($err);
  261. }
  262.  
  263.  
  264.         # Compute and display SHA checksums of requested files
  265.  
  266. my($file, $digest);
  267.  
  268. for $file (@ARGV) {
  269.         if ($digest = sumfile($file)) {
  270.                 print "$digest $modesym", "$file\n";
  271.         }
  272. }