File Coverage

blib/lib/Data/Password/Check/JPassword.pm
Criterion Covered Total %
statement 29 50 58.0
branch 8 14 57.1
condition 8 9 88.8
subroutine 6 14 42.8
pod 10 10 100.0
total 61 97 62.8


line stmt bran cond sub pod time code
1             package Data::Password::Check::JPassword;
2              
3 3     3   115571 use 5.008008;
  3         20  
  3         123  
4 3     3   37 use strict;
  3         7  
  3         158  
5 3     3   17 use warnings;
  3         13  
  3         128  
6              
7             require Exporter;
8 3     3   27294 use POSIX qw( floor log );
  3         24408  
  3         23  
9              
10             our @ISA = qw(Exporter);
11             our %EXPORT_TAGS = ( 'all' => [ qw(
12             password_check
13             ) ] );
14              
15             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16              
17             our @EXPORT = qw(
18             security is_weak is_strong is_medium advice
19             password_security
20             password_strong
21             password_medium
22             password_weak
23             password_advice
24             );
25              
26             our $VERSION = '0.02';
27              
28              
29             ###########################################################
30             sub security
31             {
32 8     8 1 2790 my( $package, $password ) = @_;
33 8 50       23 return $password if ref $password;
34 8         43 $password =~ s/^(\s+)|(\s+$)//g;
35 8         42 my $c = {number=>1, uppercase=>1, lowercase=>1, punctuation=>1, special=>1};
36 8         39 while( $password =~ /(.)/g ) {
37 78         119 my $char = ord $1;
38 78 50       139 if( $char > 127 ) {
39 0         0 $c->{special}++;
40             }
41 78 100 100     518 if( $char > 47 && $char < 58 ) {
    100 100        
    100 66        
42 4         15 $c->{number} ++;
43             }
44             elsif( $char > 64 && $char < 91 ) {
45 24         77 $c->{uppercase} ++;
46             }
47             elsif( $char > 96 && $char < 123 ) {
48 42         142 $c->{lowercase} ++;
49             }
50             else {
51 8         31 $c->{punctuation} += 2;
52             }
53             }
54              
55 8         25 my $level = $c->{number} * $c->{uppercase} * $c->{lowercase} *
56             $c->{punctuation} * $c->{special};
57             # $c->{level} = floor( log( $level*$level ) + 0.5 );
58             # jPassword uses Math.round()... why bother?
59 8         172 $c->{level} = log( $level*$level );
60 8         483 $c->{password} = $password;
61 8         28 return $c
62             }
63              
64             ###########################################################
65             # Return true if this is a strong password
66             sub is_strong
67             {
68 0     0 1 0 my( $package, $password ) = @_;
69 0         0 my $c = $package->security( $password );
70 0         0 return $c->level >= 10;
71             }
72              
73             ###########################################################
74             # Return true if this is a medium-strength password
75             sub is_medium
76             {
77 0     0 1 0 my( $package, $password ) = @_;
78 0         0 my $c = $package->security( $password );
79 0         0 return $c->level >= 5;
80             }
81              
82             ###########################################################
83             # Return true if this is a weak password
84             sub is_weak
85             {
86 0     0 1 0 my( $package, $password ) = @_;
87 0         0 my $c = $package->security( $password );
88 0         0 return $c->level < 5;
89             }
90              
91             ###########################################################
92             sub advice
93             {
94 0     0 1 0 my( $package, $password ) = @_;
95 0         0 my $c = password_security( $password );
96 0 0       0 return if password_strong( $c );
97 0         0 foreach my $k ( qw( lowercase uppercase number punctuation special ) ) {
98 0 0       0 return $k if $c->{$k} < 2;
99             }
100             }
101              
102              
103              
104              
105              
106             ###########################################################
107             sub password_security
108             {
109 4     4 1 2185 return __PACKAGE__->security( @_ );
110             }
111              
112             ###########################################################
113             sub password_strong
114             {
115 0     0 1   return __PACKAGE__->is_strong( @_ );
116             }
117              
118             ###########################################################
119             # Return true if this is a strong password
120             sub password_medium
121             {
122 0     0 1   return __PACKAGE__->is_medium( @_ );
123             }
124              
125             ###########################################################
126             # Return true if this is a weak password
127             sub password_weak
128             {
129 0     0 1   my( $password ) = @_;
130 0           return __PACKAGE__->is_weak( @_ );
131             }
132              
133             ###########################################################
134             # Return advice for a password
135             sub password_advice
136             {
137 0     0 1   my( $password ) = @_;
138 0           return __PACKAGE__->is_advice( @_ );
139             }
140              
141              
142              
143             1;
144              
145             __END__