blib/lib/POE/Component/Server/IRC/Common.pm

filter: Statementstmt 100% 68 / 68 Branchbran 73.8% 31 / 42 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 82.2% 116 / 141 CC 27 SCAR 23.1
CC
27
Cov
85%
chkpasswd
15.1
mkpasswd
10.9
_bcrypt
5.1
CRAP 10.0
« blib/lib/POE/Component/Server/IRC/Backend.pm ↑ Summary blib/lib/POE/Component/Server/IRC/Help.pm »
1 package POE::Component::Server::IRC::Common;
2 382 $POE::Component::Server::IRC::Common::VERSION = '1.66';
3 382 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 382 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 382 ▶use Crypt::PasswdMD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 382 ▶use Crypt::Eksblowfish::Bcrypt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 382 require Exporter;
9 382 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 382 our @EXPORT_OK = qw(mkpasswd chkpasswd);
11 382 our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] );
12  
13 410 ▶sub mkpasswd {
Subroutine mkpasswd: called CC=10 SCAR=23.9
Pod documented
14 410 my ($plain, %opts) = @_;
15 410 ▶ return if !defined $plain || !length $plain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plain and length $plain 0 410 410
Condition33%1/3
defined $plain and length $plain
!ll&&!rl&&r
00410
MC/DC0%0/2
defined $plain and length $plain
defined $plain length $plain
Truth table33%1/3
A and B
A= defined $plainB= length $plain
ABresult
0X0
100
111
16 410 $opts{lc $_} = delete $opts{$_} for keys %opts;
17  
18 410 ▶ return _bcrypt($plain) if $opts{bcrypt};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"bcrypt"} 107 303 410
19 303 ▶ return unix_md5_crypt($plain) if $opts{md5};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"md5"} 101 202 303
20 202 ▶ return apache_md5_crypt($plain) if $opts{apache};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"apache"} 101 101 202
21 101 my $salt = join '', ('.','/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
22 101 my $alg = '';
23 101 ▶ $alg = '$5$' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 101 101
24 101 ▶ $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 101 101
25 101 ▶ $alg = '' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 101 101
26 101 return crypt($plain, $alg.$salt);
27 }
28  
29 751 ▶sub chkpasswd {
Subroutine chkpasswd: called CC=14 SCAR=27.1
Pod documented
30 751 my ($pass, $chk) = @_;
31 751 ▶ return if !defined $pass || !length $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass and length $pass 0 751 751
Condition33%1/3
defined $pass and length $pass
!ll&&!rl&&r
00751
MC/DC0%0/2
defined $pass and length $pass
defined $pass length $pass
Truth table33%1/3
A and B
A= defined $passB= length $pass
ABresult
0X0
100
111
32 751 ▶ return if !defined $chk || !length $chk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chk and length $chk 0 751 751
Condition33%1/3
defined $chk and length $chk
!ll&&!rl&&r
00751
MC/DC0%0/2
defined $chk and length $chk
defined $chk length $chk
Truth table33%1/3
A and B
A= defined $chkB= length $chk
ABresult
0X0
100
111
33  
34 751 my $md5 = '$1$'; my $apr = '$apr1$'; my $bcr = '$2a$';
35 751 ▶ if (index($chk,$apr) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (index($chk, $apr) == 0) { } 101 650 751
elsif (index($chk, $md5) == 0) { } 101 549 650
elsif (index($chk, $bcr) == 0) { } 144 405 549
36 101 my $salt = $chk;
37 101 $salt =~ s/^\Q$apr//;
38 101 $salt =~ s/^(.*)\$/$1/;
39 101 $salt = substr( $salt, 0, 8 );
40 101 ▶ return 1 if apache_md5_crypt($pass, $salt) eq $chk;
Branch: 1 branch
BranchTrueFalseTotal
if apache_md5_crypt($pass, $salt) eq $chk 101 0 101
41 }
42 elsif ( index($chk,$md5) == 0 ) {
43 101 my $salt = $chk;
44 101 $salt =~ s/^\Q$md5//;
45 101 $salt =~ s/^(.*)\$/$1/;
46 101 $salt = substr( $salt, 0, 8 );
47 101 ▶ return 1 if unix_md5_crypt($pass, $salt) eq $chk;
Branch: 1 branch
BranchTrueFalseTotal
if unix_md5_crypt($pass, $salt) eq $chk 101 0 101
48 }
49 elsif ( index($chk,$bcr) == 0 ) {
50 144 ▶ return 1 if _bcrypt( $pass, $chk ) eq $chk;
Branch: 1 branch
BranchTrueFalseTotal
if _bcrypt($pass, $chk) eq $chk 136 8 144
51 }
52  
53 413 my $crypt = crypt( $pass, $chk );
54 413 ▶ return 1 if $crypt && $crypt eq $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $crypt and $crypt eq $chk 101 312 413
Condition66%2/3
$crypt and $crypt eq $chk
!ll&&!rl&&r
0312101
MC/DC50%1/2
$crypt and $crypt eq $chk
$crypt $crypt eq $chk
Truth table66%2/3
A and B
A= $cryptB= $crypt eq $chk
ABresult
0X0
100
111
55 312 ▶ return 1 if $pass eq $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $pass eq $chk 304 8 312
56 8 return;
57 }
58  
59 251 ▶sub _bcrypt {
Subroutine _bcrypt: called CC=5 SCAR=16.2
60 251 my $plain = shift;
61 251 my $salt = shift;
62 251 ▶ if ( !defined $salt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $salt) 107 144 251
63 107 my $cost = sprintf('%02d', 6);
64 107 my $alg = '';
65 107 ▶ $alg = '$5$' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 107 107
66 107 ▶ $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 107 107
67 107 ▶ $alg = '' if !defined(crypt("ab", $alg."cd"));
Branch: 1 branch
BranchTrueFalseTotal
unless defined crypt("ab", $alg . 'cd') 0 107 107
68 107 ▶ my $salty = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 107 my $num = 999999;
70 107 my $cr = crypt( rand($num), $alg.rand($num) ) . crypt( rand($num), $alg.rand($num) );
71 107 Crypt::Eksblowfish::Bcrypt::en_base64(substr( $cr, 4, 16 ));
72 107 };
73 107 $salt = join( '$', '$2a', $cost, $salty->() );
74 }
75 251 return Crypt::Eksblowfish::Bcrypt::bcrypt($plain,$salt);
76 }
77  
78 382 1;
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 POE::Component::Server::IRC::Common - provides a set of common functions for the POE::Component::Server::IRC suite.
85  
86 =head1 SYNOPSIS
87  
88 use strict;
89 use warnings;
90  
91 use POE::Component::Server::IRC::Common qw( :ALL );
92  
93 my $passwd = mkpasswd( 'moocow' );
94  
95  
96 =head1 DESCRIPTION
97  
98 POE::Component::IRC::Common provides a set of common functions for the
99 L<POE::Component::Server::IRC|POE::Component::Server::IRC> suite.
100  
101 =head1 FUNCTIONS
102  
103 =head2 C<mkpasswd>
104  
105 Takes one mandatory argument a plain string to 'encrypt'. If no further
106 options are specified it uses C<crypt> to generate the password. Specifying
107 'md5' option uses L<Crypt::PasswdMD5|Crypt::PasswdMD5>'s C<unix_md5_crypt>
108 function to generate the password. Specifying 'apache' uses
109 Crypt::PasswdMD5 C<apache_md5_crypt> function to generate the password.
110 Specifying 'bcrypt' option uses L<Crypt::Eksblowfish::Bcrypt> to generate
111 the password (recommended).
112  
113 my $passwd = mkpasswd( 'moocow' ); # vanilla crypt()
114 my $passwd = mkpasswd( 'moocow', md5 => 1 ) # unix_md5_crypt()
115 my $passwd = mkpasswd( 'moocow', apache => 1 ) # apache_md5_crypt()
116 my $passwd = mkpasswd( 'moocow', bcrypt => 1 ) # bcrypt() # recommended
117  
118 =head2 C<chkpasswd>
119  
120 Takes two mandatory arguments, a password string and something to check that
121 password against. The function first tries md5 comparisons (UNIX and Apache)
122 and bcrypt, then C<crypt> and finally plain-text password check.
123  
124 =head1 AUTHOR
125  
126 Chris 'BinGOs' Williams
127  
128 =head1 LICENSE
129  
130 Copyright E<copy> Chris Williams
131  
132 This module may be used, modified, and distributed under the same terms as
133 Perl itself. Please see the license that came with your Perl distribution
134 for details.
135  
136 =head1 SEE ALSO
137  
138 L<POE::Component::Server::IRC>
139 L<Crypt::PasswdMD5>
140 L<Crypt::Eksblowfish::Bcrypt>
141 L<Mojolicious::Plugin::Bcrypt>
142  
143 =cut
« blib/lib/POE/Component/Server/IRC/Backend.pm ↑ Summary blib/lib/POE/Component/Server/IRC/Help.pm »