use Digest::MD5 qw(md5);
my $password = "passwd";
my $encr_pass = md5($password);
But for anything new, you should be using SHA1 (as MD5 is quite possibly on its deathbed as far as security applications go), which is done like this:
use Digest::SHA1 qw(sha1);
my $password = "passwd";
my $encr_pass = sha1($password);
Unfortunately, Digest::SHA1 doesn't come with Perl by default (the MD5 module does).
Better still, you can use simply Digest with an OO interface to specify the hash algorithm at runtime. IMHO, its important to be able to switch from one crypto/hash algorithm to another on a moment's notice, in case your current algorithm turns out to have a catastrophic security hole. The Digest.pm module helps you to do that change. Here's how to use it:
use Digest;
my $password = "passwd";
my $digest = Digest->new("SHA1");
$digest->add($password);
my $encr_pass = $digest->digest();
The OO interface in the Digest.pm module is basically the same as the interface in the Digest::MD5 and Digest::SHA1 modules (I used the functional interface for those modules above, for simplicity's sake).
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
|