views:

92

answers:

1

Its not specific perl question I am building a perl gui/wxperl application that connect to DB . I want my application to be a password protected i.e first the user should enter the user and password and then use the appication .

what is the best secure method to store the password could someone provide an idea what is the best method to how should i store the user and the password and how should i retrieve them for authentication ? if possible could someone provide some perl code how to do this ?

+9  A: 

You definitely don't want to save the passwords in plain text, you should probably take a look at using sha256. You can use the Perl mod Digest::SHA (see CPAN for docs).

use Digest::SHA qw(sha256);
my $digest = sha256($input_password);
my $saved_digest_password = get_saved_password_for_user($input_user);
if ($digest eq $saved_digest_password){
    # they have the correct password
}

That is just pseudo code, but it should help get you started. It's up to you to define "get_saved_password_for_user" however you want to, whether that is stored in a database somewhere or on the file system or somewhere else. Just make sure you don't ever store or log the $input_password anywhere. The only thing you should need to store is the $digest password.

Hope that helps!

Matthew J Morrison
+1 even as psuedocode it gets right down to the basics of password matching.
Axeman
daotoad
This is a very good answer but as you know perl is opensource what if someone change the if ($digest eq $saved_digest_password) to if ($digest ne $saved_digest_password) then he will enter for every wrong password could someone have any idea how to solve this in elegant manner ?
oren
@oren, preventing someone from tampering with your program is a very different question, and a very difficult one. If what you're trying to do is prevent one user from accessing another's data, then you'll probably have to encrypt that data using the user's password. Then tampering with the program won't help (unless the attacker changes it to secretly save the password somewhere).
cjm
@oren - I think we need to talk about what "open source" means.
Matthew J Morrison