blib/lib/OpenSearch/Client/Hash.pm

filter: Statementstmt 80.5% 91 / 113 Branchbran 54.1% 26 / 48 Conditioncond 41.6% 20 / 48 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 60.4% 150 / 248 SCAR 48.5
CC
45
Cov
66%
create_argon2_password_hash
41.7
create_pbkdf2_password_hash
26.7
create_bcrypt_password_hash
22.8
CRAP 127.8
↑ Summary
1 package OpenSearch::Client::Hash;
2 4 $OpenSearch::Client::Hash::VERSION = '3.007010';
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 sub create_bcrypt_password_hash {
12 1 ▶ my($self, %options) = @_;
Subroutine create_bcrypt_password_hash: called CC=12 SCAR=31.3
Pod documented
13 1 ▶ $options{type} ||= '2y';
Condition50%1/2
$options{"type"} ||= "2y"
l!l
01
MC/DC0%0/1
$options{"type"} ||= "2y"
$options{"type"}
Truth table50%1/2
A
A= $options{"type"}
Aresult
11
00
14 1 ▶ $options{rounds} ||= 12;
Condition50%1/2
$options{"rounds"} ||= 12
l!l
01
MC/DC0%0/1
$options{"rounds"} ||= 12
$options{"rounds"}
Truth table50%1/2
A
A= $options{"rounds"}
Aresult
11
00
15 1 ▶ $options{password} ||= '';
Condition50%1/2
$options{"password"} ||= ""
l!l
10
MC/DC0%0/1
$options{"password"} ||= ""
$options{"password"}
Truth table50%1/2
A
A= $options{"password"}
Aresult
11
00
16
17 1 my $salt = Crypt::URandom::urandom(16);
18
19 1 ▶ unless($options{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"password"}) 0 1 1
20 0 die 'you must provide a password';
21 }
22 {
23 1 my $pwdlen = length($options{password});
24 1 ▶ if ($pwdlen > 72 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pwdlen > 72) 0 1 1
25 0 die 'your password is longer than 72 characters';
26 }
27 }
28
29 1 $options{type} = lc($options{type});
30 1 ▶ unless($options{type} =~ /^2(a|b|y)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"type"} =~ /^2(a|b|y)$/) 0 1 1
31 0 die qq(invalid type '$options{type}' : valid options are 2y, 2a or 2b);
32 }
33
34 1 ▶ unless($options{rounds}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"rounds"} and $options{"rounds"} =~ /^[1-9][0-9]*$/ and $options{"rounds"} >= 4 and $options{"rounds"} <= 31) 0 1 1
Condition33%3/9
$options{"rounds"} and $options{"rounds"} =~ /^[1-9][0-9]*$/ and $options{"rounds"} >= 4 and $options{"rounds"} <= 31
!ll&&!rl&&r
001
$options{"rounds"} and $options{"rounds"} =~ /^[1-9][0-9]*$/ and $options{"rounds"} >= 4
!ll&&!rl&&r
001
$options{"rounds"} and $options{"rounds"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
001
MC/DC0%0/4
$options{"rounds"} and $options{"rounds"} =~ /^[1-9][0-9]*$/ and $options{"rounds"} >= 4 and $options{"rounds"} <= 31
$options{"rounds"} $options{"rounds"} =~ /^[1-9][0-9]*$/ $options{"rounds"} >= 4 $options{"rounds"} <= 31
Truth table20%1/5
A and B and C and D
A= $options{"rounds"}B= $options{"rounds"} =~ /^[1-9][0-9]*$/C= $options{"rounds"} >= 4D= $options{"rounds"} <= 31
ABCDresult
0XXX0
10XX0
110X0
11100
11111
35 && $options{rounds} =~ /^[1-9][0-9]*$/
36 && $options{rounds} >= 4
37 && $options{rounds} <= 31
38 ) {
39 0 die qq(Invalid rounds "$options{rounds}". Must be an integer between 4 and 31 inclusive.);
40 }
41
42 1 my $hash = Crypt::Bcrypt::bcrypt($options{password}, $options{type}, $options{rounds}, $salt);
43 1 ▶ unless(Crypt::Bcrypt::bcrypt_check($options{password}, $hash)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Crypt::Bcrypt::bcrypt_check($options{"password"}, $hash)) 0 1 1
44 0 die 'failed to verify created hash';
45 }
46 1 return $hash;
47 }
48  
49 sub create_argon2_password_hash {
50 3 ▶ my($self, %options) = @_;
Subroutine create_argon2_password_hash: called CC=20 SCAR=37.3
Pod documented
51 3 ▶ $options{password} ||= '';
Condition50%1/2
$options{"password"} ||= ""
l!l
30
MC/DC0%0/1
$options{"password"} ||= ""
$options{"password"}
Truth table50%1/2
A
A= $options{"password"}
Aresult
11
00
52 3 ▶ $options{type} ||= 'argon2id';
Condition50%1/2
$options{"type"} ||= "argon2id"
l!l
30
MC/DC0%0/1
$options{"type"} ||= "argon2id"
$options{"type"}
Truth table50%1/2
A
A= $options{"type"}
Aresult
11
00
53 3 ▶ $options{length} ||= 32;
Condition50%1/2
$options{"length"} ||= 32
l!l
03
MC/DC0%0/1
$options{"length"} ||= 32
$options{"length"}
Truth table50%1/2
A
A= $options{"length"}
Aresult
11
00
54 3 ▶ $options{iterations} ||= 3;
Condition50%1/2
$options{"iterations"} ||= 3
l!l
03
MC/DC0%0/1
$options{"iterations"} ||= 3
$options{"iterations"}
Truth table50%1/2
A
A= $options{"iterations"}
Aresult
11
00
55 3 ▶ $options{memory} ||= 65536;
Condition50%1/2
$options{"memory"} ||= 65536
l!l
03
MC/DC0%0/1
$options{"memory"} ||= 65536
$options{"memory"}
Truth table50%1/2
A
A= $options{"memory"}
Aresult
11
00
56
57 3 my $salt = Crypt::URandom::urandom(32);
58
59 3 ▶ unless($options{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"password"}) 0 3 3
60 0 die 'you must provide a password';
61 }
62
63 3 $options{type} = lc($options{type});
64
65 3 ▶ unless($options{type} =~ /\Aargon2i\Z|\Aargon2d\Z|\Aargon2id\Z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"type"} =~ /\Aargon2i\Z|\Aargon2d\Z|\Aargon2id\Z/) 0 3 3
66 0 die qq(invalid type '$options{type}' : valid options are argon2i, argon2d or argon2id);
67 }
68
69 3 ▶ unless($options{iterations}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/) 0 3 3
Condition33%1/3
$options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
003
MC/DC0%0/2
$options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/
$options{"iterations"} $options{"iterations"} =~ /^[1-9][0-9]*$/
Truth table33%1/3
A and B
A= $options{"iterations"}B= $options{"iterations"} =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
70 && $options{iterations} =~ /^[1-9][0-9]*$/
71 ) {
72 0 die qq(Invalid iterations "$options{iterations}". Must be an integer);
73 }
74
75 3 ▶ unless($options{length}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/) 0 3 3
Condition33%1/3
$options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
003
MC/DC0%0/2
$options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/
$options{"length"} $options{"length"} =~ /^[1-9][0-9]*$/
Truth table33%1/3
A and B
A= $options{"length"}B= $options{"length"} =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
76 && $options{length} =~ /^[1-9][0-9]*$/
77 ) {
78 0 die qq(Invalid length "$options{length}". Must be an integer);
79 }
80
81 3 ▶ unless($options{memory}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"memory"} and $options{"memory"} =~ /^[1-9][0-9]*$/) 0 3 3
Condition33%1/3
$options{"memory"} and $options{"memory"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
003
MC/DC0%0/2
$options{"memory"} and $options{"memory"} =~ /^[1-9][0-9]*$/
$options{"memory"} $options{"memory"} =~ /^[1-9][0-9]*$/
Truth table33%1/3
A and B
A= $options{"memory"}B= $options{"memory"} =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
82 && $options{memory} =~ /^[1-9][0-9]*$/
83 ) {
84 0 die qq(Invalid memory "$options{memory}". Must be an integer);
85 }
86
87 3 my $parallelism = 1;
88 3 my $m_factor = $options{memory} . 'k';
89
90 3 my @cmdparams = ( $options{password}, $salt, $options{iterations}, $m_factor, $parallelism, $options{length} );
91
92 3 my $hash = '';
93
94 3 ▶ if ($options{type} eq 'argon2id') {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'type'} eq 'argon2id') { } 1 2 3
elsif ($options{'type'} eq 'argon2i') { } 1 1 2
elsif ($options{'type'} eq 'argon2d') { } 1 0 1
95 1 $hash = Crypt::Argon2::argon2id_pass(@cmdparams);
96 1 ▶ unless(Crypt::Argon2::argon2id_verify($hash, $options{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2id_verify($hash, $options{"password"})) 0 1 1
97 0 die 'failed to verify created hash';
98 }
99 } elsif($options{type} eq 'argon2i') {
100 1 $hash = Crypt::Argon2::argon2i_pass(@cmdparams);
101 1 ▶ unless(Crypt::Argon2::argon2i_verify($hash, $options{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2i_verify($hash, $options{"password"})) 0 1 1
102 0 die 'failed to verify created hash';
103 }
104 } elsif($options{type} eq 'argon2d') {
105 1 $hash = Crypt::Argon2::argon2d_pass(@cmdparams);
106 1 ▶ unless(Crypt::Argon2::argon2d_verify($hash, $options{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Crypt::Argon2::argon2d_verify($hash, $options{"password"})) 0 1 1
107 0 die 'failed to verify created hash';
108 }
109 }
110
111 3 return $hash;
112 }
113  
114 sub create_pbkdf2_password_hash {
115 1 ▶ my($self, %options) = @_;
Subroutine create_pbkdf2_password_hash: called CC=15 SCAR=32.8
Pod documented
116 1 ▶ $options{password} ||= '';
Condition50%1/2
$options{"password"} ||= ""
l!l
10
MC/DC0%0/1
$options{"password"} ||= ""
$options{"password"}
Truth table50%1/2
A
A= $options{"password"}
Aresult
11
00
117 1 ▶ $options{length} ||= 256;
Condition50%1/2
$options{"length"} ||= 256
l!l
01
MC/DC0%0/1
$options{"length"} ||= 256
$options{"length"}
Truth table50%1/2
A
A= $options{"length"}
Aresult
11
00
118 1 ▶ $options{iterations} ||= 600000;
Condition50%1/2
$options{"iterations"} ||= 600000
l!l
01
MC/DC0%0/1
$options{"iterations"} ||= 600000
$options{"iterations"}
Truth table50%1/2
A
A= $options{"iterations"}
Aresult
11
00
119 1 ▶ $options{function} ||= 'SHA256';
Condition50%1/2
$options{"function"} ||= "SHA256"
l!l
01
MC/DC0%0/1
$options{"function"} ||= "SHA256"
$options{"function"}
Truth table50%1/2
A
A= $options{"function"}
Aresult
11
00
120
121 1 my $funcmap = {
122 'SHA1' => { number => 1, class => 'HMACSHA1', size => 0 },
123 'SHA224' => { number => 2, class => 'HMACSHA2', size => 224 },
124 'SHA256' => { number => 3, class => 'HMACSHA2', size => 256 },
125 'SHA384' => { number => 4, class => 'HMACSHA2', size => 384 },
126 'SHA512' => { number => 5, class => 'HMACSHA2', size => 512 },
127 };
128
129 1 ▶ unless($options{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"password"}) 0 1 1
130 0 die 'you must provide a password';
131 }
132
133 1 ▶ unless(exists($funcmap->{$options{function}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$funcmap{$options{"function"}}) 0 1 1
134 0 my @allowedoptions = ( sort keys %$funcmap );
135 0 my $msg = qq('$options{function}' is not a valid function. Valid functions are ) . join(', ', @allowedoptions);
136 0 die $msg;
137 }
138
139 1 ▶ unless($options{length}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/) 0 1 1
Condition33%1/3
$options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
001
MC/DC0%0/2
$options{"length"} and $options{"length"} =~ /^[1-9][0-9]*$/
$options{"length"} $options{"length"} =~ /^[1-9][0-9]*$/
Truth table33%1/3
A and B
A= $options{"length"}B= $options{"length"} =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
140 && $options{length} =~ /^[1-9][0-9]*$/
141 ) {
142 0 die qq(Invalid length "$options{length}". Must be an integer);
143 }
144
145 1 ▶ if ( $options{length} % 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"length"} % 8) 0 1 1
146 0 die qq(A length of '$options{length}' bits cannot be encoded as bytes. Use a multiple of 8 ( 128, 256, 384, 512 etc) );
147 }
148
149 1 ▶ unless($options{iterations}
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/) 0 1 1
Condition33%1/3
$options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
001
MC/DC0%0/2
$options{"iterations"} and $options{"iterations"} =~ /^[1-9][0-9]*$/
$options{"iterations"} $options{"iterations"} =~ /^[1-9][0-9]*$/
Truth table33%1/3
A and B
A= $options{"iterations"}B= $options{"iterations"} =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
150 && $options{iterations} =~ /^[1-9][0-9]*$/
151 ) {
152 0 die qq(Invalid iterations "$options{iterations}". Must be an integer);
153 }
154
155 1 my $salt = Crypt::URandom::urandom(128);
156
157 1 my $base64salt = MIME::Base64::encode_base64($salt, '');
158
159 1 my $mappedfunc = $funcmap->{$options{function}};
160
161 1 my $bytelen = int($options{length} / 8);
162
163 my %hasherparams = (
164 'hash_class' => $mappedfunc->{class},
165 'iterations' => $options{iterations},
166 1 'output_len' => $bytelen,
167 'salt_len' => 128,
168 'encoding' => 'crypt',
169 );
170
171 1 ▶ if ($mappedfunc->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mappedfunc->{"size"}) 1 0 1
172 1 $hasherparams{hash_args} = { 'sha_size' => $mappedfunc->{size} };
173 }
174
175 1 my $hasher = Crypt::PBKDF2->new(%hasherparams);
176
177 1 my $basehash = $hasher->PBKDF2_base64($salt, $options{password});
178
179 1 my $keynum = $options{iterations} << 32;
180 1 $keynum |= $options{length};
181
182 my $hash = sprintf('$%s$%s$%s$%s',
183 1 $mappedfunc->{number}, $keynum, $base64salt, $basehash
184 );
185
186 ## and validate
187
188 1 my $generated;
189 1 ▶ if ($mappedfunc->{class} eq 'HMACSHA1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mappedfunc->{'class'} eq 'HMACSHA1') { } 0 1 1
190 0 $generated = sprintf('$PBKDF2$HMACSHA1:%s:%s$%s', $options{iterations}, $base64salt, $basehash );
191 } else {
192 1 $generated = sprintf('$PBKDF2$HMACSHA2{%s}:%s:%s$%s', $mappedfunc->{size}, $options{iterations}, $base64salt, $basehash );
193 }
194
195 1 ▶ unless($hasher->validate($generated, $options{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hasher->validate($generated, $options{"password"})) 0 1 1
196 0 die 'failed to verify created hash';
197 }
198
199 1 return $hash;
200
201 }
202  
203 4 1;
204  
205 __END__
↑ Summary