Skip to content

Commit 29ff095

Browse files
authored
sum: add missing sha224 algorithm
* sha224 was missing (provided by perl in Digest::SHA) * Take advantage of other Digest algorithms which may already be installed from CPAN (but don't require them to be installed) * Add SHA3 functions as well as JH, Whirlpool, Haval256, BLAKE, MD2 and MD4 * List optional algorithms in usage string and POD (don't attempt to detect which are installed prior to use) * NB: Digest::MD6 fails to build and I sent an email to the maintainer (for now don't include MD6)
1 parent 5c0679f commit 29ff095

File tree

1 file changed

+205
-1
lines changed

1 file changed

+205
-1
lines changed

bin/sum

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,29 @@ if ($Program =~ m/cksum/) {
4444
if (defined $opt{'a'}) {
4545
help() if defined $opt{'o'};
4646
my %codetab = (
47+
'blake224' => \&do_blake,
48+
'blake256' => \&do_blake,
49+
'blake384' => \&do_blake,
50+
'blake512' => \&do_blake,
4751
'crc' => \&crc32,
52+
'jh224' => \&do_jh,
53+
'jh256' => \&do_jh,
54+
'jh384' => \&do_jh,
55+
'jh512' => \&do_jh,
56+
'haval256' => \&do_haval256,
57+
'md2' => \&do_md2,
58+
'md4' => \&do_md4,
4859
'md5' => \&do_md5,
4960
'sha1' => \&sha_all,
61+
'sha224' => \&sha_all,
5062
'sha256' => \&sha_all,
5163
'sha384' => \&sha_all,
5264
'sha512' => \&sha_all,
65+
'sha3-224' => \&do_sha3,
66+
'sha3-256' => \&do_sha3,
67+
'sha3-384' => \&do_sha3,
68+
'sha3-512' => \&do_sha3,
69+
'whirlpool' => \&do_whirlpool,
5370
);
5471
if (!exists($codetab{$opt{'a'}})) {
5572
warn "$Program: invalid algorithm name\n";
@@ -146,6 +163,34 @@ sub sum2 {
146163
return $num,$crc,($len+511)/512; # round # of blocks up ...
147164
}
148165

166+
sub do_md2 {
167+
my $fh = shift;
168+
169+
eval {
170+
require Digest::MD2;
171+
1;
172+
} or do {
173+
die "The md2 algorithm is not available on your system\n";
174+
};
175+
my $ctx = Digest::MD2->new;
176+
$ctx->addfile($fh);
177+
return (0, $ctx->hexdigest, undef);
178+
}
179+
180+
sub do_md4 {
181+
my $fh = shift;
182+
183+
eval {
184+
require Digest::MD4;
185+
1;
186+
} or do {
187+
die "The md5 algorithm is not available on your system\n";
188+
};
189+
my $ctx = Digest::MD4->new;
190+
$ctx->addfile($fh);
191+
return (0, $ctx->hexdigest, undef);
192+
}
193+
149194
sub do_md5 {
150195
my $fh = shift;
151196

@@ -155,6 +200,94 @@ sub do_md5 {
155200
return (0, $ctx->hexdigest, undef);
156201
}
157202

203+
sub do_haval256 {
204+
my $fh = shift;
205+
206+
eval {
207+
require Digest::Haval256;
208+
1;
209+
} or do {
210+
die "The haval256 algorithm is not available on your system\n";
211+
};
212+
my $ctx = Digest::Haval256->new;
213+
$ctx->addfile($fh);
214+
return (0, $ctx->hexdigest, undef);
215+
}
216+
217+
sub do_blake {
218+
my $fh = shift;
219+
220+
eval {
221+
require Digest::BLAKE;
222+
1;
223+
} or do {
224+
die "The $opt{a} algorithm is not available on your system\n";
225+
};
226+
my $sz;
227+
if ($opt{'a'} =~ m/\Ablake([0-9]+)\Z/) {
228+
$sz = $1;
229+
} else {
230+
die "Unknown digest size: " . $opt{'a'};
231+
}
232+
my $ctx = Digest::BLAKE->new($sz);
233+
$ctx->addfile($fh);
234+
return (0, $ctx->hexdigest, undef);
235+
}
236+
237+
sub do_sha3 {
238+
my $fh = shift;
239+
240+
eval {
241+
require Digest::SHA3;
242+
1;
243+
} or do {
244+
die "The $opt{a} algorithm is not available on your system\n";
245+
};
246+
my $sz;
247+
if ($opt{'a'} =~ m/\Asha3\-([0-9]+)\Z/) {
248+
$sz = $1;
249+
} else {
250+
die "Unknown digest size: " . $opt{'a'};
251+
}
252+
my $ctx = Digest::SHA3->new($sz);
253+
$ctx->addfile($fh);
254+
return (0, $ctx->hexdigest, undef);
255+
}
256+
257+
sub do_jh {
258+
my $fh = shift;
259+
260+
eval {
261+
require Digest::JH;
262+
1;
263+
} or do {
264+
die "The $opt{a} algorithm is not available on your system\n";
265+
};
266+
my $sz;
267+
if ($opt{'a'} =~ m/\Ajh([0-9]+)\Z/) {
268+
$sz = $1;
269+
} else {
270+
die "Unknown digest size: " . $opt{'a'};
271+
}
272+
my $ctx = Digest::JH->new($sz);
273+
$ctx->addfile($fh);
274+
return (0, $ctx->hexdigest, undef);
275+
}
276+
277+
sub do_whirlpool {
278+
my $fh = shift;
279+
280+
eval {
281+
require Digest::Whirlpool;
282+
1;
283+
} or do {
284+
die "The whirlpool algorithm is not available on your system\n";
285+
};
286+
my $ctx = Digest::Whirlpool->new;
287+
$ctx->addfile($fh);
288+
return (0, $ctx->hexdigest, undef);
289+
}
290+
158291
sub sha_all {
159292
my $fh = shift;
160293

@@ -252,9 +385,12 @@ sub help {
252385
print "
253386
usage: $Program [-a alg] [-o 1|2] [file ...]
254387
255-
-a alg Select algorithm: crc, md5, sha1, sha256, sha384, sha512
388+
-a alg Select algorithm: crc, md5, sha1, sha224, sha256, sha384, sha512
256389
-o alg Select historic algorithm: 1 (BSD), 2 (SYSV)
257390
391+
Optional alorithms: blake224 blake256 blake384 blake512 jh224 jh256
392+
jh384 jh512 haval256 md2 md4 sha3-224 sha3-256 sha3-384 sha3-512
393+
whirlpool
258394
";
259395
exit EX_FAILURE;
260396
}
@@ -278,10 +414,54 @@ If no file names are specified, the standard input will be used.
278414
279415
=over 4
280416
417+
=item -a blake224
418+
419+
BLAKE/224 algorithm (requires Digest::BLAKE)
420+
421+
=item -a blake256
422+
423+
BLAKE/256 algorithm (requires Digest::BLAKE)
424+
425+
=item -a blake384
426+
427+
BLAKE/384 algorithm (requires Digest::BLAKE)
428+
429+
=item -a blake512
430+
431+
BLAKE/512 algorithm (requires Digest::BLAKE)
432+
281433
=item -a crc
282434
283435
CRC32 algorithm
284436
437+
=item -a jh224
438+
439+
JH/224 algorithm (requires Digest::JH)
440+
441+
=item -a jh256
442+
443+
JH/256 algorithm (requires Digest::JH)
444+
445+
=item -a jh384
446+
447+
JH/384 algorithm (requires Digest::JH)
448+
449+
=item -a jh512
450+
451+
JH/512 algorithm (requires Digest::JH)
452+
453+
=item -a haval256
454+
455+
Haval256 algorithm (requires Digest::Haval256)
456+
457+
=item -a md2
458+
459+
MD2 algorithm (requires Digest::MD2)
460+
461+
=item -a md4
462+
463+
MD4 algorithm (requires Digest::MD4)
464+
285465
=item -a md5
286466
287467
MD5 algorithm
@@ -290,6 +470,10 @@ MD5 algorithm
290470
291471
SHA-1 algorithm
292472
473+
=item -a sha224
474+
475+
SHA-2/256 algorithm
476+
293477
=item -a sha256
294478
295479
SHA-2/256 algorithm
@@ -302,6 +486,26 @@ SHA-2/384 algorithm
302486
303487
SHA-2/512 algorithm
304488
489+
=item -a sha3-224
490+
491+
SHA-3/224 algorithm (requires Digest::SHA3)
492+
493+
=item -a sha3-256
494+
495+
SHA-3/256 algorithm (requires Digest::SHA3)
496+
497+
=item -a sha3-384
498+
499+
SHA-3/384 algorithm (requires Digest::SHA3)
500+
501+
=item -a sha3-512
502+
503+
SHA-3/512 algorithm (requires Digest::SHA3)
504+
505+
=item -a whirlpool
506+
507+
Whirlpool algorithm (requires Digest::Whirlpool)
508+
305509
=item -o 1
306510
307511
Historic BSD algorithm

0 commit comments

Comments
 (0)