| 1 | package Crypt::Passphrase::Pepper::Base; | ||||||||||||||||||||
| 2 | 3 | $Crypt::Passphrase::Pepper::Base::VERSION = '0.024'; | |||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 3 | ▶ | use Carp 'croak'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use Crypt::Passphrase -encoder, -integration; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use MIME::Base64 'encode_base64'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||
| 11 | 1 | my ($class, %args) = @_; | |||||||||||||||||||
| 12 | 1 | ▶ | my $inner = delete $args{inner} // croak('No inner encoder given to pepper'); | ||||||||||||||||||
|
Condition50%1/2
delete $args{"inner"} // croak("No inner encoder given to pepper")
MC/DC0%0/1
delete $args{"inner"} // croak("No inner encoder given to pepper")
delete $args{"inner"}
Truth table50%1/2
A
A= delete $args{"inner"}
| |||||||||||||||||||||
| 13 | 1 | my $encoder = Crypt::Passphrase::_load_encoder($inner); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | croak('No peppers given') if not defined $args{active}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | 1 | ▶ | croak("Invalid pepper name '$args{active}'") if $args{active} =~ /\W/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 1 | ▶ | croak('No hashing algorithm given') if not defined $args{algorithm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | my $self = bless { | |||||||||||||||||||
| 20 | %args, | ||||||||||||||||||||
| 21 | inner => $encoder, | ||||||||||||||||||||
| 22 | }, $class; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | return $self; | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 4 | ▶ | sub _to_inner { | ||||||||||||||||||
|
Subroutine
_to_inner: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 28 | 4 | my $hash = shift; | |||||||||||||||||||
| 29 | 4 | ▶ | if ($hash =~ s/ (?<= \A \$) ([\w-]+?)-pepper-([\w-]+) \$ v=1 , id=([^\$,]+) /$1/x) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 2 | return ($hash, $2, $3); | |||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||
| 32 | 2 | return; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub prehash_password; | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | ▶ | sub hash_password { | ||||||||||||||||||
|
Subroutine
hash_password: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 39 | 1 | my ($self, $password) = @_; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 1 | my $prehashed = $self->prehash_password($password, $self->{algorithm}, $self->{active}); | |||||||||||||||||||
| 42 | 1 | my $wrapped = encode_base64($prehashed, '') =~ tr/=//dr; | |||||||||||||||||||
| 43 | 1 | my $hash = $self->{inner}->hash_password($wrapped); | |||||||||||||||||||
| 44 | 1 | return $hash =~ s/ (?<= \A \$) ([^\$]+) /$1-pepper-$self->{algorithm}\$v=1,id=$self->{active}/rx; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | ▶ | sub crypt_subtypes { | ||||||||||||||||||
|
Subroutine
crypt_subtypes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 48 | 1 | my $self = shift; | |||||||||||||||||||
| 49 | 1 | my @result; | |||||||||||||||||||
| 50 | 1 | my @supported = $self->supported_hashes; | |||||||||||||||||||
| 51 | 1 | for my $inner ($self->{inner}->crypt_subtypes) { | |||||||||||||||||||
| 52 | 1 | push @result, $inner, map { "$inner-pepper-$_" } @supported; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | 1 | return @result; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | sub needs_rehash { | ||||||||||||||||||
|
Subroutine
needs_rehash: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 58 | 2 | my ($self, $hash) = @_; | |||||||||||||||||||
| 59 | 2 | ▶ | my ($primary, $type, $id) = _to_inner($hash) or return 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 1 | ▶ | return "$type,$id" ne join(',', @{$self}{qw/algorithm active/}) || $self->{inner}->needs_rehash($primary); | ||||||||||||||||||
|
Condition33%1/3
"$type,$id" ne join(',', @{$self;}{'algorithm', 'active'}) || $self->{'inner'}->needs_rehash($primary)
MC/DC0%0/2
"$type,$id" ne join(',', @{$self;}{'algorithm', 'active'}) || $self->{'inner'}->needs_rehash($primary)
"$type,$id" ne join(',', @{$self;}{'algorithm', 'active'})
$self->{'inner'}->needs_rehash($primary)
Truth table33%1/3
A || B
A= "$type,$id" ne join(',', @{$self;}{'algorithm', 'active'})B= $self->{'inner'}->needs_rehash($primary)
| |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | ▶ | sub verify_password { | ||||||||||||||||||
|
Subroutine
verify_password: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 64 | 2 | my ($self, $password, $hash) = @_; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 2 | ▶ | if (my ($primary, $type, $id) = _to_inner($hash)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 67 | 1 | ▶ | my $prehashed = eval { $self->prehash_password($password, $type, $id) } or return !!0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 1 | my $wrapped = encode_base64($prehashed, '') =~ tr/=//dr; | |||||||||||||||||||
| 69 | 1 | return $self->{inner}->verify_password($wrapped, $primary); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | elsif ($self->{inner}->accepts_hash($hash)) { | ||||||||||||||||||||
| 72 | 1 | return $self->{inner}->verify_password($password, $hash); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||
| 75 | 0 | return !!0; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 3 | 1; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # ABSTRACT: A base class for pre-hashing pepper implementations | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||