The crypt module provides an interface to the UNIX crypt() routine that is used to encrypt passwords on many UNIX systems.
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) # Get real password entrpw = crypt.crypt(pw,realpw[:2]) # Encrypt if realpw == entrpw: # Compare ...