lib/Mail/DKIM2/Digest.pm

filter: Statementstmt 61.6% 53 / 86 Branchbran 40.6% 13 / 32 Conditioncond 42.8% 6 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 63.1% 12 / 19 Podpod 100% 12 / 12 totaltotal 57.8% 99 / 171 CC 27 SCAR 17.0
CC
27
Cov
55%
openssl_available
9.8
openssl_available
9.8
new
6.5
CRAP 5.5
« blib/lib/Mail/DKIM2/Split.pm ↑ Summary lib/Mail/DKIM2/MessageStore.pm »
1 package Mail::DKIM2::Digest;
2 212 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 212 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 212 our $VERSION = '0.35';
6  
7 212 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 212 ▶use Crypt::Digest::SHA256 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 212 ▶use Crypt::Digest::SHA512 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The hashes over message content -- every Message-Instance's header and
12 # body hashes -- are almost all of a verifier's work on a big message.
13 # CryptX's SHA-2 is portable C (about 460 MB/s on an Apple M-series, 150 on
14 # the dkim2.com box); OpenSSL's uses the CPU's SHA instructions where there
15 # are some (3.3 GB/s and 270 MB/s). So OpenSSL's, through Net::SSLeay, when
16 # that is installed (it usually is: IO::Socket::SSL needs it), and CryptX's
17 # otherwise. The digests are the same.
18  
19 212 my %CRYPTX = (
20 sha256 => 'Crypt::Digest::SHA256',
21 sha512 => 'Crypt::Digest::SHA512',
22 );
23  
24 212 my ($OPENSSL, %MD);
25 10175 ▶sub openssl_available {
Subroutine openssl_available: called CC=5 SCAR=22.8
Pod documented
26 0 ▶ $OPENSSL //= eval {
Subroutine openssl_available: not called CC=5 SCAR=22.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? : 172 0 172
eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? : 0 0 0
Condition50%2/4
$OPENSSL //= eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? 1 : 0
l!l
10003172
$OPENSSL //= eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? 1 : 0
l!l
00
MC/DC100%1/1
$OPENSSL //= eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? 1 : 0
$OPENSSL
MC/DC0%0/1
$OPENSSL //= eval { do { require Net::SSLeay; &Net::SSLeay::OpenSSL_add_all_digests(); foreach my $alg (keys %CRYPTX) { $MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; } ; 1 } } ? 1 : 0
$OPENSSL
Truth table100%2/2
A
A= $OPENSSL
Aresult
11
00
Truth table0%0/2
A
A= $OPENSSL
Aresult
11
00
27 0 require Net::SSLeay;
28 0 Net::SSLeay::OpenSSL_add_all_digests();
29 0 for my $alg (keys %CRYPTX) {
30 0 ▶ $MD{$alg} = Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n";
Branch: 2 branches
BranchTrueFalseTotal
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n" 344 0 344
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n" 0 0 0
31 }
32 0 1;
33 } ? 1 : 0;
34 0 return $OPENSSL;
35 }
36  
37 # 'openssl' or 'cryptx'; set (local) to choose, as the tests do.
38 212 our $BACKEND;
39  
40 10094 ▶sub backend {
Subroutine backend: called CC=3 SCAR=15.7
Pod documented
41 0 ▶ $BACKEND //= openssl_available() ? 'openssl' : 'cryptx';
Subroutine backend: not called CC=3 SCAR=15.7
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
openssl_available() ? : 172 0 172
openssl_available() ? : 0 0 0
Condition50%2/4
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
l!l
9922172
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
l!l
00
MC/DC100%1/1
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
$Mail::DKIM2::Digest::BACKEND
MC/DC0%0/1
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
$Mail::DKIM2::Digest::BACKEND
Truth table100%2/2
A
A= $Mail::DKIM2::Digest::BACKEND
Aresult
11
00
Truth table0%0/2
A
A= $Mail::DKIM2::Digest::BACKEND
Aresult
11
00
42 0 return $BACKEND;
43 }
44  
45 # new($alg): an object with add($data, ...) and digest(), as CryptX's.
46 10093 ▶sub new {
Subroutine new: called CC=4 SCAR=18.7
Pod documented
47 0 ▶ my ($class, $alg) = @_;
Subroutine new: not called CC=4 SCAR=18.7
Pod documented
48 0 ▶ my $cx = $CRYPTX{$alg} or croak "unsupported hash algorithm: $alg";
Branch: 2 branches
BranchTrueFalseTotal
my $cx = $CRYPTX{$alg} or croak("unsupported hash algorithm: $alg") 10091 2 10093
my $cx = $CRYPTX{$alg} or croak("unsupported hash algorithm: $alg") 0 0 0
49 0 ▶ return $cx->new unless backend() eq 'openssl' && openssl_available();
Branch: 2 branches
BranchTrueFalseTotal
unless backend() eq "openssl" and openssl_available() 89 10002 10091
unless backend() eq "openssl" and openssl_available() 0 0 0
Condition33%2/6
backend() eq "openssl" and openssl_available()
!ll&&!rl&&r
89010002
backend() eq "openssl" and openssl_available()
!ll&&!rl&&r
000
MC/DC50%1/2
backend() eq "openssl" and openssl_available()
backend() eq "openssl" openssl_available()
MC/DC0%0/2
backend() eq "openssl" and openssl_available()
backend() eq "openssl" openssl_available()
Truth table66%2/3
A and B
A= backend() eq "openssl"B= openssl_available()
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= backend() eq "openssl"B= openssl_available()
ABresult
0X0
100
111
50 0 my $ctx = Net::SSLeay::EVP_MD_CTX_create();
51 0 Net::SSLeay::EVP_DigestInit($ctx, $MD{$alg});
52 0 return bless \$ctx, $class;
53 }
54  
55 # Net::SSLeay hashes a string's buffer as it finds it, which for a string
56 # Perl holds as characters is the UTF-8 of them: the octets 0x80 to 0xFF
57 # would be hashed as two each. Such a string is hashed as the octets it
58 # stands for, as CryptX (and Digest::SHA) hash it; the caller's copy is left
59 # alone.
60 15231 ▶sub add {
Subroutine add: called CC=4 SCAR=17.9
Pod documented
61 0 ▶ my $self = shift;
Subroutine add: not called CC=4 SCAR=17.9
Pod documented
62 0 for (@_) {
63 0 ▶ if (utf8::is_utf8($_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (utf8::is_utf8($_)) { } 12 15221 15233
if (utf8::is_utf8($_)) { } 0 0 0
64 0 my $octets = $_;
65 0 ▶ utf8::downgrade($octets, 1) or croak 'Wide character in data to hash';
Branch: 2 branches
BranchTrueFalseTotal
utf8::downgrade($octets, 1) or croak("Wide character in data to hash") 10 2 12
utf8::downgrade($octets, 1) or croak("Wide character in data to hash") 0 0 0
66 0 Net::SSLeay::EVP_DigestUpdate($$self, $octets);
67 } else {
68 0 Net::SSLeay::EVP_DigestUpdate($$self, $_);
69 }
70 }
71 0 return $self;
72 }
73  
74 9885 ▶sub digest {
Subroutine digest: called CC=1 SCAR=1.2
Pod documented
75 0 ▶ my ($self) = @_;
Subroutine digest: not called CC=1 SCAR=1.2
Pod documented
76 0 my $d = Net::SSLeay::EVP_DigestFinal($$self);
77 0 Net::SSLeay::EVP_MD_CTX_destroy($$self);
78 0 $$self = undef;
79 0 return $d;
80 }
81  
82 9907 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=9.2
83 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=2 SCAR=9.2
84 0 ▶ Net::SSLeay::EVP_MD_CTX_destroy($$self) if $$self;
Branch: 2 branches
BranchTrueFalseTotal
if $$self 27 9880 9907
if $$self 0 0 0
85 }
86  
87 # digest($alg, $data): the raw digest of $data.
88 6494 ▶sub digest_data {
Subroutine digest_data: called CC=1 SCAR=1.2
Pod documented
89 0 ▶ my $d = __PACKAGE__->new($_[0]);
Subroutine digest_data: not called CC=1 SCAR=1.2
Pod documented
90 0 $d->add($_[1]);
91 0 return $d->digest;
92 }
93  
94 212 1;
95  
96 __END__
« blib/lib/Mail/DKIM2/Split.pm ↑ Summary lib/Mail/DKIM2/MessageStore.pm »