File Coverage

blib/lib/Mojolicious/Plugin/BcryptSecure.pm
Criterion Covered Total %
statement 21 21 100.0
branch 6 6 100.0
condition 8 9 88.8
subroutine 6 6 100.0
pod 1 1 100.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::BcryptSecure;
2 5     5   3063 use Mojo::Base 'Mojolicious::Plugin';
  5         10  
  5         33  
3 5     5   2585 use Crypt::Eksblowfish::Bcrypt ();
  5         6663  
  5         82  
4 5     5   2147 use Crypt::URandom ();
  5         11722  
  5         1400  
5              
6             our $VERSION = '0.02';
7              
8             sub register {
9 16     16 1 12408 my (undef, $app, $config) = @_;
10              
11 16         27 my $cost;
12 16 100       43 if (exists $config->{cost}) {
13 11         19 $cost = delete $config->{cost};
14 11 100 100     135 Carp::confess 'cost must be a positive int <= 99' unless defined $cost and $cost =~ /^\d{1,2}$/ and $cost > 0;
      100        
15             } else {
16 5         10 $cost = 12;
17             }
18              
19 9 100       29 Carp::confess 'Unknown keys/values provided: ' . Mojo::Util::dumper $config if %$config;
20              
21 7         32 my $settings_without_salt = '$2a' . sprintf '$%02i', $cost;
22             $app->helper(bcrypt => sub {
23 84   66 84   17983015 return Crypt::Eksblowfish::Bcrypt::bcrypt(
24             $_[1],
25             $_[2] // $settings_without_salt . '$' . Crypt::Eksblowfish::Bcrypt::en_base64(Crypt::URandom::urandom(16)),
26             );
27 7         64 });
28              
29             $app->helper(bcrypt_validate => sub {
30 68     68   850663 return Mojo::Util::secure_compare Crypt::Eksblowfish::Bcrypt::bcrypt($_[1], $_[2]), $_[2];
31 7         727 });
32             }
33              
34             1;
35             __END__