« 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
Condition33%1/3
defined $plain and length $plain
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
| |||||||||||||||||||||||||||||
| 16 | 410 | $opts{lc $_} = delete $opts{$_} for keys %opts; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 410 | ▶ | return _bcrypt($plain) if $opts{bcrypt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 303 | ▶ | return unix_md5_crypt($plain) if $opts{md5}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 202 | ▶ | return apache_md5_crypt($plain) if $opts{apache}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 24 | 101 | ▶ | $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi' if !defined(crypt("ab", $alg."cd")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 101 | ▶ | $alg = '' if !defined(crypt("ab", $alg."cd")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $pass and length $pass
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
| |||||||||||||||||||||||||||||
| 32 | 751 | ▶ | return if !defined $chk || !length $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $chk and length $chk
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
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 751 | my $md5 = '$1$'; my $apr = '$apr1$'; my $bcr = '$2a$'; | |||||||||||||||||||||||||||
| 35 | 751 | ▶ | if (index($chk,$apr) == 0) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif ( index($chk,$bcr) == 0 ) { | ||||||||||||||||||||||||||||
| 50 | 144 | ▶ | return 1 if _bcrypt( $pass, $chk ) eq $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 413 | my $crypt = crypt( $pass, $chk ); | |||||||||||||||||||||||||||
| 54 | 413 | ▶ | return 1 if $crypt && $crypt eq $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$crypt and $crypt eq $chk
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
| |||||||||||||||||||||||||||||
| 55 | 312 | ▶ | return 1 if $pass eq $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 107 | my $cost = sprintf('%02d', 6); | |||||||||||||||||||||||||||
| 64 | 107 | my $alg = ''; | |||||||||||||||||||||||||||
| 65 | 107 | ▶ | $alg = '$5$' if !defined(crypt("ab", $alg."cd")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 107 | ▶ | $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi' if !defined(crypt("ab", $alg."cd")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 107 | ▶ | $alg = '' if !defined(crypt("ab", $alg."cd")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||