Created
July 4, 2011 01:51
-
-
Save hoguej/1062809 to your computer and use it in GitHub Desktop.
Some modern perl code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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