File Coverage

blib/lib/Text/Password/MD5.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 4 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 32 34 94.1


line stmt bran cond sub pod time code
1             package Text::Password::MD5;
2             our $VERSION = "0.16";
3              
4 4     4   3714 use Moose;
  4         488283  
  4         31  
5             extends 'Text::Password::CoreCrypt';
6              
7 4     4   29864 use Carp;
  4         53  
  4         267  
8 4     4   2140 use Crypt::PasswdMD5;
  4         49508  
  4         787  
9              
10             =encoding utf-8
11              
12             =head1 NAME
13              
14             Text::Password::MD5 - generate and verify Password with unix_md5_crypt()
15              
16             =head1 SYNOPSIS
17              
18             my $pwd = Text::Password::MD5->new();
19             my( $raw, $hash ) = $pwd->genarate(); # list context is required
20             my $input = $req->body_parameters->{passwd};
21             my $data = $pwd->encrypt($input); # salt is made automatically
22             my $flag = $pwd->verify( $input, $data );
23              
24             =head1 DESCRIPTION
25              
26             Text::Password::MD5 is the part of Text::Password::AutoMigration.
27              
28             B<DON'T USE> directly.
29              
30             =head2 Constructor and initialization
31              
32             =head3 new()
33              
34             No arguments are required. But you can set some parameters.
35              
36             =over
37              
38             =item default
39              
40             You can set default length with param 'default' like below:
41              
42             $pwd = Text::Pasword::AutoMiglation->new( default => 12 );
43              
44             =item readablity
45              
46             Or you can set default strength for password with param 'readablity'.
47              
48             It must be a boolean, default is 1.
49              
50             If it was set as 0, you can generate stronger passwords with generate().
51              
52             $pwd = Text::Pasword::AutoMiglation->new( readability => 0 );
53            
54             =back
55              
56             =head2 Methods and Subroutines
57              
58             =head3 verify( $raw, $hash )
59              
60             returns true if the verification succeeds.
61              
62             =cut
63              
64             override 'verify' => sub {
65             my $self = shift;
66             my ( $input, $data ) = @_;
67             carp ref($self). " doesn't allow any Wide Characters or white spaces\n" if $input =~ /[^ -~]/;
68             return super() if $data =~ /^[!-~]{13}$/; # with crypt in Perl
69             return $data eq unix_md5_crypt( $input, $data );
70             };
71              
72             __PACKAGE__->meta->make_immutable;
73 4     4   45 no Moose;
  4         11  
  4         30  
74              
75             =head3 nonce($length)
76              
77             generates the random strings with enough strength.
78              
79             the length defaults to 8($self->default).
80              
81             =head3 encrypt($raw)
82              
83             returns hash with unix_md5_crypt().
84              
85             salt will be made automatically.
86              
87             =cut
88              
89             sub encrypt {
90 4     4 1 15171 my $self = shift;
91 4         6 my $input = shift;
92 4         137 my $min = $self->minimum();
93 4 50       17 croak ref($self) ." requires at least $min length" if length $input < $min;
94 4 50       22 carp ref($self). " doesn't allow any Wide Characters or white spaces\n" if $input =~ /[^ -~]/;
95              
96 4         19 return unix_md5_crypt( $input, $self->_salt() );
97             }
98              
99             =head3 generate($length)
100              
101             genarates pair of new password and it's hash.
102              
103             less readable characters(0Oo1Il|!2Zz5sS$6b9qCcKkUuVvWwXx.,:;~-^'"`) are forbidden
104             unless $self->readability is 0.
105              
106             the length defaults to 8($self->default).
107            
108             =cut
109              
110             sub _salt {
111 310     310   552 my $self = shift;
112              
113 310         501 my $salt = '';
114 310         462 do { $salt = $self->nonce(8) } while $salt =~ /\$/;
  333         877  
115 310         1645364 return $salt;
116             }
117              
118             1;
119              
120             __END__
121              
122             =head1 LICENSE
123              
124             Copyright (C) Yuki Yoshida(worthmine).
125              
126             This library is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself.
128              
129             =head1 AUTHOR
130              
131             Yuki Yoshida(worthmine) E<lt>worthmine!at!gmail.comE<gt>