File Coverage

blib/lib/Text/Password/AutoMigration.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Text::Password::AutoMigration;
2             our $VERSION = "0.16";
3              
4 2     2   72782 use Carp;
  2         16  
  2         126  
5 2     2   1111 use Moose;
  2         943382  
  2         14  
6             extends 'Text::Password::SHA';
7              
8             =encoding utf-8
9              
10             =head1 NAME
11              
12             Text::Password::AutoMigration - generate and verify Password with any contexts
13              
14             =head1 SYNOPSIS
15              
16             my $pwd = Text::Password::AutoMigration->new();
17             my( $raw, $hash ) = $pwd->genarate(); # list context is required
18             my $input = $req->body_parameters->{passwd};
19             my $data = $pwd->encrypt($input); # salt is made automatically
20             my $flag = $pwd->verify( $input, $data );
21              
22             =head1 DESCRIPTION
23              
24             Text::Password::AutoMigration is the Module for lasy Administrators.
25              
26             It always generates the password with SHA512.
27            
28             And verifies automatically the hash with
29             B<CORE::crypt>, B<MD5>, B<SHA-1 by hex>, B<SHA-256> and of course B<SHA-512>.
30              
31             All you have to do are those:
32            
33             1. use this module
34              
35             2. replace the hashes in your DB periodically.
36              
37             =head2 Constructor and initialization
38              
39             =head3 new()
40            
41             No arguments are required. But you can set some parameters.
42              
43             =over
44              
45             =item default
46              
47             You can set default length with param 'default' like below:
48              
49             $pwd = Text::Pasword::AutoMiglation->new( default => 12 );
50              
51             It must be an Int, defaults to 8.
52              
53             =item readablity
54              
55             Or you can set default strength for password with param 'readablity'.
56              
57             It must be a Boolean, defaults to 1.
58              
59             If it was set as 0, you can generate stronger passwords with generate().
60              
61             $pwd = Text::Pasword::AutoMiglation->new( readability => 0 );
62              
63             =item migrate
64              
65             It must be a Boolean, defaults to 1.
66              
67             This module is for Administrators who try to replace hashes in their DB.
68             However, if you've already done to replace them or start to make new Apps with this module,
69             you can set param migrate as 0.
70             Then it will work a little faster without regenerating new hashes.
71              
72             =cut
73              
74             has migrate => ( is => 'rw', isa => 'Bool', default => 1 );
75              
76             =back
77              
78             =head2 Methods and Subroutines
79              
80             =head3 verify( $raw, $hash )
81              
82             returns the true value if the verification succeeds.
83              
84             Actually, the value is new hash with SHA-512 from $raw.
85              
86             So you can replace hashes in your DB very easily like below:
87            
88             my $pwd = Text::Password::AutoMigration->new();
89             my $input = $req->body_parameters->{passwd};
90             my $hash = $pwd->verify( $input, $db{passwd} ); # returns hash with SHA-512, and it's true
91              
92             if ($hash) { # you don't have to execute this every time
93             $succeed = 1;
94             my $sth = $dbh->prepare('UPDATE DB SET passwd=? WHERE uid =?') or die $dbh->errstr;
95             $sth->excute( $hash, $req->body_parameters->{uid} ) or die $sth->errstr;
96             }
97              
98             New hash length is at least 98. So you have to change your DB like below:
99              
100             ALTER TABLE User CHANGE passwd passwd VARCHAR(98);
101              
102             =cut
103              
104             override 'verify' => sub {
105             my $self = shift;
106             my ( $input, $data ) = @_;
107              
108             if ( super() ){
109             return $self->encrypt($input) if $self->migrate();
110             return 1;
111             }elsif( $self->Text::Password::MD5::verify(@_) ){
112             return $self->encrypt($input) if $self->migrate();
113             return 1;
114             }
115             return undef;
116             };
117              
118             =head3 nonce($length)
119              
120             generates the random strings with enough strength.
121              
122             the length defaults to 8($self->default).
123              
124             =head3 encrypt($raw)
125              
126             returns hash with unix_sha512_crypt().
127              
128             salt will be made automatically.
129            
130             =head3 generate($length)
131              
132             genarates pair of new password and it's hash.
133              
134             less readable characters(0Oo1Il|!2Zz5sS$6b9qCcKkUuVvWwXx.,:;~-^'"`) are forbidden
135             unless $self->readability is 0.
136              
137             the length defaults to 8($self->default).
138              
139             B<DON'T TRUST> this method.
140             According to L<Password expert says he was wrong|https://www.usatoday.com/story/news/nation-now/2017/08/09/password-expert-says-he-wrong-numbers-capital-letters-and-symbols-useless/552013001/>,
141             it's not a safe way. So, I will rewrite this method as soon as I find the better way.
142              
143            
144             =cut
145              
146             __PACKAGE__->meta->make_immutable;
147 2     2   16607 no Moose;
  2         6  
  2         11  
148              
149             1;
150              
151             __END__
152              
153             =head1 SEE ALSO
154              
155             =over
156              
157             =item L<GitHub|https://github.com/worthmine/Text-Password-AutoMigration>
158              
159             =item L<CPAN|http://search.cpan.org/perldoc?Text%3A%3APassword%3A%3AAutoMigration>
160              
161             =item L<https://shattered.io/>
162              
163              
164             =back
165              
166             =head1 LICENSE
167              
168             Copyright (C) Yuki Yoshida(worthmine).
169              
170             This library is free software; you can redistribute it and/or modify
171             it under the same terms as Perl itself.
172              
173             =head1 AUTHOR
174              
175             Yuki Yoshida(worthmine) E<lt>worthmine!at!gmail.comE<gt>