| 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
| |||||||||||||||||||||||||||||||||||
| 28 | 17 | ▶ | $_[0] =~ m#\A[\x00-\xff]{6}.*\z# | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 22 | my $self = bless({ algorithm => 'Argon2' }, $class); | |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 22 | ▶ | $args{crypt} = delete $args{passphrase} if $args{passphrase}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 21 | foreach (qw/hash base64 hex/) { | |||||||||||||||||||||||||||||||||
| 80 | 63 | ▶ | if (exists $args{"stored_$_"}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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]
$$self{$_->[0]} or $args{$_->[0]}
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]
| |||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | 21 | ▶ | unless ($args{crypt}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | exists $args{$_} ? ! $self->{crypt} | ||||||||||||||||||||||||||||||||||
| 93 | ? $self->$_($args{$_}) | ||||||||||||||||||||||||||||||||||
| 94 | : croak sprintf "hash specified redundantly - %s", $_ | ||||||||||||||||||||||||||||||||||
| 95 | 16 | ▶ | : next foreach (keys %hashes); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 184 | 4 | ▶ | !$self->{$_->[0]} ? croak "$_->[0] not set" : push @params, $self->{$_->[0]} for @argons; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 185 | 3 | return argon2id_pass($pass, @params); | |||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | 6 | 1; | |||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||||||||