| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | # ABSTRACT: scrypt support for DBIx::Class::EncodedColumn | ||
| 5 | package DBIx::Class::EncodedColumn::Crypt::Scrypt; | ||
| 6 | |||
| 7 | our $VERSION = '0.004'; # VERSION | ||
| 8 | |||
| 9 | use Encode qw(is_utf8 encode_utf8); | ||
| 10 | use Crypt::ScryptKDF 0.008 qw(scrypt_hash scrypt_hash_verify | ||
| 11 | random_bytes); | ||
| 12 | |||
| 13 | sub power_of_2 { | ||
| 14 | my ($x) = @_; | ||
| 15 | |||
| 16 | while($x > 1) { | ||
| 17 | return 0 if $x % 2 == 1; | ||
| 18 | $x /= 2; | ||
| 19 | } | ||
| 20 | |||
| 21 | return $x == 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub make_encode_sub { | ||
| 25 | my ($class, $col, $args) = @_; | ||
| 26 | |||
| 27 | $args->{cost} //= 8; | ||
| 28 | $args->{blocksz} //= 8; | ||
| 29 | $args->{parallel} //= 1; | ||
| 30 | $args->{saltsz} //= 32; | ||
| 31 | $args->{keysz} //= 32; | ||
| 32 | |||
| 33 | die "Cost not a power of 2" unless power_of_2($args->{cost}); | ||
| 34 | |||
| 35 | sub { | ||
| 36 | my ($text) = @_; | ||
| 37 | $text = encode_utf8($text) if is_utf8($text); | ||
| 38 | scrypt_hash( | ||
| 39 | $text, | ||
| 40 | random_bytes($args->{saltsz}), | ||
| 41 | $args->{cost}, | ||
| 42 | $args->{blocksz}, | ||
| 43 | $args->{parallel}, | ||
| 44 | $args->{keysz}); | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub make_check_sub { | ||
| 49 | my ($class, $col) = @_; | ||
| 50 | |||
| 51 | sub { | ||
| 52 | my ($result, $pass) = @_; | ||
| 53 | $pass = encode_utf8($pass) if is_utf8($pass); | ||
| 54 | scrypt_hash_verify($pass, $result->get_column($col)); | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | =pod | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | DBIx::Class::EncodedColumn::Crypt::Scrypt - scrypt support for DBIx::Class::EncodedColumn | ||
| 65 | |||
| 66 | =head1 VERSION | ||
| 67 | |||
| 68 | version 0.004 | ||
| 69 | |||
| 70 | =head1 SYNOPSIS | ||
| 71 | |||
| 72 | __PACKAGE__->add_columns( | ||
| 73 | 'password' => { | ||
| 74 | data_type => 'text', | ||
| 75 | encode_column => 1, | ||
| 76 | encode_class => 'Crypt::Scrypt', | ||
| 77 | encode_args => { | ||
| 78 | cost => 10, | ||
| 79 | keysz => 64 | ||
| 80 | }, | ||
| 81 | encode_check_method => 'check_password', | ||
| 82 | } | ||
| 83 | ) | ||
| 84 | |||
| 85 | =head1 DESCRIPTION | ||
| 86 | |||
| 87 | =head1 NAME | ||
| 88 | |||
| 89 | DBIx::Class::EncodedColumn::Crypt::Scrypt | ||
| 90 | |||
| 91 | =head1 ACCEPTED ARGUMENTS | ||
| 92 | |||
| 93 | =head2 cost | ||
| 94 | |||
| 95 | CPU/memory cost, as a power of 2. Give the exponent only. Default: 8 | ||
| 96 | |||
| 97 | =head2 blocksz | ||
| 98 | |||
| 99 | Block size. Defaults to 8. | ||
| 100 | |||
| 101 | =head2 parallel | ||
| 102 | |||
| 103 | Parallelization parameter. Defaults to 1. | ||
| 104 | |||
| 105 | =head2 saltsz | ||
| 106 | |||
| 107 | Length of salt in bytes. Defaults to 32. | ||
| 108 | |||
| 109 | =head2 keysz | ||
| 110 | |||
| 111 | Length of derived key in bytes. Defaults to 32. | ||
| 112 | |||
| 113 | =head1 METHODS | ||
| 114 | |||
| 115 | =head2 make_encode_sub($column_name, \%encode_args) | ||
| 116 | |||
| 117 | Returns a coderef that accepts a plaintext value and returns an | ||
| 118 | encoded value. | ||
| 119 | |||
| 120 | =head2 make_check_sub($column_name, \%encode_args) | ||
| 121 | |||
| 122 | Returns a coderef that when given the row object and a plaintext value | ||
| 123 | will return a boolean if the plaintext matches the encoded value. This | ||
| 124 | is typically used for password authentication. | ||
| 125 | |||
| 126 | =head1 SEE ALSO | ||
| 127 | |||
| 128 | L<DBIx::Class::EncodedColumn> | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Forest Belton <forest@homolo.gy> | ||
| 133 | |||
| 134 | =head1 COPYRIGHT AND LICENSE | ||
| 135 | |||
| 136 | This software is Copyright (c) 2014 by Forest Belton. | ||
| 137 | |||
| 138 | This is free software, licensed under: | ||
| 139 | |||
| 140 | The MIT (X11) License | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | __END__; |