| 1 | package DBIx::Class::EncodedColumn::Crypt::Eksblowfish::Bcrypt; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Crypt::Eksblowfish::Bcrypt (); | ||
| 6 | use Encode qw(is_utf8 encode_utf8); | ||
| 7 | |||
| 8 | our $VERSION = '0.00001'; | ||
| 9 | |||
| 10 | sub make_encode_sub { | ||
| 11 | my($class, $col, $args) = @_; | ||
| 12 | my $cost = exists $args->{cost} ? $args->{cost} : 8; | ||
| 13 | my $nul = exists $args->{key_nul} ? $args->{key_nul} : 1; | ||
| 14 | |||
| 15 | die("Valid 'key_null' values are '1' and '0'. You used '${nul}'.") | ||
| 16 | unless $nul =~ /^[01]$/; | ||
| 17 | die("Valid 'cost' are 1 or 2 digit integers. You used '${cost}'.") | ||
| 18 | unless $cost =~ /^\d\d?$/; | ||
| 19 | |||
| 20 | $nul = $nul ? 'a' : ''; | ||
| 21 | $cost = sprintf("%02i", 0+$cost); | ||
| 22 | |||
| 23 | # It must begin with "$2", optional "a", "$", two digits, "$" | ||
| 24 | my $settings_base = join('','$2',$nul,'$',$cost, '$'); | ||
| 25 | |||
| 26 | my $encoder = sub { | ||
| 27 | my ($plain_text, $settings_str) = @_; | ||
| 28 | if ( is_utf8($plain_text) ) { | ||
| 29 | # Bcrypt expects octets | ||
| 30 | $plain_text = encode_utf8($plain_text); | ||
| 31 | } | ||
| 32 | unless ( $settings_str ) { | ||
| 33 | my $salt = join('', map { chr(int(rand(256))) } 1 .. 16); | ||
| 34 | $salt = Crypt::Eksblowfish::Bcrypt::en_base64( $salt ); | ||
| 35 | $settings_str = $settings_base.$salt; | ||
| 36 | } | ||
| 37 | return Crypt::Eksblowfish::Bcrypt::bcrypt($plain_text, $settings_str); | ||
| 38 | }; | ||
| 39 | |||
| 40 | return $encoder; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub make_check_sub { | ||
| 44 | my($class, $col, $args) = @_; | ||
| 45 | |||
| 46 | #fast fast fast | ||
| 47 | return eval qq^ sub { | ||
| 48 | my \$col_v = \$_[0]->get_column('${col}'); | ||
| 49 | return unless defined \$col_v; | ||
| 50 | \$_[0]->_column_encoders->{${col}}->(\$_[1], \$col_v) eq \$col_v; | ||
| 51 | } ^ || die($@); | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__; |