I am trying to build a flexible script that returns various SHA algorithms and string types.
I have it to the point where I am repeating very similar blocks of code, and I can't see a way to abstract them into a clean function call. I've gone down a couple of dead ends, and am just banging my head against a wall. Does anyone have any insights to share?
The full script is in my public scratchpad , but here are two examples of blocks I wish I could turn into one abstract function call:
#
# There has to be a way to abstract these do_xxx blocks...
#
if ($do_sha1) {
if (! open(FILE,"$fileName")) { print STDERR "$0 : cannot open file
+ \"$fileName\"\n"; return(0); }
binmode FILE ;
my $s1 = Digest::SHA->new(1) ;
my $sha1;
if ($family{sha1}) {
$s1->addfile(*FILE);
$sha1 = $s1->digest;
my $ps = unpack("B*",$sha1);
while (length($ps) < 160) { $ps = "0$ps"; }
print "sha1($fileName)= $ps\n";
seek(FILE,0,0);
}
# The block above seeks back to start-of-file,
# because addfile(*FILE) needs to reread the data,
# but that's horrible!
# I don't know if I can slurp it all into a variable, though...
if ($family{sha1_hex}) {
$s1->addfile(*FILE);
$sha1 = lc($s1->hexdigest);
if ($opt_u) { $sha1 = uc($sha1); }
print "sha1_hex($fileName)= $sha1\n";
seek(FILE,0,0);
}
if ($family{sha1_b64}) {
$s1->addfile(*FILE);
$sha1 = $s1->b64digest;
while (length($sha1) % 4) { $sha1 .= '='; }
print "sha1_b64($fileName)= $sha1\n";
}
close(FILE);
}
if ($do_256) {
if (! open(FILE,"$fileName")) { print STDERR "$0 : cannot open file
+ \"$fileName\"\n"; return(0); }
binmode FILE ;
my $s256 = Digest::SHA->new(256) ;
my $sha256;
if ($family{sha256}) {
$s256->addfile(*FILE);
$sha256 = $s256->digest;
my $ps = unpack("B*",$sha256);
while (length($ps) < 160) { $ps = "0$ps"; }
print "sha1($fileName)= $ps\n";
seek(FILE,0,0);
}
if ($family{sha256_hex}) {
$s256->addfile(*FILE);
$sha256 = lc($s256->hexdigest);
if ($opt_u) { $sha256 = uc($sha256); }
print "sha256_hex($fileName)= $sha256\n";
seek(FILE,0,0);
}
if ($family{sha256_b64}) {
$s256->addfile(*FILE);
$sha256 = $s256->b64digest;
while (length($sha256) % 4) { $sha256 .= '='; }
print "sha256_b64($fileName)= $sha256\n";
}
close(FILE);
}