crypt 
cryptPLAINTEXT,SALT
This function computes a one-way hash of a string exactly in the manner of crypt(3). This is somewhat useful for checking the password file for lousy passwords,[236] although what you really want to do is prevent people from adding the bad passwords in the first place.
crypt is intended to be a
one-way function, much like breaking eggs to make an omelette. There is
no (known) way to decrypt an encrypted password apart from exhaustive,
brute-force guessing.
When verifying an existing encrypted string, you should use the
encrypted text as the SALT (like crypt($plain, $crypted) eq $crypted). This
lets your code work with the standard crypt (and with more exotic implementations,
too).
When choosing a new SALT, you minimally
need to create a random two-character string whose characters come from
the set [./0–9A–Za–z] (like join "", (".", "/", 0..9, "A".."Z", "a".."z")[rand 64,
rand 64]). Older implementations of crypt needed only the first two characters of
the SALT, but code that gives only the first
two characters is now considered nonportable. See your local
crypt(3) manpage for details.
Here’s an example that makes sure that whoever runs this program knows his own password:
$pwd = (getpwuid ($<))[1]; # Assumes we're on Unix system "stty –echo"; # or look into Term::ReadKey on CPAN print "Password: "; chomp($word = <STDIN>); print "\n"; system "stty echo"; if ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access