blob: b6a14635b56acc2923a17c71cef35ff4ba87909d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{crypt} ---
Fred Drake38e5d272000-04-03 20:13:55 +00002 Function to check \UNIX{} passwords}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakef6863c11999-03-02 16:37:17 +00004\declaremodule{builtin}{crypt}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drake38e5d272000-04-03 20:13:55 +00006\modulesynopsis{The \cfunction{crypt()} function used to check
Fred Drakec116b822001-05-09 15:50:17 +00007 \UNIX\ passwords.}
Fred Drakef6863c11999-03-02 16:37:17 +00008\moduleauthor{Steven D. Majewski}{sdm7g@virginia.edu}
9\sectionauthor{Steven D. Majewski}{sdm7g@virginia.edu}
Fred Drake38e5d272000-04-03 20:13:55 +000010\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
Fred Drakeb91e9341998-07-23 17:59:49 +000011
Guido van Rossum5c6e3731996-04-10 16:18:20 +000012
Fred Drake38e5d272000-04-03 20:13:55 +000013This module implements an interface to the
14\manpage{crypt}{3}\index{crypt(3)} routine, which is a one-way hash
15function based upon a modified DES\indexii{cipher}{DES} algorithm; see
16the \UNIX{} man page for further details. Possible uses include
Guido van Rossum5c6e3731996-04-10 16:18:20 +000017allowing Python scripts to accept typed passwords from the user, or
Fred Drakef0867311997-12-29 17:31:22 +000018attempting to crack \UNIX{} passwords with a dictionary.
Guido van Rossum5c6e3731996-04-10 16:18:20 +000019
Martin v. Löwis37ead8f2004-07-26 12:05:16 +000020Notice that the behavior of this module depends on the actual implementation
21of the \manpage{crypt}{3}\index{crypt(3)} routine in the running system.
22Therefore, any extensions available on the current implementation will also
23be available on this module.
Fred Drakecce10901998-03-17 06:33:25 +000024\begin{funcdesc}{crypt}{word, salt}
Fred Drake38e5d272000-04-03 20:13:55 +000025 \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öwis37ead8f2004-07-26 12:05:16 +000032
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 Rossum5c6e3731996-04-10 16:18:20 +000036\end{funcdesc}
37
Fred Drake38e5d272000-04-03 20:13:55 +000038
39A simple example illustrating typical use:
40
41\begin{verbatim}
42import crypt, getpass, pwd
43
44def 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öwis37ead8f2004-07-26 12:05:16 +000051 return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
Fred Drake38e5d272000-04-03 20:13:55 +000052 else:
53 return 1
54\end{verbatim}