blib/lib/OpenSearch/Client/Hash.pm

filter: Statementstmt 94.4% 136 / 144 Branchbran 83.8% 52 / 62 Conditioncond 96.3% 26 / 27 MC/DCmcdc 92.8% 13 / 14 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 92.4% 244 / 264 CC 45 SCAR 24.4
CC
45
Cov
92%
create_argon2_password_hash
18.6
create_pbkdf2_password_hash
14.0
create_bcrypt_password_hash
10.1
CRAP 11.5
↑ Summary
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
BranchTrueFalseTotal
unless defined $value 0 44 44
16 44 ▶ return ( $value =~ /^[1-9][0-9]*$/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^[1-9][0-9]*$/ ? : 38 6 44
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
BranchTrueFalseTotal
unless $self->_is_integer($value) 1 3 4
22 3 ▶ return ( $value >= $low && $value <= $high ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value >= $low && $value <= $high ? : 2 1 3
Condition66%2/3
$value >= $low and $value <= $high
!ll&&!rl&&r
102
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
ABresult
0X0
100
111
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"
l!l
16
MC/DC100%1/1
$options{"type"} ||= "2y"
$options{"type"}
Truth table100%2/2
A
A= $options{"type"}
Aresult
11
00
29 7 ▶ $options{rounds} ||= 12;
Condition100%2/2
$options{"rounds"} ||= 12
l!l
34
MC/DC100%1/1
$options{"rounds"} ||= 12
$options{"rounds"}
Truth table100%2/2
A
A= $options{"rounds"}
Aresult
11
00
30 7 ▶ $options{password} ||= '';
Condition100%2/2
$options{"password"} ||= ""
l!l
61
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
Aresult
11
00
31
32 7 my $salt = Crypt::URandom::urandom(16);
33
34 ## validate password
35 7 ▶ unless($options{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"password"}) 1 6 7
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
BranchTrueFalseTotal
if ($pwdlen > 72) 1 5 6
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
BranchTrueFalseTotal
unless ($options{"type"} =~ /^2(a|b|y)$/) 1 4 5
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
BranchTrueFalseTotal
unless ($self->_is_integer_between($options{"rounds"}, 4, 31)) 2 2 4
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
BranchTrueFalseTotal
unless (Crypt::Bcrypt::bcrypt_check($options{"password"}, $hash)) 0 2 2
61 0 return $self->_return_on_error('failed to verify created hash');
62 }
63
64 2 ▶ if ( $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash) { } 2 0 2
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"} ||= ""
l!l
121
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
Aresult
11
00
75 13 ▶ $options{type} ||= 'argon2id';
Condition100%2/2
$options{"type"} ||= "argon2id"
l!l
49
MC/DC100%1/1
$options{"type"} ||= "argon2id"
$options{"type"}
Truth table100%2/2
A
A= $options{"type"}
Aresult
11
00
76 13 ▶ $options{length} ||= 32;
Condition100%2/2
$options{"length"} ||= 32
l!l
211
MC/DC100%1/1
$options{"length"} ||= 32
$options{"length"}
Truth table100%2/2
A
A= $options{"length"}
Aresult
11
00
77 13 ▶ $options{iterations} ||= 3;
Condition100%2/2
$options{"iterations"} ||= 3
l!l
211
MC/DC100%1/1
$options{"iterations"} ||= 3
$options{"iterations"}
Truth table100%2/2
A
A= $options{"iterations"}
Aresult
11
00
78 13 ▶ $options{memory} ||= 65536;
Condition100%2/2
$options{"memory"} ||= 65536
l!l
310
MC/DC100%1/1
$options{"memory"} ||= 65536
$options{"memory"}
Truth table100%2/2
A
A= $options{"memory"}
Aresult
11
00
79
80 13 my $salt = Crypt::URandom::urandom(32);
81
82 ## validate password
83 13 ▶ unless($options{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"password"}) 1 12 13
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
BranchTrueFalseTotal
unless ($options{"type"} =~ /\Aargon2i\Z|\Aargon2d\Z|\Aargon2id\Z/) 1 11 12
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
BranchTrueFalseTotal
unless ($self->_is_integer($options{"iterations"})) 1 10 11
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
BranchTrueFalseTotal
unless ($self->_is_integer($options{"length"})) 1 9 10
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
BranchTrueFalseTotal
unless ($self->_is_integer($options{"memory"})) 1 8 9
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
BranchTrueFalseTotal
if ($options{'type'} eq 'argon2id') { } 6 2 8
elsif ($options{'type'} eq 'argon2i') { } 1 1 2
elsif ($options{'type'} eq 'argon2d') { } 1 0 1
121 6 $hash = Crypt::Argon2::argon2id_pass(@cmdparams);
122 6 ▶ unless(Crypt::Argon2::argon2id_verify($hash, $options{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2id_verify($hash, $options{"password"})) 0 6 6
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
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2i_verify($hash, $options{"password"})) 0 1 1
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
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2d_verify($hash, $options{"password"})) 0 1 1
133 0 return $self->_return_on_error('failed to verify created hash');
134 }
135 }
136
137 8 ▶ if ( $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash) { } 8 0 8
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"} ||= ""
l!l
71
MC/DC100%1/1
$options{"password"} ||= ""
$options{"password"}
Truth table100%2/2
A
A= $options{"password"}
Aresult
11
00
148 8 ▶ $options{length} ||= 256;
Condition100%2/2
$options{"length"} ||= 256
l!l
44
MC/DC100%1/1
$options{"length"} ||= 256
$options{"length"}
Truth table100%2/2
A
A= $options{"length"}
Aresult
11
00
149 8 ▶ $options{iterations} ||= 600000;
Condition100%2/2
$options{"iterations"} ||= 600000
l!l
35
MC/DC100%1/1
$options{"iterations"} ||= 600000
$options{"iterations"}
Truth table100%2/2
A
A= $options{"iterations"}
Aresult
11
00
150 8 ▶ $options{function} ||= 'SHA256';
Condition100%2/2
$options{"function"} ||= "SHA256"
l!l
35
MC/DC100%1/1
$options{"function"} ||= "SHA256"
$options{"function"}
Truth table100%2/2
A
A= $options{"function"}
Aresult
11
00
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
BranchTrueFalseTotal
unless ($options{"password"}) 1 7 8
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
BranchTrueFalseTotal
unless (exists $$funcmap{$options{"function"}}) 1 6 7
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
BranchTrueFalseTotal
unless ($self->_is_integer($options{"length"})) 1 5 6
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
BranchTrueFalseTotal
if ($options{"length"} % 8) 1 4 5
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
BranchTrueFalseTotal
unless ($self->_is_integer($options{"iterations"})) 1 3 4
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
BranchTrueFalseTotal
if ($mappedfunc->{"size"}) 2 1 3
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
BranchTrueFalseTotal
if ($mappedfunc->{'class'} eq 'HMACSHA1') { } 1 2 3
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
BranchTrueFalseTotal
unless ($hasher->validate($generated, $options{"password"})) 0 3 3
236 0 return $self->_return_on_error('failed to verify created hash');
237 }
238
239 3 ▶ if ( $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash) { } 3 0 3
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__
↑ Summary