ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActivePython 2.5
Python Documentation
Library Reference
16. Unix Specific Services
16.1 posix -- The most common POSIX system calls
16.2 pwd -- The password database
16.3 spwd -- The shadow password database
16.4 grp -- The group database
16.5 crypt -- Function to check Unix passwords
16.6 dl -- Call C functions in shared objects
16.7 termios -- POSIX style tty control
16.8 tty -- Terminal control functions
16.9 pty -- Pseudo-terminal utilities
16.10 fcntl -- The fcntl() and ioctl() system calls
16.11 pipes -- Interface to shell pipelines
16.12 posixfile -- File-like objects with locking support
16.13 resource -- Resource usage information
16.14 nis -- Interface to Sun's NIS (Yellow Pages)
16.15 syslog -- Unix syslog library routines
16.16 commands -- Utilities for running commands

MyASPN >> Reference >> ActivePython 2.5 >> Python Documentation >> Library Reference >> 16. Unix Specific Services
ActivePython 2.5 documentation

16.5 crypt -- Function to check Unix passwords

Availability: Unix.

This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further details. Possible uses include allowing Python scripts to accept typed passwords from the user, or attempting to crack Unix passwords with a dictionary.

Notice that the behavior of this module depends on the actual implementation of the crypt(3) routine in the running system. Therefore, any extensions available on the current implementation will also be available on this module.

crypt( word, salt)
word will usually be a user's password as typed at a prompt or in a graphical interface. salt is usually a random two-character string which will be used to perturb the DES algorithm in one of 4096 ways. The characters in salt must be in the set [./a-zA-Z0-9]. Returns the hashed password as a string, which will be composed of characters from the same alphabet as the salt (the first two characters represent the salt itself).

Since a few crypt(3) extensions allow different values, with different sizes in the salt, it is recommended to use the full crypted password as salt when checking for a password.

A simple example illustrating typical use:

import crypt, getpass, pwd

def login():
    username = raw_input('Python login:')
    cryptedpasswd = pwd.getpwnam(username)[1]
    if cryptedpasswd:
        if cryptedpasswd == 'x' or cryptedpasswd == '*': 
            raise "Sorry, currently no support for shadow passwords"
        cleartext = getpass.getpass()
        return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
    else:
        return 1
See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved