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