]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
sha: avoid string eval for loading Net::SSLeay
authorEric Wong <e@80x24.org>
Fri, 9 May 2025 23:11:57 +0000 (23:11 +0000)
committerEric Wong <e@80x24.org>
Fri, 9 May 2025 23:25:43 +0000 (23:25 +0000)
We can rely on require + import and use glob assigments to give
anonymous subs a place in the symbol table.  This ought to make
it easier to do compile-only syntax checking and hopefully
easier for a hypothetical Perl implementation to optimize.

lib/PublicInbox/SHA.pm

index 5eb882c642adc4bf1763ac80683c608e9cd617fe..e708ea03888b5f03dc7eef4f41dcf4bab372298e 100644 (file)
@@ -20,43 +20,42 @@ our @ISA;
 
 BEGIN {
        push @ISA, 'Exporter';
-       unless (eval(<<'EOM')) {
-use Net::SSLeay 1.43;
-my %SHA = (
-       1 => Net::SSLeay::EVP_sha1(),
-       256 => Net::SSLeay::EVP_sha256(),
-);
+       eval {
+               require Net::SSLeay;
+               Net::SSLeay->import(1.43); # for Net::SSLeay::EVP_*
+               my %SHA = (
+                       1 => Net::SSLeay::EVP_sha1(),
+                       256 => Net::SSLeay::EVP_sha256(),
+               );
 
-sub new {
-       my ($cls, $n) = @_;
-       my $mdctx = Net::SSLeay::EVP_MD_CTX_create();
-       Net::SSLeay::EVP_DigestInit($mdctx, $SHA{$n}) or
-                       die "EVP_DigestInit $n: $!";
-       bless \$mdctx, $cls;
-}
-
-sub add {
-       my $self = shift;
-       Net::SSLeay::EVP_DigestUpdate($$self, $_) for @_;
-       $self;
-}
+               *new = sub {
+                       my ($cls, $n) = @_;
+                       my $mdctx = Net::SSLeay::EVP_MD_CTX_create();
+                       Net::SSLeay::EVP_DigestInit($mdctx, $SHA{$n}) or
+                                       die "EVP_DigestInit $n: $!";
+                       bless \$mdctx, $cls;
+               };
 
-sub digest { Net::SSLeay::EVP_DigestFinal(${$_[0]}) };
-sub hexdigest { unpack('H*', Net::SSLeay::EVP_DigestFinal(${$_[0]})) }
-sub DESTROY { Net::SSLeay::EVP_MD_CTX_destroy(${$_[0]}) };
-
-sub sha1_hex { unpack('H*', Net::SSLeay::SHA1($_[0])) };
-sub sha256_hex { unpack('H*', Net::SSLeay::SHA256($_[0])) };
-*sha256 = \&Net::SSLeay::SHA256;
-# end of eval
-EOM
-       require Digest::SHA; # stdlib fallback
-       push @ISA, 'Digest::SHA';
-       *sha1_hex = \&Digest::SHA::sha1_hex;
-       *sha256_hex = \&Digest::SHA::sha256_hex;
-       *sha256 = \&Digest::SHA::sha256;
-}
+               *add = sub {
+                       my $self = shift;
+                       Net::SSLeay::EVP_DigestUpdate($$self, $_) for @_;
+                       $self;
+               };
 
+               *digest = sub { Net::SSLeay::EVP_DigestFinal(${$_[0]}) };
+               *hexdigest = sub { unpack 'H*', digest($_[0]) };
+               *DESTROY = sub { Net::SSLeay::EVP_MD_CTX_destroy(${$_[0]}) };
+               *sha1_hex = sub { unpack 'H*', Net::SSLeay::SHA1($_[0]) };
+               *sha256_hex = sub { unpack 'H*', Net::SSLeay::SHA256($_[0]) };
+               *sha256 = \&Net::SSLeay::SHA256;
+       }; # end of eval
+       if ($@) { # Net::SSLeay unavailable
+               require Digest::SHA; # stdlib fallback
+               push @ISA, 'Digest::SHA';
+               *sha1_hex = \&Digest::SHA::sha1_hex;
+               *sha256_hex = \&Digest::SHA::sha256_hex;
+               *sha256 = \&Digest::SHA::sha256;
+       }
 } # /BEGIN
 
 sub sha_all ($$) {