blob: 0be571eb513fe7d650ea2d5efc3fd25750f22c0f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`crypt` --- Function to check Unix passwords
2=================================================
3
4.. module:: crypt
5 :platform: Unix
6 :synopsis: The crypt() function used to check Unix passwords.
7.. moduleauthor:: Steven D. Majewski <sdm7g@virginia.edu>
8.. sectionauthor:: Steven D. Majewski <sdm7g@virginia.edu>
9.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
10
11
12.. index::
13 single: crypt(3)
14 pair: cipher; DES
15
16This module implements an interface to the :manpage:`crypt(3)` routine, which is
17a one-way hash function based upon a modified DES algorithm; see the Unix man
18page for further details. Possible uses include allowing Python scripts to
19accept typed passwords from the user, or attempting to crack Unix passwords with
20a dictionary.
21
22.. index:: single: crypt(3)
23
24Notice that the behavior of this module depends on the actual implementation of
25the :manpage:`crypt(3)` routine in the running system. Therefore, any
26extensions available on the current implementation will also be available on
27this module.
28
29
30.. function:: crypt(word, salt)
31
32 *word* will usually be a user's password as typed at a prompt or in a graphical
33 interface. *salt* is usually a random two-character string which will be used
34 to perturb the DES algorithm in one of 4096 ways. The characters in *salt* must
35 be in the set ``[./a-zA-Z0-9]``. Returns the hashed password as a string, which
36 will be composed of characters from the same alphabet as the salt (the first two
37 characters represent the salt itself).
38
39 .. index:: single: crypt(3)
40
41 Since a few :manpage:`crypt(3)` extensions allow different values, with
42 different sizes in the *salt*, it is recommended to use the full crypted
43 password as salt when checking for a password.
44
45A simple example illustrating typical use::
46
47 import crypt, getpass, pwd
48
Georg Brandl116aa622007-08-15 14:28:22 +000049 def login():
Georg Brandl8d5c3922007-12-02 22:48:17 +000050 username = input('Python login:')
Georg Brandl116aa622007-08-15 14:28:22 +000051 cryptedpasswd = pwd.getpwnam(username)[1]
52 if cryptedpasswd:
Georg Brandl48310cd2009-01-03 21:18:54 +000053 if cryptedpasswd == 'x' or cryptedpasswd == '*':
Georg Brandl116aa622007-08-15 14:28:22 +000054 raise "Sorry, currently no support for shadow passwords"
55 cleartext = getpass.getpass()
56 return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
57 else:
58 return 1
59