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