| Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{crypt} --- | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 2 |          Function to check \UNIX{} passwords} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 |  | 
| Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{crypt} | 
| Fred Drake | a54a887 | 1999-03-02 17:03:42 +0000 | [diff] [blame] | 5 |   \platform{Unix} | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 6 | \modulesynopsis{The \cfunction{crypt()} function used to check | 
| Fred Drake | c116b82 | 2001-05-09 15:50:17 +0000 | [diff] [blame] | 7 |   \UNIX\ passwords.} | 
| Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 8 | \moduleauthor{Steven D. Majewski}{sdm7g@virginia.edu} | 
 | 9 | \sectionauthor{Steven D. Majewski}{sdm7g@virginia.edu} | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 10 | \sectionauthor{Peter Funk}{pf@artcom-gmbh.de} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 11 |  | 
| Guido van Rossum | 5c6e373 | 1996-04-10 16:18:20 +0000 | [diff] [blame] | 12 |  | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 13 | This module implements an interface to the | 
 | 14 | \manpage{crypt}{3}\index{crypt(3)} routine, which is a one-way hash | 
 | 15 | function based upon a modified DES\indexii{cipher}{DES} algorithm; see | 
 | 16 | the \UNIX{} man page for further details.  Possible uses include | 
| Guido van Rossum | 5c6e373 | 1996-04-10 16:18:20 +0000 | [diff] [blame] | 17 | allowing Python scripts to accept typed passwords from the user, or | 
| Fred Drake | f086731 | 1997-12-29 17:31:22 +0000 | [diff] [blame] | 18 | attempting to crack \UNIX{} passwords with a dictionary. | 
| Guido van Rossum | 5c6e373 | 1996-04-10 16:18:20 +0000 | [diff] [blame] | 19 |  | 
| Martin v. Löwis | 37ead8f | 2004-07-26 12:05:16 +0000 | [diff] [blame] | 20 | Notice that the behavior of this module depends on the actual implementation  | 
 | 21 | of the \manpage{crypt}{3}\index{crypt(3)} routine in the running system.  | 
 | 22 | Therefore, any extensions available on the current implementation will also  | 
 | 23 | be available on this module. | 
| Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 24 | \begin{funcdesc}{crypt}{word, salt}  | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 25 |   \var{word} will usually be a user's password as typed at a prompt or  | 
 | 26 |   in a graphical interface.  \var{salt} is usually a random | 
 | 27 |   two-character string which will be used to perturb the DES algorithm | 
 | 28 |   in one of 4096 ways.  The characters in \var{salt} must be in the | 
 | 29 |   set \regexp{[./a-zA-Z0-9]}.  Returns the hashed password as a | 
 | 30 |   string, which will be composed of characters from the same alphabet | 
 | 31 |    as the salt (the first two characters represent the salt itself). | 
| Martin v. Löwis | 37ead8f | 2004-07-26 12:05:16 +0000 | [diff] [blame] | 32 |  | 
 | 33 |   Since a few \manpage{crypt}{3}\index{crypt(3)} extensions allow different | 
 | 34 |   values, with different sizes in the \var{salt}, it is recommended to use  | 
 | 35 |   the full crypted password as salt when checking for a password. | 
| Guido van Rossum | 5c6e373 | 1996-04-10 16:18:20 +0000 | [diff] [blame] | 36 | \end{funcdesc} | 
 | 37 |  | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 38 |  | 
 | 39 | A simple example illustrating typical use: | 
 | 40 |  | 
 | 41 | \begin{verbatim} | 
 | 42 | import crypt, getpass, pwd | 
 | 43 |  | 
 | 44 | def login(): | 
 | 45 |     username = raw_input('Python login:') | 
 | 46 |     cryptedpasswd = pwd.getpwnam(username)[1] | 
 | 47 |     if cryptedpasswd: | 
 | 48 |         if cryptedpasswd == 'x' or cryptedpasswd == '*':  | 
 | 49 |             raise "Sorry, currently no support for shadow passwords" | 
 | 50 |         cleartext = getpass.getpass() | 
| Martin v. Löwis | 37ead8f | 2004-07-26 12:05:16 +0000 | [diff] [blame] | 51 |         return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 52 |     else: | 
 | 53 |         return 1 | 
 | 54 | \end{verbatim} |