| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Text::Password::AutoMigration; |
|
2
|
|
|
|
|
|
|
our $VERSION = "0.43"; |
|
3
|
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
511656
|
use autouse 'Carp' => qw(croak carp); |
|
|
2
|
|
|
|
|
2097
|
|
|
|
2
|
|
|
|
|
14
|
|
|
5
|
2
|
|
|
2
|
|
1503
|
use Moo; |
|
|
2
|
|
|
|
|
19661
|
|
|
|
2
|
|
|
|
|
14
|
|
|
6
|
2
|
|
|
2
|
|
5050
|
use strictures 2; |
|
|
2
|
|
|
|
|
4542
|
|
|
|
2
|
|
|
|
|
86
|
|
|
7
|
|
|
|
|
|
|
extends 'Text::Password::SHA'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=encoding utf-8 |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Text::Password::AutoMigration - generate and verify Password with any contexts |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $pwd = Text::Password::AutoMigration->new(); |
|
18
|
|
|
|
|
|
|
my( $raw, $hash ) = $pwd->generate(); # list context is required |
|
19
|
|
|
|
|
|
|
my $input = $req->body_parameters->{passwd}; # in Plack |
|
20
|
|
|
|
|
|
|
$input = $req->param('passwd'); # in CGI |
|
21
|
|
|
|
|
|
|
$input = $raw; # in CLI |
|
22
|
|
|
|
|
|
|
my $data = $pwd->encrypt($input); # you don't have to care about salt |
|
23
|
|
|
|
|
|
|
my $flag = $pwd->verify( $input, $data ); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Text::Password::AutoMigration is a module for some lasy Administrators. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
It would help you to migrate old hash what has vulnerability |
|
30
|
|
|
|
|
|
|
such as encrypted by perl, MD5, SHA-1 or even if it was with SHA-256 to SHA-512. |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The method I automatically detects the algorithm which is applied to the hash |
|
33
|
|
|
|
|
|
|
with B, B, B, B and of course B. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
And every I B generated by using B. |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Therefore all you have to do is to replace the old hash with the new one on your Databases. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 Constructor and initialization |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head3 new() |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
No arguments are required. But you can set some parameters. |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item default( I ) |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
You can set default length with using 'default' argument like below: |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$pwd = Text::Password::AutoMiglation->new( default => 8 ); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
It must be an Int, defaults to 10. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item readablity( I ) |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
You can set default strength for password with usnig 'readablity' argument like below: |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$pwd = Text::Password::AutoMiglation->new( readability => 0 ); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
It must be a Boolean, defaults to 1. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
If it was false, I starts to return stronger passwords with charactors hard to read. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item migrate( I ) |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
It must be a Boolean, defaults to 1. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
If you've already replaced all hash or started to make new applications with this module, |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
you can call the constructor with I 0>. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Then I would not return a new hash but always 1. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
It may help you a little faster without any change of your code. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
|
80
|
|
|
|
|
|
|
|
|
81
|
2
|
|
|
2
|
|
2319
|
use Types::Standard qw(Bool); |
|
|
2
|
|
|
|
|
327098
|
|
|
|
2
|
|
|
|
|
50
|
|
|
82
|
|
|
|
|
|
|
has migrate => ( is => 'rw', isa => Bool, default => 1 ); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=back |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 Methods and Subroutines |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head3 verify( $raw, $hash ) |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
To tell the truth, this is the most useful method of this module. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
it Returns a true strings instead of boolean if the verification succeeds. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Every value is B |
|
95
|
|
|
|
|
|
|
because it is actually true in Perl anyway. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
So you can replace hash in your Database easily like below: |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $pwd = Text::Password::AutoMigration->new(); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $dbh = DBI->connect(...); |
|
102
|
|
|
|
|
|
|
my $db_hash_ref = $dbh->fetchrow_hashref(...); |
|
103
|
|
|
|
|
|
|
my $param = $req->body_parameters; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $hash = $pwd->verify( $param->{passwd} || $raw, $db_hash_ref->{passwd} ); |
|
106
|
|
|
|
|
|
|
my $verified = length $hash; |
|
107
|
|
|
|
|
|
|
if ( $verified ) { # don't have to execute it every time |
|
108
|
|
|
|
|
|
|
my $sth = $dbh->prepare('UPDATE DB SET passwd=? WHERE uid =?') or die $dbh->errstr; |
|
109
|
|
|
|
|
|
|
$sth->excute( $hash, $param->{uid} ) or die $sth->errstr; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
New hash length is 100 (if it defaults). |
|
113
|
|
|
|
|
|
|
So you have to change the Table with like below: |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
ALTER TABLE User MODIFY passwd VARCHAR(100); |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
around verify => sub { |
|
120
|
|
|
|
|
|
|
my ( $orig, $self ) = ( shift, shift ); |
|
121
|
|
|
|
|
|
|
return 0 unless $self->$orig(@_); |
|
122
|
|
|
|
|
|
|
return $self->migrate() ? $self->encrypt(@_) : 1; |
|
123
|
|
|
|
|
|
|
}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head3 nonce( I ) |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
generates the random strings with enough strength. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
the length defaults to 10 || $self->default(). |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head3 encrypt( I ) |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
returns hash with unix_sha512_crypt() |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
enough strength salts will be made automatically. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head3 generate( I ) |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
generates pair of new password and its hash. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
less readable characters(0Oo1Il|!2Zz5sS$6b9qCcKkUuVvWwXx.,:;~-^'"`) are forbidden |
|
142
|
|
|
|
|
|
|
unless $self->readability is 0. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
the length defaults to 10 || $self->default(). |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
B this method. |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
According to L, |
|
149
|
|
|
|
|
|
|
it's not a safe way. So, I will rewrite this method as soon as I find the better way. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
__END__ |