| 1 | package App::Raps2::Password; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Crypt::CBC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Crypt::Eksblowfish; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Crypt::Eksblowfish::Bcrypt qw(bcrypt_hash en_base64 de_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.54'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 8 | my ( $obj, %conf ) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | $conf{cost} //= 12; | ||||||||||||||||||||||||||
|
Condition100%2/2
$conf{"cost"} //= 12
MC/DC100%1/1
$conf{"cost"} //= 12
$conf{"cost"}
Truth table100%2/2
A
A= $conf{"cost"}
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | if ( not defined $conf{salt} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 2 | $conf{salt} = create_salt(); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | if ( length( $conf{salt} ) != 16 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 2 | confess('incorrect salt length'); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | if ( not( defined $conf{passphrase} and length $conf{passphrase} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $conf{"passphrase"} and length $conf{"passphrase"}
MC/DC100%2/2
defined $conf{"passphrase"} and length $conf{"passphrase"}
defined $conf{"passphrase"}
length $conf{"passphrase"}
Truth table100%3/3
A and B
A= defined $conf{"passphrase"}B= length $conf{"passphrase"}
| |||||||||||||||||||||||||||||
| 28 | 2 | confess('no passphrase given'); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | my $ref = \%conf; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | return bless( $ref, $obj ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | sub create_salt { | ||||||||||||||||||||||||||
|
Subroutine
create_salt: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 38 | 3 | my $salt = q{}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | for ( 1 .. 16 ) { | |||||||||||||||||||||||||||
| 41 | 48 | $salt .= chr( 0x21 + int( rand(90) ) ); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | return $salt; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | sub salt { | ||||||||||||||||||||||||||
|
Subroutine
salt: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 1 | my ( $self, $salt ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | if ( defined $salt ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( length($salt) != 16 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | confess('incorrect salt length'); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | $self->{salt} = $salt; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | return $self->{salt}; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub encrypt { | ||||||||||||||||||||||||||
|
Subroutine
encrypt: called CC=3 SCAR=13.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 1 | my ( $self, %opt ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | $opt{salt} //= $self->{salt}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opt{"salt"} //= $self->{"salt"}
MC/DC0%0/2
$opt{"salt"} //= $self->{"salt"}
$opt{"salt"}
$self->{"salt"}
Truth table33%1/3
A //= B
A= $opt{"salt"}B= $self->{"salt"}
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | $opt{cost} //= $self->{cost}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opt{"cost"} //= $self->{"cost"}
MC/DC0%0/2
$opt{"cost"} //= $self->{"cost"}
$opt{"cost"}
$self->{"cost"}
Truth table33%1/3
A //= B
A= $opt{"cost"}B= $self->{"cost"}
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $eksblowfish | ||||||||||||||||||||||||||||
| 68 | 1 | = Crypt::Eksblowfish->new( $opt{cost}, $opt{salt}, $self->{passphrase}, ); | |||||||||||||||||||||||||||
| 69 | 1 | my $cbc = Crypt::CBC->new( -cipher => $eksblowfish ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return $cbc->encrypt_hex( $opt{data} ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub decrypt { | ||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=3 SCAR=13.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 1 | my ( $self, %opt ) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $opt{cost} //= $self->{cost}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opt{"cost"} //= $self->{"cost"}
MC/DC0%0/2
$opt{"cost"} //= $self->{"cost"}
$opt{"cost"}
$self->{"cost"}
Truth table33%1/3
A //= B
A= $opt{"cost"}B= $self->{"cost"}
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | $opt{salt} //= $self->{salt}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opt{"salt"} //= $self->{"salt"}
MC/DC0%0/2
$opt{"salt"} //= $self->{"salt"}
$opt{"salt"}
$self->{"salt"}
Truth table33%1/3
A //= B
A= $opt{"salt"}B= $self->{"salt"}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my $eksblowfish | ||||||||||||||||||||||||||||
| 81 | 1 | = Crypt::Eksblowfish->new( $opt{cost}, $opt{salt}, $self->{passphrase}, ); | |||||||||||||||||||||||||||
| 82 | 1 | my $cbc = Crypt::CBC->new( -cipher => $eksblowfish ); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | return $cbc->decrypt_hex( $opt{data} ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub bcrypt { | ||||||||||||||||||||||||||
|
Subroutine
bcrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | return en_base64( | ||||||||||||||||||||||||||||
| 91 | bcrypt_hash( | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | key_nul => 1, | ||||||||||||||||||||||||||||
| 94 | cost => $self->{cost}, | ||||||||||||||||||||||||||||
| 95 | salt => $self->{salt}, | ||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | $self->{passphrase}, | ||||||||||||||||||||||||||||
| 98 | ) | ||||||||||||||||||||||||||||
| 99 | 2 | ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub verify { | ||||||||||||||||||||||||||
|
Subroutine
verify: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 1 | my ( $self, $testhash ) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | my $myhash = $self->bcrypt(); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | if ( $testhash eq $myhash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | return 1; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | confess('Passwords did not match'); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||