February 2006
Intermediate to advanced
648 pages
14h 53m
English
The crypt module provides an interface to the UNIX crypt() routine that is used to encrypt passwords on many UNIX systems.
crypt(word, salt)Encrypts word using a modified DES algorithm. salt is a two-character seed used to initialize the algorithm. Returns the encrypted word as a string. Only the first eight characters of word are significant.
The following code reads a password from the user and compares it against the value in the system password database:
import getpass import pwd import crypt uname = getpass.getuser() # Get username from environment pw = getpass.getpass() # Get entered password realpw = pwd.getpwnam(uname)[1] # Get real password entrpw = crypt.crypt(pw,realpw[:2]) # Encrypt if realpw == entrpw: # Compare ...
Read now
Unlock full access