File Coverage

blib/lib/Crypt/Passphrase/System.pm
Criterion Covered Total %
statement 41 46 89.1
branch 3 12 25.0
condition 5 15 33.3
subroutine 12 12 100.0
pod 6 6 100.0
total 67 91 73.6


line stmt bran cond sub pod time code
1             package Crypt::Passphrase::System;
2             $Crypt::Passphrase::System::VERSION = '0.016';
3 1     1   7 use strict;
  1         2  
  1         29  
4 1     1   6 use warnings;
  1         2  
  1         33  
5              
6 1     1   6 use Crypt::Passphrase -encoder;
  1         1  
  1         26  
7              
8 1     1   7 use Carp 'croak';
  1         1  
  1         1014  
9              
10             my @possibilities = (
11             [1 , '$1$' , 6, '$1$aaaaaa$FuYJ957Lgsw.eVsENqOok1' ],
12             [5 , '$5$rounds=535000$', 12, '$5$aaaaaa$9hHgJfCniK4.dU43ykArHVETrhKDDElbS.cioeCajw.' ],
13             [6 , '$6$rounds=656000$', 12, '$6$aaaaaa$RgJSheuY/DBadaBm/5gQ.s3M9a/2n8gubwCE41kMiz1P4KcxORD6LxY2NUCuOQNZawfiD8tWWfRKg9v0CQjbH0'],
14             ['2x', '$2x$12$' , 16, '$2x$08$......................qrjEXaz4RUVmquy3IT5eLKXLB28ahI2' ],
15             ['2a', '$2a$12$' , 16, '$2a$08$......................qrjEXaz4RUVmquy3IT5eLKXLB28ahI2' ],
16             ['2y', '$2y$12$' , 16, '$2y$08$......................qrjEXaz4RUVmquy3IT5eLKXLB28ahI2' ],
17             ['2b', '$2b$12$' , 16, '$2b$08$......................qrjEXaz4RUVmquy3IT5eLKXLB28ahI2' ],
18             [7 , '$7$DU..../....' , 16, '$7$AU..../....2Q9obwLhin8qvQl6sisAO/$E1HizYWxBmnIH4sdPkd1UOML9t62Gf.wvNTnt5XFzs8' ],
19             ['gy', '$gy$j8T$' , 16, '$gy$j9T$......................$5.2XCu2DhNfGzpifM7X8goEG2Wkio9cWIMtyWnX4tp2' ],
20             ['y' , '$y$j8T$' , 16, '$y$j9T$F5Jx5fExrKuPp53xLKQ..1$tnSYvahCwPBHKZUspmcxMfb0.WiB9W.zEaKlOBL35rC' ],
21             );
22              
23             my (%algorithm, %salt_for, $default);
24             for my $row (@possibilities) {
25             my ($name, $setting, $salt_size, $value) = @{$row};
26             my $hash = eval { crypt('password', $value) };
27             if (defined $hash and $hash eq $value) {
28             $algorithm{$name} = { settings => $setting, salt_size => $salt_size };
29             $default = $name;
30             }
31             }
32              
33             sub _get_parameters {
34 1     1   2 my %args = @_;
35              
36 1 50 33     9 if (defined(my $settings = $args{settings})) {
    50          
37 0 0       0 return ('', 2) if $settings eq '';
38              
39 0 0       0 my ($type) = $settings =~ /\A \$ ([^\$]+) \$ /x or croak "Invalid settings string '$settings'";
40 0 0       0 croak "Unsupported algorithm $type" if not $algorithm{$type};
41 0   0     0 return ($settings, $args{salt_size} // $algorithm{$type}{salt_size});
42             }
43             elsif (my $type = $args{type} // $default) {
44 1   33     3 $settings = $algorithm{$type}{settings} // croak "No such crypt type $type known";
45 1   33     7 return ($settings, $args{salt_size} // $algorithm{$type}{salt_size});
46             }
47             else {
48 0         0 return ('', 2);
49             }
50             }
51              
52             sub new {
53 1     1 1 2 my ($class, %args) = @_;
54              
55 1         4 my ($settings, $salt_size) = _get_parameters(%args);
56 1         6 return bless {
57             settings => $settings,
58             salt_size => $salt_size,
59             }, $class;
60             }
61              
62             my $base64_digits = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
63             sub _encode_crypt64 {
64 1     1   3 my $bytes = shift;
65 1         3 my $nbytes = length $bytes;
66 1         4 my $npadbytes = 2 - ($nbytes + 2) % 3;
67 1         3 $bytes .= "\0" x $npadbytes;
68 1         2 my $digits = '';
69 1         4 for(my $i = 0; $i < $nbytes; $i += 3) {
70 4         13 my $v = ord(substr $bytes, $i, 1) |
71             (ord(substr $bytes, $i + 1, 1) << 8) |
72             (ord(substr $bytes, $i + 2, 1) << 16);
73 4         14 $digits .= substr($base64_digits, $v & 0x3f, 1) .
74             substr($base64_digits, ($v >> 6) & 0x3f, 1) .
75             substr($base64_digits, ($v >> 12) & 0x3f, 1) .
76             substr($base64_digits, ($v >> 18) & 0x3f, 1);
77             }
78 1         3 substr $digits, -$npadbytes, $npadbytes, '';
79 1         3 return $digits;
80             }
81              
82              
83             sub hash_password {
84 1     1 1 3 my ($self, $password) = @_;
85 1         9 my $salt = $self->random_bytes($self->{salt_size});
86 1         10758 my $encoded_salt = _encode_crypt64($salt);
87 1         655258 return crypt($password, "$self->{settings}$encoded_salt\$");
88             }
89              
90             my $descrypt = qr{ \A [./0-9A-Za-z]{13} \z }x;
91              
92             sub accepts_hash {
93 3     3 1 10 my ($self, $hash) = @_;
94 3   66     63 return $hash =~ $descrypt || $self->SUPER::accepts_hash($hash);
95             }
96              
97             sub crypt_subtypes {
98 2     2 1 25 return sort keys %algorithm;
99             }
100              
101             sub needs_rehash {
102 1     1 1 4 my ($self, $hash) = @_;
103 1 50       15 return length $self->{settings} ? substr($hash, 0, length $self->{settings}) ne $self->{settings} : $hash !~ $descrypt;
104             }
105              
106             sub verify_password {
107 3     3 1 8 my ($class, $password, $hash) = @_;
108 3         756728 my $new_hash = crypt($password, $hash);
109 3         70 return $class->secure_compare($hash, $new_hash);
110             }
111              
112             #ABSTRACT: An system crypt() encoder for Crypt::Passphrase
113              
114             1;
115              
116             __END__