blib/lib/Authen/Passphrase/Argon2.pm

filter: Statementstmt 100% 95 / 95 Branchbran 100% 42 / 42 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 33 / 33 Podpod 100% 15 / 15 totaltotal 100% 194 / 194 CC 29 SCAR 16.2
CC
29
Cov
100%
new
15.0
_hash_of
4.0
__ANON__
3.0
CRAP 5.0
↑ Summary
1 package Authen::Passphrase::Argon2;
2  
3 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Crypt::Argon2 qw/argon2id_pass argon2id_verify/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Syntax::Construct qw( ?<> /a );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use parent 'Authen::Passphrase';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $VERSION = '1.01';
15  
16 6 our (%salts, %hashes, @argons);
17 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 @argons = (
19 ['salt'],
20 [qw/cost 3/],
21 [qw/factor 32M/],
22 [qw/parallelism 1/],
23 [qw/size 16/]
24 );
25 %salts = (
26 17 ▶ salt => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
27 17 ▶ $_[0] = $salts{salt_random}() if "$_[0]" eq 'random';
Branch: 1 branch
BranchTrueFalseTotal
if "$_[0]" eq "random" 1 16 17
28 17 ▶ $_[0] =~ m#\A[\x00-\xff]{6}.*\z#
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\A[\x00-\xff]{6}.*\z/u or croak(sprintf('%s is not a valid raw salt', $_[0])) 14 3 17
29 or croak sprintf("%s is not a valid raw salt", $_[0]);
30 14 $_[0];
31 },
32 2 ▶ salt_hex => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 2 $_[0] = $salts{salt}($_[0]);
34 2 unpack("H*", $_[0]);
35 },
36 2 ▶ salt_base64 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 $_[0] = $salts{salt}($_[0]);
38 2 encode_base64($_[0]);
39 },
40 1 ▶ salt_random => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 Data::GUID->new->as_string;
42 },
43 6 );
44 %hashes = (
45 15 ▶ hash => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 15 ▶ $_[0] =~ m/^
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^ \$argon2id \$v=\d+ \$m=(?<factor>\d+), t=(?<cost>\d+), p=(?<parallelism>\d+) \$ /ax or croak(sprintf('not a valid raw hash - %s', $_[0])) 11 4 15
47 \$argon2id
48 \$v=\d+
49 \$m=(?<factor>\d+),
50 t=(?<cost>\d+),
51 p=(?<parallelism>\d+)
52 \$
53 /ax or croak sprintf "not a valid raw hash - %s", $_[0];
54 11 ("$_[0]", %+);
55 },
56 5 ▶ hash_base64 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 5 ($_[0]) = $hashes{hash}($_[0]);
58 4 encode_base64($_[0]);
59 },
60 5 ▶ hash_hex => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 5 ($_[0]) = $hashes{hash}($_[0]);
62 4 unpack("H*", $_[0]);
63 }
64 6 );
65 }
66  
67 22 ▶sub new {
Subroutine new: called CC=15 SCAR=27.1
Pod documented
68 22 ▶ my ($class, %args) = (shift, (scalar @_ == 1 ? %{$_[0]} : @_));
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 1 21 22
69  
70 22 my $self = bless({ algorithm => 'Argon2' }, $class);
71  
72 22 ▶ $args{crypt} = delete $args{passphrase} if $args{passphrase};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"passphrase"} 6 16 22
73  
74 exists $args{$_} ? ! $self->{salt}
75 6 ? do { $self->{salt} = $salts{$_}->($args{$_}) }
76 : croak sprintf "salt specified redundantly - %s", $_
77 22 ▶ : next foreach (keys %salts);
Branch: 2 branches
BranchTrueFalseTotal
exists $args{$_} ? : 7 81 88
!$self->{'salt'} ? : 6 1 7
78  
79 21 foreach (qw/hash base64 hex/) {
80 63 ▶ if (exists $args{"stored_$_"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"stored_$_"}) 1 62 63
81 1 ($args{"hash_$_"}) = delete $args{crypt};
82 1 last;
83 }
84 }
85  
86 $self->{$_->[0]} = $self->{$_->[0]}
87 || $args{$_->[0]}
88 || $_->[1]
89 21 ▶ foreach @argons;
Condition100%6/6
$$self{$_->[0]} || $args{$_->[0]} || $_->[1]
l!l&&r!l&&!r
117816
$$self{$_->[0]} or $args{$_->[0]}
l!l&&r!l&&!r
5694
MC/DC100%3/3
$$self{$_->[0]} || $args{$_->[0]} || $_->[1]
$$self{$_->[0]} $args{$_->[0]} $_->[1]
Truth table100%4/4
A or B || C
A= $$self{$_->[0]}B= $args{$_->[0]}C= $_->[1]
ABCresult
1XX1
01X1
0011
0000
90  
91 21 ▶ unless ($args{crypt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'crypt'}) { } 5 16 21
92 exists $args{$_} ? ! $self->{crypt}
93 ? $self->$_($args{$_})
94 : croak sprintf "hash specified redundantly - %s", $_
95 16 ▶ : next foreach (keys %hashes);
Branch: 2 branches
BranchTrueFalseTotal
exists $args{$_} ? : 3 45 48
!$self->{'crypt'} ? : 2 1 3
96 } else {
97 5 $self->{crypt} = $self->_hash_of($args{crypt});
98 }
99  
100 20 return $self;
101 }
102  
103 10 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
104 10 my ($self, $passphrase) = @_;
105  
106 10 return argon2id_verify($self->{crypt}, $passphrase);
107 }
108  
109 3 ▶sub from_crypt {
Subroutine from_crypt: called CC=1 SCAR=0.0
Pod documented
110 3 my ($self, $crypt, $info) = (@_, {});
111 3 return $self->new(%{ $info }, 'passphrase', $hashes{hash}($crypt));
112 }
113  
114 2 ▶sub from_rfc2307 {
Subroutine from_rfc2307: called CC=2 SCAR=6.9
Pod documented
115 2 my ( $class, $rfc2307, $info) = (@_, {});
116  
117 2 ▶ croak "invalid Argon2 RFC2307 format" unless $rfc2307 =~ m/^{ARGON2}(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rfc2307 =~ /^{ARGON2}(.*)$/u 1 1 2
118  
119 1 return $class->from_crypt($1, $info);
120 }
121  
122 16 ▶sub as_crypt {
Subroutine as_crypt: called CC=2 SCAR=6.9
Pod documented
123 16 my ($self, $val) = @_;
124  
125 16 ▶ $self->{crypt} = $self->_hash_of($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 7 9 16
126  
127 16 return $self->{crypt};
128 }
129  
130 1 ▶sub as_hex {
Subroutine as_hex: called CC=1 SCAR=0.0
Pod documented
131 1 goto &hash_hex;
132 }
133  
134 1 ▶sub as_base64 {
Subroutine as_base64: called CC=1 SCAR=0.0
Pod documented
135 1 goto &hash_base64;
136 }
137  
138 2 ▶sub as_rfc2307 {
Subroutine as_rfc2307: called CC=1 SCAR=0.0
Pod documented
139 2 my ($self, $val) = @_;
140  
141 2 return '{ARGON2}' . $self->as_crypt($val);
142 }
143  
144 2 ▶sub algorithm {
Subroutine algorithm: called CC=1 SCAR=0.0
Pod documented
145 2 my($self) = @_;
146  
147 2 return $self->{algorithm};
148 }
149  
150 16 ▶sub salt {
Subroutine salt: called CC=2 SCAR=6.9
Pod documented
151 16 my($self, $val) = @_;
152 16 ▶ $self->{salt} = $salts{salt}($val) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 7 9 16
153 13 $self->{salt};
154 }
155  
156 3 ▶sub salt_hex {
Subroutine salt_hex: called CC=2 SCAR=6.9
Pod documented
157 3 my($self, $val) = @_;
158 3 ▶ $salts{salt_hex}($self->salt($val ? pack("H*", $val) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 1 3
159 }
160  
161 3 ▶sub salt_base64 {
Subroutine salt_base64: called CC=2 SCAR=6.9
Pod documented
162 3 my($self, $val) = @_;
163 3 ▶ $salts{salt_base64}($self->salt($val ? decode_base64($val) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 1 3
164 }
165  
166 8 ▶sub hash {
Subroutine hash: called CC=2 SCAR=6.9
Pod documented
167 8 my($self, $val) = @_;
168 8 ▶ return $val ? $self->as_crypt($hashes{hash}($val)) : $self->{crypt};
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 6 8
169 }
170  
171 5 ▶sub hash_hex {
Subroutine hash_hex: called CC=2 SCAR=6.9
Pod documented
172 5 my($self, $val) = @_;
173 5 ▶ return $hashes{hash_hex}($self->as_crypt($val ? pack("H*", $val) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 3 2 5
174 }
175  
176 5 ▶sub hash_base64 {
Subroutine hash_base64: called CC=2 SCAR=6.9
Pod documented
177 5 my($self, $val) = @_;
178 5 ▶ return $hashes{hash_base64}($self->as_crypt($val ? decode_base64($val) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 3 2 5
179 }
180  
181 13 ▶sub _hash_of {
Subroutine _hash_of: called CC=4 SCAR=13.9
182 13 my ($self, $pass, @params) = @_;
183 13 ▶ return $pass if ($pass =~ m/\$argon2/);
Branch: 1 branch
BranchTrueFalseTotal
if $pass =~ /\$argon2/u 9 4 13
184 4 ▶ !$self->{$_->[0]} ? croak "$_->[0] not set" : push @params, $self->{$_->[0]} for @argons;
Branch: 1 branch
BranchTrueFalseTotal
!$$self{$_->[0]} ? : 1 16 17
185 3 return argon2id_pass($pass, @params);
186 }
187  
188 6 1;
189  
190 __END__
↑ Summary