blob: ec5a4b0ae242656508147a8660d1513ccbbb45f3 [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
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000018page for further details. Possible uses include storing hashed passwords
19so you can check passwords without storing the actual password, or attempting
20to crack Unix passwords with a dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +000021
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
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000029Hashing Methods
30---------------
Georg Brandl116aa622007-08-15 14:28:22 +000031
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000032The :mod:`crypt` module defines the list of hashing methods (not all methods
33are available on all platforms):
34
35.. data:: METHOD_SHA512
36
37 A Modular Crypt Format method with 16 character salt and 86 character
38 hash. This is the strongest method.
39
Georg Brandl08f5cf52011-02-23 07:31:24 +000040 .. versionadded:: 3.3
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000041
42.. data:: METHOD_SHA256
43
44 Another Modular Crypt Format method with 16 character salt and 43
45 character hash.
46
Georg Brandl08f5cf52011-02-23 07:31:24 +000047 .. versionadded:: 3.3
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000048
49.. data:: METHOD_MD5
50
51 Another Modular Crypt Format method with 8 character salt and 22
52 character hash.
53
Georg Brandl08f5cf52011-02-23 07:31:24 +000054 .. versionadded:: 3.3
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000055
56.. data:: METHOD_CRYPT
57
58 The traditional method with a 2 character salt and 13 characters of
59 hash. This is the weakest method.
60
Georg Brandl08f5cf52011-02-23 07:31:24 +000061 .. versionadded:: 3.3
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000062
Brett Cannondaa57992011-02-22 21:48:06 +000063
64Module Attributes
65-----------------
66
67
68.. attribute:: methods
69
70 A list of available password hashing algorithms, as
71 ``crypt.METHOD_*`` objects. This list is sorted from strongest to
72 weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
73
Georg Brandl08f5cf52011-02-23 07:31:24 +000074 .. versionadded:: 3.3
Brett Cannondaa57992011-02-22 21:48:06 +000075
76
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000077Module Functions
78----------------
79
80The :mod:`crypt` module defines the following functions:
81
82.. function:: crypt(word, salt=None)
Georg Brandl116aa622007-08-15 14:28:22 +000083
84 *word* will usually be a user's password as typed at a prompt or in a graphical
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000085 interface. The optional *salt* is either a string as returned from
86 :func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all
87 may be available on all platforms), or a full encrypted password
88 including salt, as returned by this function. If *salt* is not
89 provided, the strongest method will be used (as returned by
90 :func:`methods`.
91
92 Checking a password is usually done by passing the plain-text password
93 as *word* and the full results of a previous :func:`crypt` call,
94 which should be the same as the results of this call.
95
96 *salt* (either a random 2 or 16 character string, possibly prefixed with
97 ``$digit$`` to indicate the method) which will be used to perturb the
98 encryption algorithm. The characters in *salt* must be in the set
99 ``[./a-zA-Z0-9]``, with the exception of Modular Crypt Format which
100 prefixes a ``$digit$``.
101
102 Returns the hashed password as a string, which will be composed of
103 characters from the same alphabet as the salt.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 .. index:: single: crypt(3)
106
107 Since a few :manpage:`crypt(3)` extensions allow different values, with
108 different sizes in the *salt*, it is recommended to use the full crypted
109 password as salt when checking for a password.
110
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000111.. versionchanged:: 3.3
112 Before version 3.3, *salt* must be specified as a string and cannot
113 accept ``crypt.METHOD_*`` values (which don't exist anyway).
114
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000115
116.. function:: mksalt(method=None)
117
118 Return a randomly generated salt of the specified method. If no
119 *method* is given, the strongest method available as returned by
120 :func:`methods` is used.
121
122 The return value is a string either of 2 characters in length for
123 ``crypt.METHOD_CRYPT``, or 19 characters starting with ``$digit$`` and
124 16 random characters from the set ``[./a-zA-Z0-9]``, suitable for
125 passing as the *salt* argument to :func:`crypt`.
126
127.. versionadded:: 3.3
128
129Examples
130--------
131
Georg Brandl116aa622007-08-15 14:28:22 +0000132A simple example illustrating typical use::
133
134 import crypt, getpass, pwd
135
Georg Brandl116aa622007-08-15 14:28:22 +0000136 def login():
Georg Brandl8d5c3922007-12-02 22:48:17 +0000137 username = input('Python login:')
Georg Brandl116aa622007-08-15 14:28:22 +0000138 cryptedpasswd = pwd.getpwnam(username)[1]
139 if cryptedpasswd:
Georg Brandl48310cd2009-01-03 21:18:54 +0000140 if cryptedpasswd == 'x' or cryptedpasswd == '*':
Georg Brandl116aa622007-08-15 14:28:22 +0000141 raise "Sorry, currently no support for shadow passwords"
142 cleartext = getpass.getpass()
143 return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
144 else:
145 return 1
146
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000147To generate a hash of a password using the strongest available method and
148check it against the original::
149
150 import crypt
151
152 hashed = crypt.crypt(plaintext)
153 if hashed != crypt.crypt(plaintext, hashed):
154 raise "Hashed version doesn't validate against original"