Skip to content

Instantly share code, notes, and snippets.

@hoguej
Created July 4, 2011 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoguej/1062809 to your computer and use it in GitHub Desktop.
Save hoguej/1062809 to your computer and use it in GitHub Desktop.
Some modern perl code
use MooseX::Declare;
use Helper qw/Bool Str Num Int Object/;
class User {
has 'id' => Int();
has 'name' => Str();
has 'email' => Str();
has 'password_hash' => Str();
has 'salt' => Str();
has 'active' => Bool();
has 'admin' => Bool();
method authorize ( Str $password ) {
my $hash = crypt( $password, $self->salt );
if( $hash eq $self->password_hash ) {
return 1;
} else {
return 0;
}
}
method set_password ( Str $password ) {
my @salt_chars = ( '.', '/', 'a'..'z', 'A'..'Z', '0'..'9' );
my $salt = $salt_chars[rand(63)] . $salt_chars[rand(63)];
my $new_hash = crypt( $password, $salt );
$self->password_hash( $new_hash );
$self->salt( $salt );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment