| 1 | package Crypt::PBKDF2; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The PBKDF2 password hashing algorithm. | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | our $VERSION = '0.261630'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Moo 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Crypt::URandom (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Module::Runtime; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Type::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Types::Standard 1.000005 qw(Str Int HashRef ConsumerOf); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1044 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1044 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 1044 | $self->hasher; # Force instantiation, so we get errors ASAP | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | has hash_class => ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | default => 'HMACSHA2', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | predicate => 'has_hash_class', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | has hash_args => ( | ||||||||||||||||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | isa => HashRef, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | predicate => 'has_hash_args', | |||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | has hasher => ( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => ConsumerOf['Crypt::PBKDF2::Hash'], | ||||||||||||||||||||||||||||||||||||||||||
| 42 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 20 | ▶ | default => sub { shift->_lazy_hasher }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | has _lazy_hasher => ( | |||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | isa => ConsumerOf['Crypt::PBKDF2::Hash'], | ||||||||||||||||||||||||||||||||||||||||||
| 49 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | predicate => 'has_lazy_hasher', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | builder => '_build_hasher', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 20 | ▶ | sub _build_hasher { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_hasher: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 20 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 20 | my $class = $self->hash_class; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | ▶ | if ($class !~ s/^\+//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 20 | $class = "Crypt::PBKDF2::Hash::$class"; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | my $hash_args = $self->hash_args; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 20 | return Module::Runtime::use_module($class)->new( %$hash_args ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | has iterations => ( | |||||||||||||||||||||||||||||||||||||||||
| 68 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 69 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | default => 600000, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | has output_len => ( | |||||||||||||||||||||||||||||||||||||||||
| 75 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 76 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | predicate => 'has_output_len', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | has salt_len => ( | |||||||||||||||||||||||||||||||||||||||||
| 82 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | default => 4, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1023 | ▶ | sub _random_salt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_salt: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1023 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1023 | return Crypt::URandom::urandom($self->salt_len); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 6 | has encoding => ( | |||||||||||||||||||||||||||||||||||||||||
| 94 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 95 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | default => 'ldap', | ||||||||||||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | has length_limit => ( | |||||||||||||||||||||||||||||||||||||||||
| 101 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 102 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | predicate => 'has_length_limit', | ||||||||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1023 | ▶ | sub generate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1023 | my ($self, $password, $salt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 1023 | ▶ | $salt = $self->_random_salt unless defined $salt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1023 | ▶ | if ($self->has_length_limit and length($password) > $self->length_limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_length_limit and length $password > $self->length_limit
MC/DC100%2/2
$self->has_length_limit and length $password > $self->length_limit
$self->has_length_limit
length $password > $self->length_limit
Truth table100%3/3
A and B
A= $self->has_length_limitB= length $password > $self->length_limit
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | croak "Password exceeds length limit"; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1022 | my $hash = $self->PBKDF2($salt, $password); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1022 | return $self->encode_string($salt, $hash); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1023 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1023 | my ($self, $hashed, $password) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 1023 | ▶ | if ($self->has_length_limit and length($password) > $self->length_limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_length_limit and length $password > $self->length_limit
MC/DC100%2/2
$self->has_length_limit and length $password > $self->length_limit
$self->has_length_limit
length $password > $self->length_limit
Truth table100%3/3
A and B
A= $self->has_length_limitB= length $password > $self->length_limit
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | croak "Password exceeds length limit"; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1022 | my $info = $self->decode_string($hashed); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1022 | ▶ | my $hasher = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1022 | $self->hasher_from_algorithm($info->{algorithm}, $info->{algorithm_options}); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $opts = defined($info->{algorithm_options}) ? " (options ''$info->{algorithm_options}'')" : ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | croak "Couldn't construct hasher for ''$info->{algorithm}''$opts: $_"; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1022 | }; | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | my $checker = $self->clone( | ||||||||||||||||||||||||||||||||||||||||||
| 137 | hasher => $hasher, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | iterations => $info->{iterations}, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 1022 | output_len => length($info->{hash}), | |||||||||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1022 | my $check_hash = $checker->PBKDF2($info->{salt}, $password); | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1022 | return _secure_compare($check_hash, $info->{hash}); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Constant-time string comparison, to avoid timing attacks on the hash check. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 1022 | ▶ | sub _secure_compare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_secure_compare: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1022 | my ($a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1022 | my $r = length($a) != length($b); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1022 | ▶ | $a = $b if $r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 1022 | $r |= ord(substr($a, $_)) ^ ord(substr($b, $_)) for 0 .. length($a) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 1022 | return $r == 0; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 5053 | ▶ | sub PBKDF2 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PBKDF2: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 5053 | my ($self, $salt, $password) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 5053 | my $iterations = $self->iterations; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 5053 | my $hasher = $self->hasher; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 5053 | ▶ | my $output_len = $self->output_len || $hasher->hash_len; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->output_len || $hasher->hash_len
MC/DC0%0/2
$self->output_len || $hasher->hash_len
$self->output_len
$hasher->hash_len
Truth table66%2/3
A || B
A= $self->output_lenB= $hasher->hash_len
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 5053 | my $hLen = $hasher->hash_len; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 5053 | my $l = int($output_len / $hLen); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 5053 | my $r = $output_len % $hLen; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 5053 | ▶ | if ($l > 0xffffffff or $l == 0xffffffff && $r > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$l > 4294967295 or $l == 4294967295 and $r > 0
$l == 4294967295 and $r > 0
MC/DC0%0/3
$l > 4294967295 or $l == 4294967295 and $r > 0
$l > 4294967295
$l == 4294967295
$r > 0
Truth table25%1/4
A or B and C
A= $l > 4294967295B= $l == 4294967295C= $r > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | croak "output_len too large for PBKDF2"; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 5053 | my $output; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 5053 | for my $i (1 .. $l) { | |||||||||||||||||||||||||||||||||||||||||
| 177 | 5050 | $output .= $self->_PBKDF2_F($hasher, $salt, $password, $iterations, $i); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 5053 | ▶ | if ($r) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 9 | $output .= substr( $self->_PBKDF2_F($hasher, $salt, $password, $iterations, $l + 1), 0, $r); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 5053 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 1000 | ▶ | sub PBKDF2_base64 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PBKDF2_base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 1000 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1000 | return MIME::Base64::encode( $self->PBKDF2(@_), "" ); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 1009 | ▶ | sub PBKDF2_hex { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PBKDF2_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 1009 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 1009 | return unpack "H*", $self->PBKDF2(@_); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 5059 | ▶ | sub _PBKDF2_F { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_PBKDF2_F: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 5059 | my ($self, $hasher, $salt, $password, $iterations, $i) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 5059 | my $result = | |||||||||||||||||||||||||||||||||||||||||
| 203 | my $hash = | ||||||||||||||||||||||||||||||||||||||||||
| 204 | $hasher->generate( $salt . pack("N", $i), $password ); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 5059 | for my $iter (2 .. $iterations) { | |||||||||||||||||||||||||||||||||||||||||
| 207 | 1407650 | $hash = $hasher->generate( $hash, $password ); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 1407650 | $result ^= $hash; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 5059 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 1022 | ▶ | sub encode_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_string: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1022 | my ($self, $salt, $hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 1022 | ▶ | if ($self->encoding eq 'crypt') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 511 | return $self->_encode_string_cryptlike($salt, $hash); | |||||||||||||||||||||||||||||||||||||||||
| 219 | } elsif ($self->encoding eq 'ldap') { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 511 | return $self->_encode_string_ldaplike($salt, $hash); | |||||||||||||||||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | die "Unknown setting '", $self->encoding, "' for encoding"; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 511 | ▶ | sub _encode_string_cryptlike { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_string_cryptlike: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 511 | my ($self, $salt, $hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 511 | my $hasher = $self->hasher; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 511 | my $hasher_class = blessed($hasher); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 511 | ▶ | if (!defined $hasher_class || $hasher_class !~ s/^Crypt::PBKDF2::Hash:://) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $hasher_class and $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
MC/DC0%0/2
defined $hasher_class and $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
defined $hasher_class
$hasher_class =~ s/^Crypt::PBKDF2::Hash:://
Truth table33%1/3
A and B
A= defined $hasher_classB= $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | croak "Can't ''encode_string'' with a hasher class outside of Crypt::PBKDF2::Hash::*"; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 511 | my $algo_string = $hasher->to_algo_string; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 511 | ▶ | $algo_string = defined($algo_string) ? "{$algo_string}" : ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 511 | return '$PBKDF2$' . "$hasher_class$algo_string:" . $self->iterations . ':' | |||||||||||||||||||||||||||||||||||||||||
| 238 | . MIME::Base64::encode($salt, "") . '$' | ||||||||||||||||||||||||||||||||||||||||||
| 239 | . MIME::Base64::encode($hash, ""); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 511 | ▶ | sub _encode_string_ldaplike { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_string_ldaplike: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 511 | my ($self, $salt, $hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 511 | my $hasher = $self->hasher; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 511 | my $hasher_class = blessed($hasher); | |||||||||||||||||||||||||||||||||||||||||
| 246 | 511 | ▶ | if (!defined $hasher_class || $hasher_class !~ s/^Crypt::PBKDF2::Hash:://) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $hasher_class and $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
MC/DC0%0/2
defined $hasher_class and $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
defined $hasher_class
$hasher_class =~ s/^Crypt::PBKDF2::Hash:://
Truth table33%1/3
A and B
A= defined $hasher_classB= $hasher_class =~ s/^Crypt::PBKDF2::Hash:://
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | croak "Can't ''encode_string'' with a hasher class outside of Crypt::PBKDF2::Hash::*"; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 511 | my $algo_string = $hasher->to_algo_string; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 511 | ▶ | $algo_string = defined($algo_string) ? "+$algo_string" : ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 511 | return '{X-PBKDF2}' . "$hasher_class$algo_string:" | |||||||||||||||||||||||||||||||||||||||||
| 254 | . $self->_b64_encode_int32($self->iterations) . ':' | ||||||||||||||||||||||||||||||||||||||||||
| 255 | . MIME::Base64::encode($salt, "") . ':' | ||||||||||||||||||||||||||||||||||||||||||
| 256 | . MIME::Base64::encode($hash, ""); | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 1022 | ▶ | sub decode_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_string: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 1022 | my ($self, $hashed) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 1022 | ▶ | if ($hashed =~ /^\$PBKDF2\$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 511 | return $self->_decode_string_cryptlike($hashed); | |||||||||||||||||||||||||||||||||||||||||
| 264 | } elsif ($hashed =~ /^\{X-PBKDF2}/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 511 | return $self->_decode_string_ldaplike($hashed); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | croak "Unrecognized hash"; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 511 | ▶ | sub _decode_string_cryptlike { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_string_cryptlike: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 511 | my ($self, $hashed) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 511 | ▶ | if ($hashed !~ /^\$PBKDF2\$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | croak "Unrecognized hash"; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 511 | ▶ | if (my ($algorithm, $opts, $iterations, $salt, $hash) = $hashed =~ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | /^\$PBKDF2\$([^:}]+)(?:\{([^}]+)\})?:(\d+):([^\$]+)\$(.*)/) { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 511 | algorithm => $algorithm, | |||||||||||||||||||||||||||||||||||||||||
| 281 | algorithm_options => $opts, | ||||||||||||||||||||||||||||||||||||||||||
| 282 | iterations => $iterations, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | salt => MIME::Base64::decode($salt), | ||||||||||||||||||||||||||||||||||||||||||
| 284 | hash => MIME::Base64::decode($hash), | ||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | croak "Invalid format"; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | 511 | ▶ | sub _decode_string_ldaplike { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_string_ldaplike: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 511 | my ($self, $hashed) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 511 | ▶ | if ($hashed !~ /^\{X-PBKDF2}/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | croak "Unrecognized hash"; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 511 | ▶ | if (my ($algo_str, $iterations, $salt, $hash) = $hashed =~ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | /^\{X-PBKDF2}([^:]+):([^:]{6}):([^\$]+):(.*)/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 511 | my ($algorithm, $opts) = split /\+/, $algo_str; | |||||||||||||||||||||||||||||||||||||||||
| 300 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 511 | algorithm => $algorithm, | |||||||||||||||||||||||||||||||||||||||||
| 302 | algorithm_options => $opts, | ||||||||||||||||||||||||||||||||||||||||||
| 303 | iterations => $self->_b64_decode_int32($iterations), | ||||||||||||||||||||||||||||||||||||||||||
| 304 | salt => MIME::Base64::decode($salt), | ||||||||||||||||||||||||||||||||||||||||||
| 305 | hash => MIME::Base64::decode($hash), | ||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | croak "Invalid format"; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 1022 | ▶ | sub hasher_from_algorithm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hasher_from_algorithm: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 1022 | my ($self, $algorithm, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 1022 | my $class = Module::Runtime::use_module("Crypt::PBKDF2::Hash::$algorithm"); | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 1022 | ▶ | if (defined $args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 1022 | return $class->from_algo_string($args); | |||||||||||||||||||||||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | return $class->new; | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 1034 | ▶ | sub clone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 1034 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 1034 | my $class = ref $self; | |||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | # If the hasher was built from hash_class and hash_args, then omit it from | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # the clone. But if it was set by the user, then we need to copy it. We're | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # assuming that the hasher has no state, so it doesn't need a deep clone. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | # This is true of all of the ones that I'm shipping, but if it's not true for | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # you, let me know. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 1034 | ▶ | my %new_args = ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | $self->has_hash_class ? (hash_class => $self->hash_class) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 337 | $self->has_hash_args ? (hash_args => $self->hash_args) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 338 | $self->has_output_len ? (output_len => $self->output_len) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 339 | $self->has_lazy_hasher ? () : (hasher => $self->hasher), | ||||||||||||||||||||||||||||||||||||||||||
| 340 | iterations => $self->iterations, | ||||||||||||||||||||||||||||||||||||||||||
| 341 | salt_len => $self->salt_len, | ||||||||||||||||||||||||||||||||||||||||||
| 342 | %params, | ||||||||||||||||||||||||||||||||||||||||||
| 343 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 1034 | return $class->new(%new_args); | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 511 | ▶ | sub _b64_encode_int32 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_b64_encode_int32: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 511 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 511 | my $b64 = MIME::Base64::encode(pack("N", $value), ""); | |||||||||||||||||||||||||||||||||||||||||
| 351 | 511 | $b64 =~ s/==$//; | |||||||||||||||||||||||||||||||||||||||||
| 352 | 511 | return $b64; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 511 | ▶ | sub _b64_decode_int32 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_b64_decode_int32: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 511 | my ($self, $b64) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 511 | $b64 .= "=="; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 511 | return unpack "N", MIME::Base64::decode($b64); | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | __END__ | ||||||||||||||||||||||||||||||||||||||||||