O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

crypt

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.

Example

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required