-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.pl
More file actions
33 lines (30 loc) · 980 Bytes
/
Copy pathcrypt.pl
File metadata and controls
33 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/perl
use warnings;
use strict;
# Validate a user
# Check the db for the user
# Get the salt from the hashed password in the db
# check the password with the current salt in the db and see if it matches the passed in response
sub validate_user {
my $self = shift;
my $user = shift;
my $pass = shift;
my $sth = $self->dbh->prepare('SELECT * FROM user WHERE username = ?');
$sth->execute($user);
if (my $res = $sth->fetchrow_hashref) {
my $salt = substr($res->{'password'}, 0, 2);
return (crypt($pass, $salt) eq $res->{'password'})
? $res->{'id'}
: undef;
} else {
return undef;
}
}
# Take a given password, generate a salt for it, and return the result of crypt
sub encrypt {
my $self = shift;
my $pass = shift;
my @letters = ('A' .. 'Z', 'a' .. 'z', '0' .. '9', '/', '.');
my $salt = $letters[rand @letters] . $letters[rand @letters];
return crypt($pass, $salt);
};