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

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