File Coverage

blib/lib/Text/Password/SHA.pm
Criterion Covered Total %
statement 34 34 100.0
branch 12 14 85.7
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package Text::Password::SHA;
2             our $VERSION = "0.43";
3              
4 3     3   345452 use Moo;
  3         11592  
  3         26  
5 3     3   3535 use strictures 2;
  3         2357  
  3         151  
6 3     3   4511 use Crypt::Passwd::XS;
  3         1429  
  3         201  
7              
8 3     3   544 use autouse 'Carp' => qw(croak carp);
  3         1138  
  3         27  
9 3     3   468 use autouse 'Digest::SHA' => qw(sha1_hex);
  3         8  
  3         14  
10              
11 3     3   1206 use Types::Standard qw(Int);
  3         159628  
  3         26  
12 3     3   7333 use constant Min => 4;
  3         8  
  3         1892  
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 )
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 )
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 0>.
57              
58             Then you can generate stronger passwords with I.
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 1709     1709 1 618711 my ( $self, $input, $data ) = ( shift, @_ );
77 1709         47677 my $m = $self->default();
78 1709 50       16800 carp 'Invalid input' unless length $input;
79 1709 50       4827 carp 'Invalid hash' unless length $data;
80              
81 1709 100       10084474 return $data eq Crypt::Passwd::XS::unix_sha512_crypt( $input, $data )
82             if $data =~ m|^\$6\$[ -~]{1,$m}\$[\w/\.]{86}$|;
83 3 100       9421 return $data eq Crypt::Passwd::XS::unix_sha256_crypt( $input, $data )
84             if $data =~ m|^\$5\$[ -~]{1,$m}\$[\w/\.]{43}$|;
85 2 100       30 return $data eq sha1_hex($input) if $data =~ /^[\da-f]{40}$/i;
86 1         245 carp __PACKAGE__, " doesn't support this hash: ", $data;
87 1         760 return;
88             }
89              
90             =head3 nonce( I )
91              
92             generates the random strings with enough strength.
93              
94             the length defaults to 10 || $self->default().
95              
96             =head3 encrypt( I )
97              
98             returns hash with unix_sha512_crypt().
99              
100             salt will be made automatically.
101              
102             =cut
103              
104             sub encrypt {
105 1811     1811 1 21527 my ( $self, $input ) = @_;
106 1811 100       6164 croak ref $self, " requires a strings longer than at least ", Min if length $input < Min;
107 1810 100       7750 croak ref $self, " doesn't allow any Wide Characters or control codes" if $input =~ /[^ -~]/;
108 1809         8255 return Crypt::Passwd::XS::unix_sha512_crypt( $input, $self->nonce() );
109             }
110              
111             =head3 generate( I )
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__