Revision history for Perl extension Digest::SHA.
+5.36 Mon May 8 01:38:36 MST 2006
+ - fixed the "portable" option in shasum
+ -- normalize line-breaks in text files only
+
+5.35 Thu May 4 16:54:42 MST 2006
+ - added "portable" option to shasum
+ -- to make digests match across Windows/Unix/MacOS
+ - enabled bundling of shasum command line options
+ -- to mimic behavior of md5sum
+ - removed \r's from text files in t/nist directory
+ -- resolves SIGNATURE clashes (rt.cpan.org #18983)
+ - changed suffix on SHA64_MAX (src/sha.h) to ULL
+ -- eliminates gcc warnings (rt.cpan.org #18988)
+ - specified minimum Perl version for module and Makefile.PL
+ -- closes rt.cpan.org #18984
+
5.34 Thu Feb 2 18:55:40 MST 2006
- removed Unix-style pathnames in test scripts
-- causing problems on OpenVMS
+require 5.006000;
+
use ExtUtils::MakeMaker;
use Getopt::Std;
use Config;
-Digest::SHA version 5.34
+Digest::SHA version 5.36
========================
Digest::SHA is a complete implementation of the NIST Secure Hash
use warnings;
use integer;
-our $VERSION = '5.34_01';
+our $VERSION = '5.36_01';
require Exporter;
our @ISA = qw(Exporter);
#
# Copyright (C) 2003-2006 Mark Shelor, All Rights Reserved
#
- # Version: 5.34
- # Thu Feb 2 18:55:40 MST 2006
+ # Version: 5.36
+ # Mon May 8 01:38:36 MST 2006
=head1 NAME
-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
+ -p, --portable read files in portable mode
+ produces same digest on Windows/Unix/MacOS
-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
+ -s, --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
+ -h, --help display this help and exit
+ -v, --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.
+ 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 portable, ` ' for text), and name for each FILE.
=head1 AUTHOR
=head1 SEE ALSO
-Shasum is implemented using the Perl module L<Digest::SHA> or
+shasum is implemented using the Perl module L<Digest::SHA> or
L<Digest::SHA::PurePerl>.
=cut
use strict;
use Getopt::Long;
-my $VERSION = "5.34";
+my $VERSION = "5.36";
# Try to use Digest::SHA, since it's faster. If not installed,
# Usage statement adapted from Ulrich Drepper's md5sum.
- # Include an "-a" option for algorithm selection.
+ # Include an "-a" option for algorithm selection,
+ # and a "-p" option for portable digest computation.
sub usage {
- my($err) = @_;
+ my($err, $msg) = @_;
- my $stream = $err ? *STDERR : *STDOUT;
- print $stream <<'END_OF_USAGE';
+ $msg = "" unless defined $msg;
+ if ($err) {
+ print STDERR "$msg", "Type shasum -h for help\n";
+ exit($err);
+ }
+ print STDOUT <<'END_OF_USAGE';
Usage: shasum [OPTION] [FILE]...
or: shasum [OPTION] --check [FILE]
Print or check SHA checksums.
-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
+ -p, --portable read files in portable mode
+ produces same digest on Windows/Unix/MacOS
-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
+ -s, --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
+ -h, --help display this help and exit
+ -v, --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.
+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 portable, ` ' for text), and name for each FILE.
Report bugs to <mshelor@cpan.org>.
END_OF_USAGE
# Collect options from command line
my ($alg, $binary, $check, $text, $status, $warn, $help, $version);
+my ($portable);
+Getopt::Long::Configure ("bundling");
GetOptions(
- 'binary' => \$binary, 'check' => \$check,
- 'text' => \$text, 'algorithm=i' => \$alg,
- 'status' => \$status, 'warn' => \$warn,
- 'help' => \$help, 'version' => \$version
-) or usage(1);
+ 'b|binary' => \$binary, 'c|check' => \$check,
+ 't|text' => \$text, 'a|algorithm=i' => \$alg,
+ 's|status' => \$status, 'w|warn' => \$warn,
+ 'h|help' => \$help, 'v|version' => \$version,
+ 'p|portable' => \$portable
+) 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;
+usage(0)
+ if $help;
+usage(1, "ambiguous file mode\n")
+ if scalar(grep { defined $_ } ($binary, $portable, $text)) > 1;
+usage(1, "warn option used only when verifying checksums\n")
+ if $warn && !$check;
+usage(1, "status option used only when verifying checksums\n")
+ if $status && !$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);
+grep { $_ == $alg } (1, 224, 256, 384, 512)
+ or usage(1, "unrecognized algorithm\n");
# Display version information if requested
# 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.
+ # explicitly overriden by command line "text" or
+ # "portable" options.
my $isDOSish = ($^O =~ /^(MSWin\d\d|os2|dos|mint|cygwin)$/);
-if ($isDOSish) { $binary = 1 unless $text }
+if ($isDOSish) { $binary = 1 unless $text || $portable }
+
+my $mode = $binary ? '*' : ($portable ? '?' : ' ');
# Read from STDIN (-) if no files listed on command line
print STDERR "shasum: $file: No such file or directory\n";
return;
}
- binmode($fh) if $binary;
- $digest = $module->new($alg)->addfile($fh)->hexdigest;
+ binmode($fh) if $binary || $portable;
+
+ local $/;
+ my $buf = <$fh>;
close($fh);
- return($digest);
+
+ if ($portable && -T $file) {
+ $buf =~ s/\015\012/\012/g;
+ $buf =~ s/\015/\012/g;
+ }
+
+ $digest = $module->new($alg)->add($buf)->hexdigest;
}
unless open($fh, "<$checkfile");
while (<$fh>) {
s/\s+$//;
- ($sum, $binary, $fname) = /^(\S+)\s+(\*?)(.*)$/;
+ ($sum, $mode, $fname) = /^(\S+) (.)(.*)$/;
+ ($binary, $portable, $text) =
+ map { $_ eq $mode } ('*', '?', ' ');
unless ($alg = $len2alg{length($sum)}) {
print STDERR "shasum: $checkfile: $.: improperly ",
"formatted SHA checksum line\n" if $warn;
next;
}
next unless my $digest = sumfile($_);
- print "$digest ", $binary ? "\*" : " ", "$_\n";
+ print "$digest $mode", "$_\n";
}
*
* Copyright (C) 2003-2006 Mark Shelor, All Rights Reserved
*
- * Version: 5.34
- * Thu Feb 2 18:55:40 MST 2006
+ * Version: 5.36
+ * Mon May 8 01:38:36 MST 2006
*
*/
*
* Copyright (C) 2003-2006 Mark Shelor, All Rights Reserved
*
- * Version: 5.34
- * Thu Feb 2 18:55:40 MST 2006
+ * Version: 5.36
+ * Mon May 8 01:38:36 MST 2006
*
*/
*
* Copyright (C) 2003-2006 Mark Shelor, All Rights Reserved
*
- * Version: 5.34
- * Thu Feb 2 18:55:40 MST 2006
+ * Version: 5.36
+ * Mon May 8 01:38:36 MST 2006
*
*/
*
* Copyright (C) 2003-2006 Mark Shelor, All Rights Reserved
*
- * Version: 5.34
- * Thu Feb 2 18:55:40 MST 2006
+ * Version: 5.36
+ * Mon May 8 01:38:36 MST 2006
*
*/
#include <limits.h>
#define SHA32_MAX 4294967295U
-#define SHA64_MAX 18446744073709551615U
#define SHA32_SHR(x, n) ((x) >> (n))
#define SHA32_SHL(x, n) ((x) << (n))
#elif defined(SHA_ULL_EXISTS)
#undef SHA64_ALIGNED
#undef SHA64_SHR
+ #define SHA64_MAX 18446744073709551615ULL
#define SHA64_SHR(x, n) (((x) & SHA64_MAX) >> (n))
#define SHA64 unsigned long long
#define SHA64_CONST(c) c ## ULL
-\r
- Sample Vectors for SHA-1 Testing\r
-\r
- This file describes tests and vectors that can be used in verifying the correctness of \r
-an SHA-1 implementation. However, use of these vectors does not take the place of validation \r
-obtained through the Cryptographic Module Validation Program.\r
-\r
- There are three areas of the Secure Hash Standard for which test vectors are supplied:\r
-short messages of varying length, selected long messages, and pseudorandomly generated messages.\r
-Since it is possible for an implementation to correctly handle the hashing of byte-oriented\r
-messages (and not messages of a non-byte length), the SHS tests each come in two flavors. For\r
-both byte oriented and bit oriented messages, the message lengths are given in bits.\r
-\r
-Type I Test: Messages of Varying Length\r
-\r
- An implementation of the SHS must be able to correctly generate message digests for\r
-messages of arbitrary length. This functionality can be tested by supplying the implementation\r
-with 1025 pseudorandomly generated messages with lengths from 0 to 1024 bits (for an implementation\r
-that only hashes byte-oriented data correctly, 129 messages of length 0, 8, 16, 24,...,1024 bits\r
-will be supplied).\r
-\r
-Type II Test: Selected Long Messages\r
-\r
- Additional testing of an implementation can be performed by testing that the implementation\r
-can correctly generate digests for longer messages. A list of 100 messages, each of length > 1024,\r
-is supplied. These can be used to verify the hashing of longer message lengths. For bit oriented\r
-testing the messages are from 1025 to 103425 bits long (length=1025+i*1024, where 0<=i<100). For\r
-byte oriented testing the messages are from 1032 to 103432 (length=1032+i*1024, where 0<=i<100).\r
-\r
-Type III Test: Pseudorandomly Generated Messages\r
-\r
- This test determines whether the implementation can compute message digests for messages\r
-that are generated using a given seed. A sequence of 100 message digests is generated using this\r
-seed. The digests are generated according to the following pseudocode:\r
-\r
-procedure MonteCarlo(string SEED)\r
-{\r
- integer i, j, a;\r
- string M;\r
-\r
- M := SEED;\r
- for j = 0 to 99 do {\r
- for i = 1 to 50000 do {\r
- for a = 1 to (j/4*8 + 24) do M := M || 0; /*0' is the binary zero bit. */\r
- M := M || i; /* Here, the value for i is expressed as a 32-bit word\r
- and concatenated with M. The first bit\r
- concatenated with M is the most significant bit of\r
- this 32-bit word. */\r
- M := SHA(M);\r
- }\r
- print(M);\r
- }\r
- }\r
-\r
-NOTE: In the above procedure, || denotes concatenation. Also, M || i denotes appending the 32-bit\r
-word representing the value i, as defined in section 2 of the SHS. Within the procedure, M is a string\r
-of variable length. The initial length of 416 bits ensures that the length of M never exceeds 512 bits\r
-during execution of the above procedure, and it ensures that messages will be of a byte length. Each\r
-element printed should be 160 bits in length.\r
-\r
-\r
-File formats:\r
-\r
-There are two files included for each test type (bit-oriented and byte-oriented). One file contains\r
-the messages and the other file contains the hashes.\r
-\r
-The message files provided use "compact strings" to store the message values. Compact strings are \r
-used to represented the messages in a compact form. A compact string has the form\r
- z || b || n(1) || n(2) || ... || n(z)\r
-where z>=0 that represents the number of n, b is either 0 or 1, and each n(i) is a decimal integer\r
-representing a positive number. The length of the compact string is given by the summation of the n(i).\r
-\r
-The compact string is interpreted as the representation of the bit string consisting of b repeated n(1) times,\r
-followed by 1-b repeated n(2) times, followed by b repeated n(3) times, and so on.\r
-\r
-Example:\r
- M = 5 1 7 13 5 1 2\r
- where z = 5 and b = 1. Then the compact string M represents the bit string\r
- 1111111000000000000011111011\r
- where 1 is repeated 7 times, 0 is repeated 13 times, 1 is repeated 5 times,\r
- 0 is repeated 1 time, and 1 is repeated 2 times.\r
-\r
+
+ Sample Vectors for SHA-1 Testing
+
+ This file describes tests and vectors that can be used in verifying the correctness of
+an SHA-1 implementation. However, use of these vectors does not take the place of validation
+obtained through the Cryptographic Module Validation Program.
+
+ There are three areas of the Secure Hash Standard for which test vectors are supplied:
+short messages of varying length, selected long messages, and pseudorandomly generated messages.
+Since it is possible for an implementation to correctly handle the hashing of byte-oriented
+messages (and not messages of a non-byte length), the SHS tests each come in two flavors. For
+both byte oriented and bit oriented messages, the message lengths are given in bits.
+
+Type I Test: Messages of Varying Length
+
+ An implementation of the SHS must be able to correctly generate message digests for
+messages of arbitrary length. This functionality can be tested by supplying the implementation
+with 1025 pseudorandomly generated messages with lengths from 0 to 1024 bits (for an implementation
+that only hashes byte-oriented data correctly, 129 messages of length 0, 8, 16, 24,...,1024 bits
+will be supplied).
+
+Type II Test: Selected Long Messages
+
+ Additional testing of an implementation can be performed by testing that the implementation
+can correctly generate digests for longer messages. A list of 100 messages, each of length > 1024,
+is supplied. These can be used to verify the hashing of longer message lengths. For bit oriented
+testing the messages are from 1025 to 103425 bits long (length=1025+i*1024, where 0<=i<100). For
+byte oriented testing the messages are from 1032 to 103432 (length=1032+i*1024, where 0<=i<100).
+
+Type III Test: Pseudorandomly Generated Messages
+
+ This test determines whether the implementation can compute message digests for messages
+that are generated using a given seed. A sequence of 100 message digests is generated using this
+seed. The digests are generated according to the following pseudocode:
+
+procedure MonteCarlo(string SEED)
+{
+ integer i, j, a;
+ string M;
+
+ M := SEED;
+ for j = 0 to 99 do {
+ for i = 1 to 50000 do {
+ for a = 1 to (j/4*8 + 24) do M := M || 0; /*0' is the binary zero bit. */
+ M := M || i; /* Here, the value for i is expressed as a 32-bit word
+ and concatenated with M. The first bit
+ concatenated with M is the most significant bit of
+ this 32-bit word. */
+ M := SHA(M);
+ }
+ print(M);
+ }
+ }
+
+NOTE: In the above procedure, || denotes concatenation. Also, M || i denotes appending the 32-bit
+word representing the value i, as defined in section 2 of the SHS. Within the procedure, M is a string
+of variable length. The initial length of 416 bits ensures that the length of M never exceeds 512 bits
+during execution of the above procedure, and it ensures that messages will be of a byte length. Each
+element printed should be 160 bits in length.
+
+
+File formats:
+
+There are two files included for each test type (bit-oriented and byte-oriented). One file contains
+the messages and the other file contains the hashes.
+
+The message files provided use "compact strings" to store the message values. Compact strings are
+used to represented the messages in a compact form. A compact string has the form
+ z || b || n(1) || n(2) || ... || n(z)
+where z>=0 that represents the number of n, b is either 0 or 1, and each n(i) is a decimal integer
+representing a positive number. The length of the compact string is given by the summation of the n(i).
+
+The compact string is interpreted as the representation of the bit string consisting of b repeated n(1) times,
+followed by 1-b repeated n(2) times, followed by b repeated n(3) times, and so on.
+
+Example:
+ M = 5 1 7 13 5 1 2
+ where z = 5 and b = 1. Then the compact string M represents the bit string
+ 1111111000000000000011111011
+ where 1 is repeated 7 times, 0 is repeated 13 times, 1 is repeated 5 times,
+ 0 is repeated 1 time, and 1 is repeated 2 times.
+
-# Configuration information for "SHA-1 Test"\r
-# SHA tests are configured for BIT oriented implementations\r
-H>SHS Type 1 Hashes<H\r
-D>\r
-DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ^\r
-59C4526AA2CC59F9A5F56B5579BA7108E7CCB61A ^\r
-6E42FB84067CFF056C43A49E484997AF23190879 ^\r
-C63FBB9A87171A176E6E054890E29A8C5F125F6C ^\r
-3109E33C1C4B9A0169D1599169D0E5A520A1E71C ^\r
-9195E1E73CC68D7170F44BD1D83CB624BC87FA0B ^\r
-64F7C374527278C0436DBC8DE5AABEC2BBF634BC ^\r
-154B622EA426FB151B1FF1BE1CE871752B9EDEB4 ^\r
-12BDD00FD4038756CBCF8ECDAD1B0CD862603CD8 ^\r
-6700F93E1691E83735279E167F67AF61FEE9813B ^\r
+# Configuration information for "SHA-1 Test"
+# SHA tests are configured for BIT oriented implementations
+H>SHS Type 1 Hashes<H
+D>
+DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ^
+59C4526AA2CC59F9A5F56B5579BA7108E7CCB61A ^
+6E42FB84067CFF056C43A49E484997AF23190879 ^
+C63FBB9A87171A176E6E054890E29A8C5F125F6C ^
+3109E33C1C4B9A0169D1599169D0E5A520A1E71C ^
+9195E1E73CC68D7170F44BD1D83CB624BC87FA0B ^
+64F7C374527278C0436DBC8DE5AABEC2BBF634BC ^
+154B622EA426FB151B1FF1BE1CE871752B9EDEB4 ^
+12BDD00FD4038756CBCF8ECDAD1B0CD862603CD8 ^
+6700F93E1691E83735279E167F67AF61FEE9813B ^
-# Configuration information for "SHA-1 Test"\r
-# SHA tests are configured for BIT oriented implementations\r
-H>SHS Type 1 Strings<H\r
-D>\r
-0 1 ^\r
-1 1 1 ^\r
-2 1 1 1 ^\r
-3 0 1 1 1 ^\r
-2 0 2 2 ^\r
-4 1 1 1 2 1 ^\r
-3 0 2 2 2 ^\r
-4 1 1 2 2 2 ^\r
-5 1 2 2 1 1 2 ^\r
-5 0 2 2 1 1 3 ^\r
+# Configuration information for "SHA-1 Test"
+# SHA tests are configured for BIT oriented implementations
+H>SHS Type 1 Strings<H
+D>
+0 1 ^
+1 1 1 ^
+2 1 1 1 ^
+3 0 1 1 1 ^
+2 0 2 2 ^
+4 1 1 1 2 1 ^
+3 0 2 2 2 ^
+4 1 1 2 2 2 ^
+5 1 2 2 1 1 2 ^
+5 0 2 2 1 1 3 ^
-# Configuration information for "SHA-1 Test"\r
-# SHA tests are configured for BYTE oriented implementations\r
-H>SHS Type 1 Hashes<H\r
-D>\r
-DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ^\r
-3CDF2936DA2FC556BFA533AB1EB59CE710AC80E5 ^\r
-19C1E2048FA7393CFBF2D310AD8209EC11D996E5 ^\r
-CA775D8C80FAA6F87FA62BECA6CA6089D63B56E5 ^\r
-71AC973D0E4B50AE9E5043FF4D615381120A25A0 ^\r
-A6B5B9F854CFB76701C3BDDBF374B3094EA49CBA ^\r
-D87A0EE74E4B9AD72E6847C87BDEEB3D07844380 ^\r
-1976B8DD509FE66BF09C9A8D33534D4EF4F63BFD ^\r
-5A78F439B6DB845BB8A558E4CEB106CD7B7FF783 ^\r
-F871BCE62436C1E280357416695EE2EF9B83695C ^\r
+# Configuration information for "SHA-1 Test"
+# SHA tests are configured for BYTE oriented implementations
+H>SHS Type 1 Hashes<H
+D>
+DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ^
+3CDF2936DA2FC556BFA533AB1EB59CE710AC80E5 ^
+19C1E2048FA7393CFBF2D310AD8209EC11D996E5 ^
+CA775D8C80FAA6F87FA62BECA6CA6089D63B56E5 ^
+71AC973D0E4B50AE9E5043FF4D615381120A25A0 ^
+A6B5B9F854CFB76701C3BDDBF374B3094EA49CBA ^
+D87A0EE74E4B9AD72E6847C87BDEEB3D07844380 ^
+1976B8DD509FE66BF09C9A8D33534D4EF4F63BFD ^
+5A78F439B6DB845BB8A558E4CEB106CD7B7FF783 ^
+F871BCE62436C1E280357416695EE2EF9B83695C ^
-# Configuration information for "SHA-1 Test"\r
-# SHA tests are configured for BYTE oriented implementations\r
-H>SHS Type 1 Strings<H\r
-D>\r
-0 1 ^\r
-5 0 2 1 2 1 2 ^\r
-5 0 1 3 4 4 4 ^\r
-7 0 4 3 4 4 1 4 4 ^\r
-10 0 4 1 5 3 4 4 3 1 3 4 ^\r
-10 0 3 1 6 5 5 1 3 6 6 4 ^\r
-13 1 3 2 5 3 3 3 4 6 6 1 4 6 2 ^\r
-16 1 3 5 5 1 2 1 3 3 6 3 5 2 3 5 7 2 ^\r
-15 1 8 1 5 3 2 7 4 5 6 7 3 3 1 6 3 ^\r
-15 1 4 6 8 2 1 4 2 5 1 6 8 8 6 4 7 ^\r
+# Configuration information for "SHA-1 Test"
+# SHA tests are configured for BYTE oriented implementations
+H>SHS Type 1 Strings<H
+D>
+0 1 ^
+5 0 2 1 2 1 2 ^
+5 0 1 3 4 4 4 ^
+7 0 4 3 4 4 1 4 4 ^
+10 0 4 1 5 3 4 4 3 1 3 4 ^
+10 0 3 1 6 5 5 1 3 6 6 4 ^
+13 1 3 2 5 3 3 3 4 6 6 1 4 6 2 ^
+16 1 3 5 5 1 2 1 3 3 6 3 5 2 3 5 7 2 ^
+15 1 8 1 5 3 2 7 4 5 6 7 3 3 1 6 3 ^
+15 1 4 6 8 2 1 4 2 5 1 6 8 8 6 4 7 ^