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

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.