NAME

DBIx::Class::DigestOnSet - Automatically encode columns with Digest

SYNOPSIS

In your DBIx::Class ResultSource class (the 'table' class):

__PACKAGE__->load_components(qw/DigestOnSet ... Core/);

#Simplest example. use hex encoding and SHA-1 algorithm
__PACKAGE__->add_columns(
  'password' => {
    data_type     => 'CHAR',
    size          => 40,
    digest_enable => 1,
}

#SHA-1 / hex encoding / generate check method
__PACKAGE__->add_columns(
  'password' => {
    data_type   => 'CHAR',
    size        => 40,
    digest_enable       => 1,
    digest_check_method => 'check_password',
}

#SHA-1 / binary encoding / generate check method
__PACKAGE__->add_columns(
  'password' => {
    data_type   => 'BLOB',
    size        => 20,
    digest_enable       => 1,
    digest_encoding     => 'binary',
    digest_check_method => 'check_password',
}

#MD5 /  hex encoding / generate check method
__PACKAGE__->add_columns(
  'password' => {
    data_type => 'CHAR',
    size      => 32,
    digest_enable       => 1,
    digest_algorithm    => 'MD5',
    digest_check_method => 'check_password',
}

In your application code:

#updating the value.
$row->password('plaintext');
my $digest = $row->password;

#checking against an existing value with a check_method
$row->check_password('old_password'); #true
$row->password('new_password');
$row->check_password('new_password'); #returns true
$row->check_password('old_password'); #returns false

Note: The component needs to be loaded before Core.

DESCRIPTION

This DBIx::Class component can be used to automatically encode a column's contents whenever the value of that column is set.

This module is similar to the existing DBIx::Class::DigestColumns, but there is some key differences. The main difference is that DigestColumns performs the encode operation on insert and update, and DigestOnSet performs the operation when the value is set. Another difference is that DigestOnSet supports having more than one encoded column per table using different Digest algorithms. Finally, DigestOnSet adds only one item to the namespace of the object utilizing it (_digest_encoders).

There is, unfortunately, some defficiencies that come with DigestOnSet. DigestColumns supports changing certain options at runtime, as well as the option to not automatically encode values on set. The author of this module found these options to be non essential and they were left out by design.

Options added to add_column

If any one of these options is present the column will be treated as a digest column and all of the defaults will be applied to the rest of the options.

digest_enable => 1

Enable automatic encoding of column values. If this option is not set to true any other options will become noops.

digest_check_method => $method_name

By using the digest_check_method attribute when you declare a column you can create a check method for that column. The check method accepts a plain text string, and returns a boolean that indicates whether the digest of the provided value matches the current value.

digest_encoding

The encoding to use for the digest. Valid values are 'binary', 'hex', and 'base64'. Will default to 'hex' if not specified.

digest_algorithm

The digest algorithm to use for the digest. You may specify any valid Digest algorithm. Examples are MD5, SHA-1, Whirlpool etc. Will default to 'SHA-1' if not specified.

See Digest for supported digest algorithms.

EXTENDED METHODS

The following DBIx::Class::ResultSource method is extended:

register_column - Handle the options described above.

The following DBIx::Class::Row methods are extended by this module:

new - Encode the columns on new() so that copy and create DWIM.
set_column - Encode values whenever column is set.

SEE ALSO

DBIx::Class::DigestColumns, DBIx::Class, Digest

AUTHOR

Guillermo Roditi (groditi) <groditi@cpan.org>

Inspired by the original module written by Tom Kirkpatrick (tkp) <tkp@cpan.org> featuring contributions from Guillermo Roditi (groditi) <groditi@cpan.org> and Marc Mims <marc@questright.com>

LICENSE

You may distribute this code under the same terms as Perl itself.