| 1 | package OpenSearch::Client::Hash; | ||||||||||||||||||||||||||||
| 2 | 4 | $OpenSearch::Client::Hash::VERSION = '3.009'; | |||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Crypt::URandom; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Crypt::Bcrypt 0.011; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Crypt::Argon2 0.013; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Crypt::PBKDF2 0.161520; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | has 'errorstring' => ( is => 'rwp', default => '' ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 44 | ▶ | sub _is_integer { | ||||||||||||||||||||||||||
|
Subroutine
_is_integer: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 14 | 44 | my($self, $value) = @_; | |||||||||||||||||||||||||||
| 15 | 44 | ▶ | return 0 unless defined($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 44 | ▶ | return ( $value =~ /^[1-9][0-9]*$/ ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | sub _is_integer_between { | ||||||||||||||||||||||||||
|
Subroutine
_is_integer_between: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 20 | 4 | my($self, $value, $low, $high) = @_; | |||||||||||||||||||||||||||
| 21 | 4 | ▶ | return 0 unless $self->_is_integer($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | return ( $value >= $low && $value <= $high ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value >= $low and $value <= $high
MC/DC50%1/2
$value >= $low and $value <= $high
$value >= $low
$value <= $high
Truth table66%2/3
A and B
A= $value >= $lowB= $value <= $high
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 7 | ▶ | sub create_bcrypt_password_hash { | ||||||||||||||||||||||||||
|
Subroutine
create_bcrypt_password_hash: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 7 | my($self, %options) = @_; | |||||||||||||||||||||||||||
| 27 | 7 | $self->_set_errorstring(''); | |||||||||||||||||||||||||||
| 28 | 7 | ▶ | $options{type} ||= '2y'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"type"} ||= "2y"
MC/DC100%1/1
$options{"type"} ||= "2y"
$options{"type"}
Truth table100%2/2
A
A= $options{"type"}
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | $options{rounds} ||= 12; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"rounds"} ||= 12
MC/DC100%1/1
$options{"rounds"} ||= 12
$options{"rounds"}
Truth table100%2/2
A
A= $options{"rounds"}
| |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | $options{password} ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"password"} ||= ""
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | my $salt = Crypt::URandom::urandom(16); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ## validate password | ||||||||||||||||||||||||||||
| 35 | 7 | ▶ | unless($options{password}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | return $self->_return_on_error('you must provide a password'); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 6 | my $pwdlen = length($options{password}); | |||||||||||||||||||||||||||
| 40 | 6 | ▶ | if ($pwdlen > 72 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | return $self->_return_on_error('your password is longer than 72 characters'); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | ## validate type | ||||||||||||||||||||||||||||
| 46 | 5 | $options{type} = lc($options{type}); | |||||||||||||||||||||||||||
| 47 | 5 | ▶ | unless($options{type} =~ /^2(a|b|y)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | return $self->_return_on_error(qq(invalid type '$options{type}' : valid options are 2y, 2a or 2b)); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ## validate rounds | ||||||||||||||||||||||||||||
| 52 | 4 | ▶ | unless( $self->_is_integer_between($options{rounds}, 4, 31) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 2 | return $self->_return_on_error(qq(Invalid rounds "$options{rounds}". Must be an integer between 4 and 31 inclusive.)); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ## create hash | ||||||||||||||||||||||||||||
| 57 | 2 | my $hash = Crypt::Bcrypt::bcrypt($options{password}, $options{type}, $options{rounds}, $salt); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ## validate hash | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | unless(Crypt::Bcrypt::bcrypt_check($options{password}, $hash)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | return $self->_return_on_error('failed to verify created hash'); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | if ( $hash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 2 | return $hash; | |||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 0 | return $self->_return_on_error('unexpected error creating hash'); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 13 | ▶ | sub create_argon2_password_hash { | ||||||||||||||||||||||||||
|
Subroutine
create_argon2_password_hash: called CC=18 SCAR=29.2
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 13 | my($self, %options) = @_; | |||||||||||||||||||||||||||
| 73 | 13 | $self->_set_errorstring(''); | |||||||||||||||||||||||||||
| 74 | 13 | ▶ | $options{password} ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"password"} ||= ""
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
| |||||||||||||||||||||||||||||
| 75 | 13 | ▶ | $options{type} ||= 'argon2id'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"type"} ||= "argon2id"
MC/DC100%1/1
$options{"type"} ||= "argon2id"
$options{"type"}
Truth table100%2/2
A
A= $options{"type"}
| |||||||||||||||||||||||||||||
| 76 | 13 | ▶ | $options{length} ||= 32; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"length"} ||= 32
MC/DC100%1/1
$options{"length"} ||= 32
$options{"length"}
Truth table100%2/2
A
A= $options{"length"}
| |||||||||||||||||||||||||||||
| 77 | 13 | ▶ | $options{iterations} ||= 3; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"iterations"} ||= 3
MC/DC100%1/1
$options{"iterations"} ||= 3
$options{"iterations"}
Truth table100%2/2
A
A= $options{"iterations"}
| |||||||||||||||||||||||||||||
| 78 | 13 | ▶ | $options{memory} ||= 65536; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"memory"} ||= 65536
MC/DC100%1/1
$options{"memory"} ||= 65536
$options{"memory"}
Truth table100%2/2
A
A= $options{"memory"}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 13 | my $salt = Crypt::URandom::urandom(32); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ## validate password | ||||||||||||||||||||||||||||
| 83 | 13 | ▶ | unless($options{password}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | return $self->_return_on_error('you must provide a password'); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ## validate type | ||||||||||||||||||||||||||||
| 88 | 12 | $options{type} = lc($options{type}); | |||||||||||||||||||||||||||
| 89 | 12 | ▶ | unless($options{type} =~ /\Aargon2i\Z|\Aargon2d\Z|\Aargon2id\Z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | my $err_string = qq(invalid type '$options{type}' : valid options are argon2i, argon2d or argon2id); | |||||||||||||||||||||||||||
| 91 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | ## validate iterations | ||||||||||||||||||||||||||||
| 95 | 11 | ▶ | unless($self->_is_integer($options{iterations})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | my $err_string = qq(Invalid iterations "$options{iterations}". Must be an integer); | |||||||||||||||||||||||||||
| 97 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ## validate length | ||||||||||||||||||||||||||||
| 101 | 10 | ▶ | unless($self->_is_integer($options{length})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | my $err_string = qq(Invalid length "$options{length}". Must be an integer); | |||||||||||||||||||||||||||
| 103 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | ## validate memory | ||||||||||||||||||||||||||||
| 107 | 9 | ▶ | unless($self->_is_integer($options{memory})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | my $err_string = qq(Invalid memory "$options{memory}". Must be an integer); | |||||||||||||||||||||||||||
| 109 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 8 | my $parallelism = 1; | |||||||||||||||||||||||||||
| 113 | 8 | my $m_factor = $options{memory} . 'k'; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 8 | my @cmdparams = ( $options{password}, $salt, $options{iterations}, $m_factor, $parallelism, $options{length} ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | my $hash = ''; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ## create and validate hash | ||||||||||||||||||||||||||||
| 120 | 8 | ▶ | if ($options{type} eq 'argon2id') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 121 | 6 | $hash = Crypt::Argon2::argon2id_pass(@cmdparams); | |||||||||||||||||||||||||||
| 122 | 6 | ▶ | unless(Crypt::Argon2::argon2id_verify($hash, $options{password})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | return $self->_return_on_error('failed to verify created hash'); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } elsif($options{type} eq 'argon2i') { | ||||||||||||||||||||||||||||
| 126 | 1 | $hash = Crypt::Argon2::argon2i_pass(@cmdparams); | |||||||||||||||||||||||||||
| 127 | 1 | ▶ | unless(Crypt::Argon2::argon2i_verify($hash, $options{password})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | return $self->_return_on_error('failed to verify created hash'); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } elsif($options{type} eq 'argon2d') { | ||||||||||||||||||||||||||||
| 131 | 1 | $hash = Crypt::Argon2::argon2d_pass(@cmdparams); | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | unless(Crypt::Argon2::argon2d_verify($hash, $options{password})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | return $self->_return_on_error('failed to verify created hash'); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 8 | ▶ | if ( $hash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 8 | return $hash; | |||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | 0 | return $self->_return_on_error('unexpected error creating hash'); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 8 | ▶ | sub create_pbkdf2_password_hash { | ||||||||||||||||||||||||||
|
Subroutine
create_pbkdf2_password_hash: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 8 | my($self, %options) = @_; | |||||||||||||||||||||||||||
| 146 | 8 | $self->_set_errorstring(''); | |||||||||||||||||||||||||||
| 147 | 8 | ▶ | $options{password} ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"password"} ||= ""
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
| |||||||||||||||||||||||||||||
| 148 | 8 | ▶ | $options{length} ||= 256; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"length"} ||= 256
MC/DC100%1/1
$options{"length"} ||= 256
$options{"length"}
Truth table100%2/2
A
A= $options{"length"}
| |||||||||||||||||||||||||||||
| 149 | 8 | ▶ | $options{iterations} ||= 600000; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"iterations"} ||= 600000
MC/DC100%1/1
$options{"iterations"} ||= 600000
$options{"iterations"}
Truth table100%2/2
A
A= $options{"iterations"}
| |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | $options{function} ||= 'SHA256'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"function"} ||= "SHA256"
MC/DC100%1/1
$options{"function"} ||= "SHA256"
$options{"function"}
Truth table100%2/2
A
A= $options{"function"}
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 8 | my $funcmap = { | |||||||||||||||||||||||||||
| 153 | 'SHA1' => { number => 1, class => 'HMACSHA1', size => 0 }, | ||||||||||||||||||||||||||||
| 154 | 'SHA224' => { number => 2, class => 'HMACSHA2', size => 224 }, | ||||||||||||||||||||||||||||
| 155 | 'SHA256' => { number => 3, class => 'HMACSHA2', size => 256 }, | ||||||||||||||||||||||||||||
| 156 | 'SHA384' => { number => 4, class => 'HMACSHA2', size => 384 }, | ||||||||||||||||||||||||||||
| 157 | 'SHA512' => { number => 5, class => 'HMACSHA2', size => 512 }, | ||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | ## validate password | ||||||||||||||||||||||||||||
| 161 | 8 | ▶ | unless($options{password}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | return $self->_return_on_error('you must provide a password'); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | ## validate function | ||||||||||||||||||||||||||||
| 166 | 7 | ▶ | unless(exists($funcmap->{$options{function}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 1 | my @allowedoptions = ( sort keys %$funcmap ); | |||||||||||||||||||||||||||
| 168 | 1 | my $err_string = qq('$options{function}' is not a valid function. Valid functions are ) . join(', ', @allowedoptions); | |||||||||||||||||||||||||||
| 169 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | ## validate length | ||||||||||||||||||||||||||||
| 173 | 6 | ▶ | unless($self->_is_integer($options{length})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | my $err_string = qq(Invalid length "$options{length}". Must be an integer); | |||||||||||||||||||||||||||
| 175 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 5 | ▶ | if ( $options{length} % 8 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | my $err_string = qq(A length of '$options{length}' bits cannot be encoded as bytes. Use a multiple of 8 ( 128, 224, 256, 384, 512 etc)); | |||||||||||||||||||||||||||
| 180 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | ## validate iterations | ||||||||||||||||||||||||||||
| 184 | 4 | ▶ | unless($self->_is_integer($options{iterations})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | my $err_string = qq(Invalid iterations "$options{iterations}". Must be an integer); | |||||||||||||||||||||||||||
| 186 | 1 | return $self->_return_on_error($err_string); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | ## create hash | ||||||||||||||||||||||||||||
| 190 | 3 | my $salt = Crypt::URandom::urandom(128); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 3 | my $base64salt = MIME::Base64::encode_base64($salt, ''); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 3 | my $mappedfunc = $funcmap->{$options{function}}; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 3 | my $bytelen = int($options{length} / 8); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | my %hasherparams = ( | ||||||||||||||||||||||||||||
| 199 | 'hash_class' => $mappedfunc->{class}, | ||||||||||||||||||||||||||||
| 200 | 'iterations' => $options{iterations}, | ||||||||||||||||||||||||||||
| 201 | 3 | 'output_len' => $bytelen, | |||||||||||||||||||||||||||
| 202 | 'salt_len' => 128, | ||||||||||||||||||||||||||||
| 203 | 'encoding' => 'crypt', | ||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | ▶ | if ($mappedfunc->{size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 2 | $hasherparams{hash_args} = { 'sha_size' => $mappedfunc->{size} }; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 3 | my $hasher = Crypt::PBKDF2->new(%hasherparams); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 3 | my $basehash = $hasher->PBKDF2_base64($salt, $options{password}); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 3 | my $keyprefix = ''; | |||||||||||||||||||||||||||
| 215 | ## 32 bit perl with 32 bit int needs bigint | ||||||||||||||||||||||||||||
| 216 | { | ||||||||||||||||||||||||||||
| 217 | 4 | ▶ | use bigint; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 218 | 3 | my $prefix_bigint = ( $options{iterations} * (2 ** 32) ) + $options{length}; | |||||||||||||||||||||||||||
| 219 | 3 | $keyprefix .= $prefix_bigint; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | my $hash = sprintf('$%s$%s$%s$%s', | ||||||||||||||||||||||||||||
| 223 | 3 | $mappedfunc->{number}, $keyprefix, $base64salt, $basehash | |||||||||||||||||||||||||||
| 224 | ); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | ## validate hash | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 3 | my $generated; | |||||||||||||||||||||||||||
| 229 | 3 | ▶ | if ($mappedfunc->{class} eq 'HMACSHA1' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 1 | $generated = sprintf('$PBKDF2$HMACSHA1:%s:%s$%s', $options{iterations}, $base64salt, $basehash ); | |||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||
| 232 | 2 | $generated = sprintf('$PBKDF2$HMACSHA2{%s}:%s:%s$%s', $mappedfunc->{size}, $options{iterations}, $base64salt, $basehash ); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 3 | ▶ | unless($hasher->validate($generated, $options{password})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | return $self->_return_on_error('failed to verify created hash'); | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 3 | ▶ | if ( $hash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 3 | return $hash; | |||||||||||||||||||||||||||
| 241 | } else { | ||||||||||||||||||||||||||||
| 242 | 0 | return $self->_return_on_error('unexpected error creating hash'); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 15 | ▶ | sub _return_on_error { | ||||||||||||||||||||||||||
|
Subroutine
_return_on_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 247 | 15 | my ($self, $errmsg) = @_; | |||||||||||||||||||||||||||
| 248 | 15 | $self->_set_errorstring($errmsg); | |||||||||||||||||||||||||||
| 249 | 15 | return ''; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 4 | 1; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | __END__ | ||||||||||||||||||||||||||||