| 1 | package Mail::DKIM2::BodyHasher; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.35'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use base 'Mail::DKIM2::HeaderParser'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Mail::DKIM2::Digest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Mail::DKIM2::MessageInstance; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 96 | ▶ | sub known_options { return qw(Algs) } | ||||||||||||||||||||||||||
|
Subroutine
known_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 96 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 96 | my $self = shift; | |||||||||||||||||||||||||||
| 19 | 96 | $self->SUPER::init; | |||||||||||||||||||||||||||
| 20 | 96 | ▶ | my $algs = $self->{Algs} // ['sha256']; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"Algs"} // ["sha256"]
MC/DC100%1/1
$self->{"Algs"} // ["sha256"]
$self->{"Algs"}
Truth table100%2/2
A
A= $self->{"Algs"}
| |||||||||||||||||||||||||||||
| 21 | 96 | ▶ | croak "Algs must be a non-empty array reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $algs eq "ARRAY" and @$algs
MC/DC50%1/2
ref $algs eq "ARRAY" and @$algs
ref $algs eq "ARRAY"
@$algs
Truth table66%2/3
A and B
A= ref $algs eq "ARRAY"B= @$algs
| |||||||||||||||||||||||||||||
| 22 | unless ref $algs eq 'ARRAY' && @$algs; | ||||||||||||||||||||||||||||
| 23 | 95 | my $impl = Mail::DKIM2::MessageInstance::hash_algs(); | |||||||||||||||||||||||||||
| 24 | 95 | my %digest; | |||||||||||||||||||||||||||
| 25 | 95 | for my $alg (map { lc } @$algs) { | |||||||||||||||||||||||||||
| 26 | 96 | ▶ | croak "unsupported hash algorithm $alg" unless $impl->{$alg}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 95 | ▶ | $digest{$alg} //= Mail::DKIM2::Digest->new($alg); | ||||||||||||||||||||||||||
|
Condition33%1/3
$digest{$alg} //= "Mail::DKIM2::Digest"->new($alg)
MC/DC0%0/2
$digest{$alg} //= "Mail::DKIM2::Digest"->new($alg)
$digest{$alg}
"Mail::DKIM2::Digest"->new($alg)
Truth table33%1/3
A //= B
A= $digest{$alg}B= "Mail::DKIM2::Digest"->new($alg)
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 94 | $self->{_digest} = \%digest; | |||||||||||||||||||||||||||
| 30 | 94 | $self->{_pending} = ''; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 93 | ▶ | sub finish_header { | ||||||||||||||||||||||||||
|
Subroutine
finish_header: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 93 | my $self = shift; | |||||||||||||||||||||||||||
| 35 | # Weak: the parser holds the sink, so a strong $self would be a cycle. | ||||||||||||||||||||||||||||
| 36 | 93 | weaken(my $weak = $self); | |||||||||||||||||||||||||||
| 37 | 93 | ▶ | $self->{_body_sink} = sub { $weak->_add_body($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Hash a piece once a megabyte is waiting, holding back the CR and LF at | ||||||||||||||||||||||||||||
| 41 | # its end until it is known whether they end the body. | ||||||||||||||||||||||||||||
| 42 | 417 | ▶ | sub _add_body { | ||||||||||||||||||||||||||
|
Subroutine
_add_body: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 417 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 417 | $self->{_pending} .= $_[0]; | |||||||||||||||||||||||||||
| 45 | 417 | ▶ | return if length $self->{_pending} < Mail::DKIM2::MessageInstance::BODY_CHUNK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | my $took = Mail::DKIM2::MessageInstance::_body_digest_feed( | ||||||||||||||||||||||||||||
| 47 | 19 | [ values %{$self->{_digest}} ], \$self->{_pending}, 0); | |||||||||||||||||||||||||||
| 48 | 19 | substr($self->{_pending}, 0, $took, ''); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 93 | ▶ | sub finish_body { | ||||||||||||||||||||||||||
|
Subroutine
finish_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 93 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | Mail::DKIM2::MessageInstance::_body_digest_feed( | ||||||||||||||||||||||||||||
| 54 | 93 | [ values %{$self->{_digest}} ], \$self->{_pending}, 1); | |||||||||||||||||||||||||||
| 55 | 93 | $self->{_pending} = ''; | |||||||||||||||||||||||||||
| 56 | 93 | my $digest = $self->{_digest}; | |||||||||||||||||||||||||||
| 57 | $self->{_hashes} = { | ||||||||||||||||||||||||||||
| 58 | 93 | map { $_ => encode_base64($digest->{$_}->digest, '') } keys %$digest | |||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub body_hashes { | ||||||||||||||||||||||||||
|
Subroutine
body_hashes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | croak "body_hashes before CLOSE" unless $self->{_hashes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | return { %{$self->{_hashes}} }; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 93 | ▶ | sub body_hash { | ||||||||||||||||||||||||||
|
Subroutine
body_hash: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 93 | my ($self, $alg) = @_; | |||||||||||||||||||||||||||
| 70 | 93 | ▶ | croak "body_hash before CLOSE" unless $self->{_hashes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 93 | ▶ | $alg = lc($alg // 'sha256'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$alg // "sha256"
MC/DC100%1/1
$alg // "sha256"
$alg
Truth table100%2/2
A
A= $alg
| |||||||||||||||||||||||||||||
| 72 | 93 | ▶ | croak "body_hash: $alg was not among Algs" unless exists $self->{_hashes}{$alg}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 92 | return $self->{_hashes}{$alg}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | 1; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||