Raps2/Password.pm

filter: Statementstmt 90.3% 56 / 62 Branchbran 66.6% 8 / 12 Conditioncond 52.9% 9 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 78.8% 97 / 123 CC 14 SCAR 11.9
CC
14
Cov
80%
new
6.0
salt
4.9
encrypt
3.7
CRAP 3.3
« Raps2.pm ↑ Summary
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
l!l
35
MC/DC100%1/1
$conf{"cost"} //= 12
$conf{"cost"}
Truth table100%2/2
A
A= $conf{"cost"}
Aresult
11
00
18  
19 8 ▶ if ( not defined $conf{salt} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"salt"}) 2 6 8
20 2 $conf{salt} = create_salt();
21 }
22  
23 8 ▶ if ( length( $conf{salt} ) != 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $conf{"salt"} != 16) 2 6 8
24 2 confess('incorrect salt length');
25 }
26  
27 6 ▶ if ( not( defined $conf{passphrase} and length $conf{passphrase} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"passphrase"} and length $conf{"passphrase"}) 2 4 6
Condition100%3/3
defined $conf{"passphrase"} and length $conf{"passphrase"}
!ll&&!rl&&r
114
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (defined $salt) 0 1 1
51 0 ▶ if ( length($salt) != 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $salt != 16) 0 0 0
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"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$opt{"salt"} //= $self->{"salt"}
$opt{"salt"} $self->{"salt"}
Truth table33%1/3
A //= B
A= $opt{"salt"}B= $self->{"salt"}
ABresult
1X1
011
000
65 1 ▶ $opt{cost} //= $self->{cost};
Condition33%1/3
$opt{"cost"} //= $self->{"cost"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$opt{"cost"} //= $self->{"cost"}
$opt{"cost"} $self->{"cost"}
Truth table33%1/3
A //= B
A= $opt{"cost"}B= $self->{"cost"}
ABresult
1X1
011
000
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"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt{"cost"} //= $self->{"cost"}
$opt{"cost"} $self->{"cost"}
Truth table33%1/3
A //= B
A= $opt{"cost"}B= $self->{"cost"}
ABresult
1X1
011
000
78 1 ▶ $opt{salt} //= $self->{salt};
Condition33%1/3
$opt{"salt"} //= $self->{"salt"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt{"salt"} //= $self->{"salt"}
$opt{"salt"} $self->{"salt"}
Truth table33%1/3
A //= B
A= $opt{"salt"}B= $self->{"salt"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($testhash eq $myhash) 1 0 1
108 1 return 1;
109 }
110 0 confess('Passwords did not match');
111 }
112  
113 2 1;
114  
115 __END__
« Raps2.pm ↑ Summary