Mojolicious-Plugin-Bcrypt

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    $settings is an optional string which encodes the algorithm parameters,
    as described in Crypt::Eksblowfish::Bcrypt.

        sub signup {
            my $self = shift;
            my $crypted_pass = $self->bcrypt( $self->param('password') );
            ...
        }

  bcrypt_validate
    Validates a password against a crypted copy (for example from your
    database).

        sub login {
            my $self = shift;
            my $entered_pass = $self->param('password');
            my $crypted_pass = $self->get_password_from_db();
            if ( $self->bcrypt_validate( $entered_pass, $crypted_pass ) ) {

                # Authenticated
                ...;
            }
            else {

                # Wrong password
                ...;
            }
        }

lib/Mojolicious/Plugin/Bcrypt.pm  view on Meta::CPAN

            my ( $password, $settings ) = @_;
            unless ( defined $settings && $settings =~ /^\$2a\$/ ) {
                my $cost = sprintf('%02d', $config->{cost} || 6);
                $settings = join( '$', '$2a', $cost, _salt() );
            }
            return bcrypt( $password, $settings );
        }
    );

    $app->helper(
        bcrypt_validate => sub {
            my $c = shift;
            my ( $plain, $crypted ) = @_;
            return $c->bcrypt( $plain, $crypted ) eq $crypted;
        }
    );
}

sub _salt {
    my $num = 999999;
    my $cr = crypt( rand($num), rand($num) ) . crypt( rand($num), rand($num) );

lib/Mojolicious/Plugin/Bcrypt.pm  view on Meta::CPAN


C<$settings> is an optional string which encodes the algorithm parameters, as
described in L<Crypt::Eksblowfish::Bcrypt>.

    sub signup {
        my $self = shift;
        my $crypted_pass = $self->bcrypt( $self->param('password') );
        ...
    }

=head2 bcrypt_validate

Validates a password against a crypted copy (for example from your database).

    sub login {
        my $self = shift;
        my $entered_pass = $self->param('password');
        my $crypted_pass = $self->get_password_from_db();
        if ( $self->bcrypt_validate( $entered_pass, $crypted_pass ) ) {

            # Authenticated
            ...;
        }
        else {

            # Wrong password
            ...;
        }
    }

t/10-logic.t  view on Meta::CPAN


get '/bc' => sub {
    my $self = shift;
    my ( $p, $s ) = map { $self->param($_) } qw/p s/;
    $self->render( text => $self->bcrypt( $p, $s ) );
};

get '/bv' => sub {
    my $self = shift;
    my ( $p, $c ) = map { $self->param($_) } qw/p c/;
    my $ok = $self->bcrypt_validate( $p, $c );
    $self->render( text => ($ok ? 'Pass' : 'Fail') );
};

my $t = Test::Mojo->new();
my @A = <DATA>;

for (@A) {
    chomp;
    s/([^ ]+) ([^ ]+) *//;
    my ( $settings, $hash ) = ( $1, $2 );
    my $encoded = encode("utf-8", $_);
    $t->get_ok("/bc?p=$encoded&s=$settings")->content_is( $settings . $hash );
    $t->get_ok( "/bv?p=$encoded&c=" . $settings . $hash, encode("utf-8", $_) );
}

my $password = 'big secret';
my $bcrypted = app->bcrypt($password);
ok( app->bcrypt_validate( $password, $bcrypted ), 'accept ok' );
ok( !app->bcrypt_validate( 'meow!', $bcrypted ), 'deny ok' );

__DATA__
$2a$06$cDTyXCPyZ0npLBTSbVTSTe 7GWMx9.3G/fpj8oDiyuQdsa2iqpFGmO
$2a$06$OxDCTUayLyPtLRWxbhPoPe r8io68QbDErcImQ1oQKuFgO5Vkawfuu password
$2a$06$LULFY1a3ZyXTLhLqRDb/Qe kyfCo7Mcdq3yim3Qvkcwt3j6WkGkotu 0
$2a$06$YDDSKEnPLDi0MRDxTU3zKu iGlbH4EazT7YiiSSbAGONYfPYZLfm3m short skirt and long jacket
$2a$06$TRTxb0bYKRbxLB/2SiX0PO NmMUp3S1PE0XxrPCOIyF9Y01irLMmgi нова загора



( run in 0.829 second using v1.01-cache-2.11-cpan-39bf76dae61 )