blob: d25c626a175854ac5edca4c2f5d30a30bdb60e94 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. moduleauthor:: Steven D. Majewski <sdm7g@virginia.edu>
9.. sectionauthor:: Steven D. Majewski <sdm7g@virginia.edu>
10.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012**Source code:** :source:`Lib/crypt.py`
Georg Brandl116aa622007-08-15 14:28:22 +000013
14.. index::
15 single: crypt(3)
16 pair: cipher; DES
17
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040018--------------
19
Georg Brandl116aa622007-08-15 14:28:22 +000020This module implements an interface to the :manpage:`crypt(3)` routine, which is
21a one-way hash function based upon a modified DES algorithm; see the Unix man
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000022page for further details. Possible uses include storing hashed passwords
23so you can check passwords without storing the actual password, or attempting
24to crack Unix passwords with a dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +000025
26.. index:: single: crypt(3)
27
28Notice that the behavior of this module depends on the actual implementation of
29the :manpage:`crypt(3)` routine in the running system. Therefore, any
30extensions available on the current implementation will also be available on
31this module.
32
pxinwr236d0b72019-04-15 17:02:20 +080033.. availability:: Unix. Not available on VxWorks.
34
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000035Hashing Methods
36---------------
Georg Brandl116aa622007-08-15 14:28:22 +000037
Éric Araujof2156312011-05-29 03:27:48 +020038.. versionadded:: 3.3
39
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000040The :mod:`crypt` module defines the list of hashing methods (not all methods
41are available on all platforms):
42
43.. data:: METHOD_SHA512
44
45 A Modular Crypt Format method with 16 character salt and 86 character
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +030046 hash based on the SHA-512 hash function. This is the strongest method.
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000047
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000048.. data:: METHOD_SHA256
49
50 Another Modular Crypt Format method with 16 character salt and 43
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +030051 character hash based on the SHA-256 hash function.
52
53.. data:: METHOD_BLOWFISH
54
55 Another Modular Crypt Format method with 22 character salt and 31
56 character hash based on the Blowfish cipher.
57
58 .. versionadded:: 3.7
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000059
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000060.. data:: METHOD_MD5
61
62 Another Modular Crypt Format method with 8 character salt and 22
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +030063 character hash based on the MD5 hash function.
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000064
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000065.. data:: METHOD_CRYPT
66
67 The traditional method with a 2 character salt and 13 characters of
68 hash. This is the weakest method.
69
Brett Cannondaa57992011-02-22 21:48:06 +000070
71Module Attributes
72-----------------
73
Éric Araujof2156312011-05-29 03:27:48 +020074.. versionadded:: 3.3
Brett Cannondaa57992011-02-22 21:48:06 +000075
76.. attribute:: methods
77
78 A list of available password hashing algorithms, as
79 ``crypt.METHOD_*`` objects. This list is sorted from strongest to
Victor Stinner6661d882015-10-02 23:00:39 +020080 weakest.
Brett Cannondaa57992011-02-22 21:48:06 +000081
Brett Cannondaa57992011-02-22 21:48:06 +000082
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000083Module Functions
84----------------
85
86The :mod:`crypt` module defines the following functions:
87
88.. function:: crypt(word, salt=None)
Georg Brandl116aa622007-08-15 14:28:22 +000089
90 *word* will usually be a user's password as typed at a prompt or in a graphical
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000091 interface. The optional *salt* is either a string as returned from
92 :func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all
93 may be available on all platforms), or a full encrypted password
94 including salt, as returned by this function. If *salt* is not
95 provided, the strongest method will be used (as returned by
Andre Delfino55f41e42018-12-05 16:45:30 -030096 :func:`methods`).
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000097
98 Checking a password is usually done by passing the plain-text password
99 as *word* and the full results of a previous :func:`crypt` call,
100 which should be the same as the results of this call.
101
102 *salt* (either a random 2 or 16 character string, possibly prefixed with
103 ``$digit$`` to indicate the method) which will be used to perturb the
104 encryption algorithm. The characters in *salt* must be in the set
105 ``[./a-zA-Z0-9]``, with the exception of Modular Crypt Format which
106 prefixes a ``$digit$``.
107
108 Returns the hashed password as a string, which will be composed of
109 characters from the same alphabet as the salt.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111 .. index:: single: crypt(3)
112
113 Since a few :manpage:`crypt(3)` extensions allow different values, with
114 different sizes in the *salt*, it is recommended to use the full crypted
115 password as salt when checking for a password.
116
Éric Araujof2156312011-05-29 03:27:48 +0200117 .. versionchanged:: 3.3
118 Accept ``crypt.METHOD_*`` values in addition to strings for *salt*.
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000119
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000120
Serhiy Storchakacede8c92017-11-16 13:22:51 +0200121.. function:: mksalt(method=None, *, rounds=None)
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000122
123 Return a randomly generated salt of the specified method. If no
124 *method* is given, the strongest method available as returned by
125 :func:`methods` is used.
126
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +0300127 The return value is a string suitable for passing as the *salt* argument
128 to :func:`crypt`.
129
Serhiy Storchakacede8c92017-11-16 13:22:51 +0200130 *rounds* specifies the number of rounds for ``METHOD_SHA256``,
131 ``METHOD_SHA512`` and ``METHOD_BLOWFISH``.
132 For ``METHOD_SHA256`` and ``METHOD_SHA512`` it must be an integer between
133 ``1000`` and ``999_999_999``, the default is ``5000``. For
134 ``METHOD_BLOWFISH`` it must be a power of two between ``16`` (2\ :sup:`4`)
135 and ``2_147_483_648`` (2\ :sup:`31`), the default is ``4096``
136 (2\ :sup:`12`).
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000137
Éric Araujof2156312011-05-29 03:27:48 +0200138 .. versionadded:: 3.3
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000139
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +0300140 .. versionchanged:: 3.7
Serhiy Storchakacede8c92017-11-16 13:22:51 +0200141 Added the *rounds* parameter.
Serhiy Storchakaeab3ff72017-10-24 19:36:17 +0300142
143
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000144Examples
145--------
146
Nick Coghlan74cca702012-09-28 18:50:38 +0530147A simple example illustrating typical use (a constant-time comparison
148operation is needed to limit exposure to timing attacks.
149:func:`hmac.compare_digest` is suitable for this purpose)::
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Éric Araujobc577892011-05-29 03:24:45 +0200151 import pwd
152 import crypt
153 import getpass
Nick Coghlan74cca702012-09-28 18:50:38 +0530154 from hmac import compare_digest as compare_hash
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Georg Brandl116aa622007-08-15 14:28:22 +0000156 def login():
Éric Araujobc577892011-05-29 03:24:45 +0200157 username = input('Python login: ')
Georg Brandl116aa622007-08-15 14:28:22 +0000158 cryptedpasswd = pwd.getpwnam(username)[1]
159 if cryptedpasswd:
Georg Brandl48310cd2009-01-03 21:18:54 +0000160 if cryptedpasswd == 'x' or cryptedpasswd == '*':
Éric Araujobc577892011-05-29 03:24:45 +0200161 raise ValueError('no support for shadow passwords')
Georg Brandl116aa622007-08-15 14:28:22 +0000162 cleartext = getpass.getpass()
Nick Coghlan74cca702012-09-28 18:50:38 +0530163 return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
Georg Brandl116aa622007-08-15 14:28:22 +0000164 else:
Éric Araujobc577892011-05-29 03:24:45 +0200165 return True
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000167To generate a hash of a password using the strongest available method and
168check it against the original::
169
170 import crypt
Nick Coghlan74cca702012-09-28 18:50:38 +0530171 from hmac import compare_digest as compare_hash
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +0000172
173 hashed = crypt.crypt(plaintext)
Nick Coghlan74cca702012-09-28 18:50:38 +0530174 if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300175 raise ValueError("hashed version doesn't validate against original")