| 1 | package Dancer2::Plugin::Passphrase::Core; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Digest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use MIME::Base64 qw(decode_base64 encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Data::Entropy::Algorithms qw(rand_bits rand_int); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Passphrases and Passwords as objects for Dancer2 | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Dancer2::Plugin::Passphrase::Core - Core package for Dancer2::Plugin::Passphrase. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | B<FOR INTERNAL USE ONLY> | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Maintainer: Henk van Oers <hvoers@cpan.org> | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This software is copyright (c) 2012 by James Aitken. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 28 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 98 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 98 | my $class = shift; | |||||||||||||||||||||||||||
| 34 | 98 | my @args = @_; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | return bless { @args == 1 ? %{$args[0]} : @args }, $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # { algorithm => '...', this => '...' } | ||||||||||||||||||||||||||||
| 39 | 26 | ▶ | sub _merge_options { | ||||||||||||||||||||||||||
|
Subroutine
_merge_options: called CC=11 SCAR=24.7
| |||||||||||||||||||||||||||||
| 40 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 26 | my $options = shift; | |||||||||||||||||||||||||||
| 42 | 26 | my $algorithm = $self->{'algorithm'}; | |||||||||||||||||||||||||||
| 43 | 26 | my $settings = {}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # if we got options | ||||||||||||||||||||||||||||
| 46 | 26 | ▶ | if ($options) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 24 | $algorithm = delete $options->{'algorithm'}; | |||||||||||||||||||||||||||
| 48 | $settings = | ||||||||||||||||||||||||||||
| 49 | defined $options->{$algorithm} | ||||||||||||||||||||||||||||
| 50 | ? $options->{$algorithm} | ||||||||||||||||||||||||||||
| 51 | 24 | ▶ | : $self->{$algorithm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Specify empty string to get an unsalted hash | ||||||||||||||||||||||||||||
| 55 | # Leaving it undefs results in 128 random bits being used as salt | ||||||||||||||||||||||||||||
| 56 | # bcrypt requires this amount, and is reasonable for other algorithms | ||||||||||||||||||||||||||||
| 57 | $settings->{'salt'} = rand_bits(128) | ||||||||||||||||||||||||||||
| 58 | 26 | ▶ | unless defined $settings->{'salt'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # RFC 2307 scheme is based on the algorithm, with a prefixed 'S' for salted | ||||||||||||||||||||||||||||
| 61 | 26 | $settings->{'scheme'} = join '', $algorithm =~ /[\w]+/g; | |||||||||||||||||||||||||||
| 62 | $settings->{'scheme'} = 'S'. $settings->{'scheme'} | ||||||||||||||||||||||||||||
| 63 | 26 | ▶ | if $settings->{'salt'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 26 | ▶ | if ( $settings->{'scheme'} eq 'SHA1' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 0 | $settings->{'scheme'} = 'SHA'; | |||||||||||||||||||||||||||
| 67 | } elsif ( $settings->{'scheme'} eq 'SSHA1' ) { | ||||||||||||||||||||||||||||
| 68 | 6 | $settings->{'scheme'} = 'SSHA'; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Bcrypt requires a cost parameter | ||||||||||||||||||||||||||||
| 72 | 26 | ▶ | if ( $algorithm eq 'Bcrypt' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 5 | $settings->{'scheme'} = 'CRYPT'; | |||||||||||||||||||||||||||
| 74 | 5 | $settings->{'type'} = '2a'; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | ▶ | $settings->{'cost'} //= $self->{'Bcrypt'}{'cost'} || 4; | ||||||||||||||||||||||||||
|
Condition50%2/4
$settings->{"cost"} //= $self->{"Bcrypt"}{"cost"} || 4
$self->{'Bcrypt'}{'cost'} || 4
MC/DC0%0/1
$settings->{"cost"} //= $self->{"Bcrypt"}{"cost"} || 4
$settings->{"cost"}
Truth table50%1/2
A
A= $settings->{"cost"}
| |||||||||||||||||||||||||||||
| 77 | 5 | ▶ | $settings->{'cost'} = 31 if $settings->{'cost'} > 31; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 5 | $settings->{'cost'} = sprintf '%02d', $settings->{'cost'}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 26 | $settings->{'algorithm'} = $algorithm; | |||||||||||||||||||||||||||
| 82 | 26 | $settings->{'plaintext'} = $self->{'plaintext'}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 26 | return $settings; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # From Crypt::Eksblowfish::Bcrypt. | ||||||||||||||||||||||||||||
| 88 | # Bcrypt uses it's own variation on base64 | ||||||||||||||||||||||||||||
| 89 | 84 | ▶ | sub _en_bcrypt_base64 { | ||||||||||||||||||||||||||
|
Subroutine
_en_bcrypt_base64: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 84 | my ($octets) = @_; | |||||||||||||||||||||||||||
| 91 | 84 | my $text = encode_base64($octets, ''); | |||||||||||||||||||||||||||
| 92 | 84 | $text =~ tr{A-Za-z0-9+/=}{./A-Za-z0-9}d; | |||||||||||||||||||||||||||
| 93 | 84 | return $text; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # And the decoder of bcrypt's custom base64 | ||||||||||||||||||||||||||||
| 98 | 38 | ▶ | sub _de_bcrypt_base64 { | ||||||||||||||||||||||||||
|
Subroutine
_de_bcrypt_base64: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 38 | my ($text) = @_; | |||||||||||||||||||||||||||
| 100 | 38 | $text =~ tr{./A-Za-z0-9}{A-Za-z0-9+/}; | |||||||||||||||||||||||||||
| 101 | 38 | $text .= "=" x (3 - (length($text) + 3) % 4); | |||||||||||||||||||||||||||
| 102 | 38 | return decode_base64($text); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Extracts the settings from an RFC 2307 string | ||||||||||||||||||||||||||||
| 106 | 69 | ▶ | sub _extract_settings { | ||||||||||||||||||||||||||
|
Subroutine
_extract_settings: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 107 | 69 | my ($self, $rfc2307_string) = @_; | |||||||||||||||||||||||||||
| 108 | 69 | my $settings = {}; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 69 | my ($scheme, $rfc_settings) = ($rfc2307_string =~ m/^{(\w+)}(.*)/s); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 69 | ▶ | unless ($scheme && $rfc_settings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$scheme and $rfc_settings
MC/DC50%1/2
$scheme and $rfc_settings
$scheme
$rfc_settings
Truth table66%2/3
A and B
A= $schemeB= $rfc_settings
| |||||||||||||||||||||||||||||
| 113 | 1 | croak "An RFC 2307 compliant string must be passed to matches()"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 68 | ▶ | if ($scheme eq 'CRYPT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 39 | ▶ | if ($rfc_settings =~ m/^\$2(?:a|x|y)\$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 38 | $scheme = 'Bcrypt'; | |||||||||||||||||||||||||||
| 119 | 38 | $rfc_settings =~ m{\A\$(2a|2x|2y)\$([0-9]{2})\$([./A-Za-z0-9]{22})}x; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 38 | @{$settings}{qw<type cost salt>} = ( $1, $2, _de_bcrypt_base64($3) ); | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 1 | croak "Unknown CRYPT format"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 67 | my $scheme_meta = { | |||||||||||||||||||||||||||
| 128 | 'MD5' => { algorithm => 'MD5', octets => 128 / 8 }, | ||||||||||||||||||||||||||||
| 129 | 'SMD5' => { algorithm => 'MD5', octets => 128 / 8 }, | ||||||||||||||||||||||||||||
| 130 | 'SHA' => { algorithm => 'SHA-1', octets => 160 / 8 }, | ||||||||||||||||||||||||||||
| 131 | 'SSHA' => { algorithm => 'SHA-1', octets => 160 / 8 }, | ||||||||||||||||||||||||||||
| 132 | 'SHA224' => { algorithm => 'SHA-224', octets => 224 / 8 }, | ||||||||||||||||||||||||||||
| 133 | 'SSHA224' => { algorithm => 'SHA-224', octets => 224 / 8 }, | ||||||||||||||||||||||||||||
| 134 | 'SHA256' => { algorithm => 'SHA-256', octets => 256 / 8 }, | ||||||||||||||||||||||||||||
| 135 | 'SSHA256' => { algorithm => 'SHA-256', octets => 256 / 8 }, | ||||||||||||||||||||||||||||
| 136 | 'SHA384' => { algorithm => 'SHA-384', octets => 384 / 8 }, | ||||||||||||||||||||||||||||
| 137 | 'SSHA384' => { algorithm => 'SHA-384', octets => 384 / 8 }, | ||||||||||||||||||||||||||||
| 138 | 'SHA512' => { algorithm => 'SHA-512', octets => 512 / 8 }, | ||||||||||||||||||||||||||||
| 139 | 'SSHA512' => { algorithm => 'SHA-512', octets => 512 / 8 }, | ||||||||||||||||||||||||||||
| 140 | 'Bcrypt' => { algorithm => 'Bcrypt', octets => 128 / 8 }, | ||||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 67 | $settings->{'scheme'} = $scheme; | |||||||||||||||||||||||||||
| 144 | 67 | $settings->{'algorithm'} = $scheme_meta->{$scheme}{algorithm}; | |||||||||||||||||||||||||||
| 145 | 67 | $settings->{'plaintext'} = $self->{'plaintext'};; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 67 | ▶ | if ( !defined $settings->{'salt'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | $settings->{'salt'} = substr( | ||||||||||||||||||||||||||||
| 149 | decode_base64($rfc_settings), | ||||||||||||||||||||||||||||
| 150 | $scheme_meta->{$scheme}{octets}, | ||||||||||||||||||||||||||||
| 151 | 29 | ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 67 | return $settings; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 93 | ▶ | sub _calculate_hash { | ||||||||||||||||||||||||||
|
Subroutine
_calculate_hash: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 158 | 93 | my ( $self, $settings ) = @_; | |||||||||||||||||||||||||||
| 159 | 93 | my $hasher = Digest->new( $settings->{'algorithm'} ); | |||||||||||||||||||||||||||
| 160 | 93 | my ( $hash, $rfc2307 ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 93 | ▶ | if ( $settings->{'algorithm'} eq 'Bcrypt' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 43 | $hasher->add( $settings->{'plaintext'} ); | |||||||||||||||||||||||||||
| 164 | 43 | $hasher->salt( $settings->{'salt'} ); | |||||||||||||||||||||||||||
| 165 | 43 | $hasher->cost( $settings->{'cost'} ); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 43 | $hash = $hasher->digest; | |||||||||||||||||||||||||||
| 168 | $rfc2307 = '{CRYPT}$' | ||||||||||||||||||||||||||||
| 169 | . $settings->{'type'} . '$' | ||||||||||||||||||||||||||||
| 170 | . $settings->{'cost'} . '$' | ||||||||||||||||||||||||||||
| 171 | 42 | . _en_bcrypt_base64( $settings->{'salt'} ) | |||||||||||||||||||||||||||
| 172 | . _en_bcrypt_base64($hash); | ||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 50 | $hasher->add( $settings->{'plaintext'} ); | |||||||||||||||||||||||||||
| 175 | 47 | $hasher->add( $settings->{'salt'} ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 47 | $hash = $hasher->digest; | |||||||||||||||||||||||||||
| 178 | $rfc2307 = '{' . $settings->{'scheme'} . '}' | ||||||||||||||||||||||||||||
| 179 | . encode_base64( | ||||||||||||||||||||||||||||
| 180 | 47 | $hash . $settings->{'salt'}, | |||||||||||||||||||||||||||
| 181 | '' | ||||||||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | return Dancer2::Plugin::Passphrase::Hashed->new( | ||||||||||||||||||||||||||||
| 186 | hash => $hash, | ||||||||||||||||||||||||||||
| 187 | rfc2307 => $rfc2307, | ||||||||||||||||||||||||||||
| 188 | 89 | %{$settings}, | |||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 26 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | 26 | my $options = shift; | |||||||||||||||||||||||||||
| 195 | 26 | my $settings = $self->_merge_options($options); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 26 | return $self->_calculate_hash($settings); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub generate_random { | ||||||||||||||||||||||||||
|
Subroutine
generate_random: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 201 | 3 | my ($self, $options) = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Default is 16 URL-safe base64 chars. Supported everywhere and a reasonable length | ||||||||||||||||||||||||||||
| 204 | 3 | ▶ | my $length = $options->{length} || 16; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'length'} || 16
MC/DC100%1/1
$options->{'length'} || 16
$options->{'length'}
Truth table100%2/2
A
A= $options->{'length'}
| |||||||||||||||||||||||||||||
| 205 | 3 | ▶ | my $charset = $options->{charset} || ['a'..'z', 'A'..'Z', '0'..'9', '-', '_']; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'charset'} || [('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'), ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), 0..9, '-', '_']
MC/DC100%1/1
$options->{'charset'} || [('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'), ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), 0..9, '-', '_']
$options->{'charset'}
Truth table100%2/2
A
A= $options->{'charset'}
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 3 | return join '', map { @$charset[rand_int scalar @$charset] } 1..$length; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 69 | ▶ | sub matches { | ||||||||||||||||||||||||||
|
Subroutine
matches: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 69 | my ($self, $stored_hash) = @_; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 69 | my $settings = $self->_extract_settings($stored_hash); | |||||||||||||||||||||||||||
| 214 | 67 | my $new_hash = $self->_calculate_hash($settings)->rfc2307; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 67 | ▶ | return ($new_hash eq $stored_hash) ? 1 : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 11 | 1; | |||||||||||||||||||||||||||