line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Password::SHA; |
2
|
|
|
|
|
|
|
our $VERSION = "0.17"; |
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
6162
|
use Moo; |
|
3
|
|
|
|
|
7731
|
|
|
3
|
|
|
|
|
22
|
|
5
|
3
|
|
|
3
|
|
2332
|
use Carp; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
192
|
|
6
|
3
|
|
|
3
|
|
2399
|
use Crypt::Passwd::XS; |
|
3
|
|
|
|
|
974
|
|
|
3
|
|
|
|
|
121
|
|
7
|
3
|
|
|
3
|
|
1345
|
use autouse 'Digest::SHA' => qw(sha1_hex); |
|
3
|
|
|
|
|
2349
|
|
|
3
|
|
|
|
|
18
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
837
|
use Types::Standard qw(Int); |
|
3
|
|
|
|
|
112136
|
|
|
3
|
|
|
|
|
21
|
|
10
|
3
|
|
|
3
|
|
5349
|
use constant Min => 4; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1486
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
extends 'Text::Password::MD5'; |
13
|
|
|
|
|
|
|
has default => ( is => 'rw', isa => Int->where('$_ >= 10'), default => sub {10} ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=encoding utf-8 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Text::Password::SHA - generate and verify Password with SHA |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $pwd = Text::Password::SHA->new(); |
24
|
|
|
|
|
|
|
my( $raw, $hash ) = $pwd->genarate(); # list context is required |
25
|
|
|
|
|
|
|
my $input = $req->body_parameters->{passwd}; |
26
|
|
|
|
|
|
|
my $data = $pwd->encrypt($input); # you don't have to care about salt |
27
|
|
|
|
|
|
|
my $flag = $pwd->verify( $input, $data ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Text::Password::SHA is the last part of Text::Password::AutoMigration. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 Constructor and initialization |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head3 new() |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
No arguments are required. But you can set some arguments. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=over |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=item default( I<Int> ) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
You can set other length to 'default' like below: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$pwd = Text::Pasword::AutoMiglation->new( default => 8 ); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item readablity( I<Bool> ) |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
It must be a boolean, default is 1. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
less readable characters(I<0Oo1Il|!2Zz5sS$6b9qCcKkUuVvWwXx.,:;~-^'"`>) are forbidden |
52
|
|
|
|
|
|
|
while $self->readability is 1. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
You can let passwords to be more secure with setting I<readablity =E<lt> 0>. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Then you can generate stronger passwords with I<generate()>. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$pwd = Text::Pasword::AutoMiglation->new( readability => 0 ); # or |
59
|
|
|
|
|
|
|
$pwd->readability(0); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=back |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 Methods and Subroutines |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head3 verify( $raw, $hash ) |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
returns true if the verification succeeds. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub verify { |
72
|
309
|
|
|
309
|
1
|
1675
|
my ( $self, $input, $data ) = ( shift, @_ ); |
73
|
309
|
|
|
|
|
5024
|
my $m = $self->default(); |
74
|
309
|
50
|
|
|
|
2402
|
carp 'Invalid input' unless length $input; |
75
|
309
|
50
|
|
|
|
686
|
carp 'Invalid hash' unless length $data; |
76
|
|
|
|
|
|
|
|
77
|
309
|
100
|
|
|
|
1650653
|
return $data eq Crypt::Passwd::XS::unix_sha512_crypt( $input, $data ) |
78
|
|
|
|
|
|
|
if $data =~ m|^\$6\$[!-~]{1,$m}\$[\w/\.]{86}$|; |
79
|
3
|
100
|
|
|
|
6671
|
return $data eq Crypt::Passwd::XS::unix_sha256_crypt( $input, $data ) |
80
|
|
|
|
|
|
|
if $data =~ m|^\$5\$[!-~]{1,$m}\$[\w/\.]{43}$|; |
81
|
2
|
100
|
|
|
|
37
|
return $data eq sha1_hex($input) if $data =~ /^[\da-f]{40}$/i; |
82
|
1
|
|
|
|
|
248
|
carp __PACKAGE__, " doesn't support this hash: ", $data; |
83
|
1
|
|
|
|
|
1025
|
return; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head3 nonce( I<Int> ) |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
generates the random strings with enough strength. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
the length defaults to 10 || $self->default(). |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head3 encrypt( I<Str> ) |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
returns hash with unix_sha512_crypt(). |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
salt will be made automatically. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub encrypt { |
101
|
411
|
|
|
411
|
1
|
4229
|
my ( $self, $input ) = @_; |
102
|
411
|
100
|
|
|
|
1251
|
croak ref $self, " requires a strings longer than at least ", Min if length($input) < Min; |
103
|
410
|
100
|
|
|
|
1309
|
croak ref $self, " doesn't allow any Wide Characters or white spaces" if $input =~ /[^ -~]/; |
104
|
409
|
|
|
|
|
1131
|
return Crypt::Passwd::XS::unix_sha512_crypt( $input, $self->nonce() ); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head3 generate( I<Int> ) |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
genarates pair of new password and it's hash. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
less readable characters(I<0Oo1Il|!2Zz5sS$6b9qCcKkUuVvWwXx.,:;~-^'"`>) are forbidden |
113
|
|
|
|
|
|
|
unless $self->readability is 0. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
the length defaults to 10 || $self->default(). |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 LICENSE |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Copyright (C) Yuki Yoshida(worthmine). |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
128
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Yuki Yoshida E<lt>worthmine@users.noreply.github.comE<gt> |