2007-08-15 11:28:22 -03:00
|
|
|
:mod:`crypt` --- Function to check Unix passwords
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
.. module:: crypt
|
|
|
|
:platform: Unix
|
|
|
|
:synopsis: The crypt() function used to check Unix passwords.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. moduleauthor:: Steven D. Majewski <sdm7g@virginia.edu>
|
|
|
|
.. sectionauthor:: Steven D. Majewski <sdm7g@virginia.edu>
|
|
|
|
.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/crypt.py`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: crypt(3)
|
|
|
|
pair: cipher; DES
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
This module implements an interface to the :manpage:`crypt(3)` routine, which is
|
|
|
|
a one-way hash function based upon a modified DES algorithm; see the Unix man
|
2011-02-22 06:55:44 -04:00
|
|
|
page for further details. Possible uses include storing hashed passwords
|
|
|
|
so you can check passwords without storing the actual password, or attempting
|
|
|
|
to crack Unix passwords with a dictionary.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index:: single: crypt(3)
|
|
|
|
|
|
|
|
Notice that the behavior of this module depends on the actual implementation of
|
|
|
|
the :manpage:`crypt(3)` routine in the running system. Therefore, any
|
|
|
|
extensions available on the current implementation will also be available on
|
|
|
|
this module.
|
|
|
|
|
2011-02-22 06:55:44 -04:00
|
|
|
Hashing Methods
|
|
|
|
---------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-05-28 22:27:48 -03:00
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
2011-02-22 06:55:44 -04:00
|
|
|
The :mod:`crypt` module defines the list of hashing methods (not all methods
|
|
|
|
are available on all platforms):
|
|
|
|
|
|
|
|
.. data:: METHOD_SHA512
|
|
|
|
|
|
|
|
A Modular Crypt Format method with 16 character salt and 86 character
|
2017-10-24 13:36:17 -03:00
|
|
|
hash based on the SHA-512 hash function. This is the strongest method.
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
.. data:: METHOD_SHA256
|
|
|
|
|
|
|
|
Another Modular Crypt Format method with 16 character salt and 43
|
2017-10-24 13:36:17 -03:00
|
|
|
character hash based on the SHA-256 hash function.
|
|
|
|
|
|
|
|
.. data:: METHOD_BLOWFISH
|
|
|
|
|
|
|
|
Another Modular Crypt Format method with 22 character salt and 31
|
|
|
|
character hash based on the Blowfish cipher.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
.. data:: METHOD_MD5
|
|
|
|
|
|
|
|
Another Modular Crypt Format method with 8 character salt and 22
|
2017-10-24 13:36:17 -03:00
|
|
|
character hash based on the MD5 hash function.
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
.. data:: METHOD_CRYPT
|
|
|
|
|
|
|
|
The traditional method with a 2 character salt and 13 characters of
|
|
|
|
hash. This is the weakest method.
|
|
|
|
|
2011-02-22 17:48:06 -04:00
|
|
|
|
|
|
|
Module Attributes
|
|
|
|
-----------------
|
|
|
|
|
2011-05-28 22:27:48 -03:00
|
|
|
.. versionadded:: 3.3
|
2011-02-22 17:48:06 -04:00
|
|
|
|
|
|
|
.. attribute:: methods
|
|
|
|
|
|
|
|
A list of available password hashing algorithms, as
|
|
|
|
``crypt.METHOD_*`` objects. This list is sorted from strongest to
|
2015-10-02 18:00:39 -03:00
|
|
|
weakest.
|
2011-02-22 17:48:06 -04:00
|
|
|
|
|
|
|
|
2011-02-22 06:55:44 -04:00
|
|
|
Module Functions
|
|
|
|
----------------
|
|
|
|
|
|
|
|
The :mod:`crypt` module defines the following functions:
|
|
|
|
|
|
|
|
.. function:: crypt(word, salt=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
*word* will usually be a user's password as typed at a prompt or in a graphical
|
2011-02-22 06:55:44 -04:00
|
|
|
interface. The optional *salt* is either a string as returned from
|
|
|
|
:func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all
|
|
|
|
may be available on all platforms), or a full encrypted password
|
|
|
|
including salt, as returned by this function. If *salt* is not
|
|
|
|
provided, the strongest method will be used (as returned by
|
2018-12-05 16:15:54 -04:00
|
|
|
:func:`methods`).
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
Checking a password is usually done by passing the plain-text password
|
|
|
|
as *word* and the full results of a previous :func:`crypt` call,
|
|
|
|
which should be the same as the results of this call.
|
|
|
|
|
|
|
|
*salt* (either a random 2 or 16 character string, possibly prefixed with
|
|
|
|
``$digit$`` to indicate the method) which will be used to perturb the
|
|
|
|
encryption algorithm. The characters in *salt* must be in the set
|
|
|
|
``[./a-zA-Z0-9]``, with the exception of Modular Crypt Format which
|
|
|
|
prefixes a ``$digit$``.
|
|
|
|
|
|
|
|
Returns the hashed password as a string, which will be composed of
|
|
|
|
characters from the same alphabet as the salt.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index:: single: crypt(3)
|
|
|
|
|
|
|
|
Since a few :manpage:`crypt(3)` extensions allow different values, with
|
|
|
|
different sizes in the *salt*, it is recommended to use the full crypted
|
|
|
|
password as salt when checking for a password.
|
|
|
|
|
2011-05-28 22:27:48 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
Accept ``crypt.METHOD_*`` values in addition to strings for *salt*.
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
|
2017-11-16 07:22:51 -04:00
|
|
|
.. function:: mksalt(method=None, *, rounds=None)
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
Return a randomly generated salt of the specified method. If no
|
|
|
|
*method* is given, the strongest method available as returned by
|
|
|
|
:func:`methods` is used.
|
|
|
|
|
2017-10-24 13:36:17 -03:00
|
|
|
The return value is a string suitable for passing as the *salt* argument
|
|
|
|
to :func:`crypt`.
|
|
|
|
|
2017-11-16 07:22:51 -04:00
|
|
|
*rounds* specifies the number of rounds for ``METHOD_SHA256``,
|
|
|
|
``METHOD_SHA512`` and ``METHOD_BLOWFISH``.
|
|
|
|
For ``METHOD_SHA256`` and ``METHOD_SHA512`` it must be an integer between
|
|
|
|
``1000`` and ``999_999_999``, the default is ``5000``. For
|
|
|
|
``METHOD_BLOWFISH`` it must be a power of two between ``16`` (2\ :sup:`4`)
|
|
|
|
and ``2_147_483_648`` (2\ :sup:`31`), the default is ``4096``
|
|
|
|
(2\ :sup:`12`).
|
2011-02-22 06:55:44 -04:00
|
|
|
|
2011-05-28 22:27:48 -03:00
|
|
|
.. versionadded:: 3.3
|
2011-02-22 06:55:44 -04:00
|
|
|
|
2017-10-24 13:36:17 -03:00
|
|
|
.. versionchanged:: 3.7
|
2017-11-16 07:22:51 -04:00
|
|
|
Added the *rounds* parameter.
|
2017-10-24 13:36:17 -03:00
|
|
|
|
|
|
|
|
2011-02-22 06:55:44 -04:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
2012-09-28 10:20:38 -03:00
|
|
|
A simple example illustrating typical use (a constant-time comparison
|
|
|
|
operation is needed to limit exposure to timing attacks.
|
|
|
|
:func:`hmac.compare_digest` is suitable for this purpose)::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-05-28 22:24:45 -03:00
|
|
|
import pwd
|
|
|
|
import crypt
|
|
|
|
import getpass
|
2012-09-28 10:20:38 -03:00
|
|
|
from hmac import compare_digest as compare_hash
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
def login():
|
2011-05-28 22:24:45 -03:00
|
|
|
username = input('Python login: ')
|
2007-08-15 11:28:22 -03:00
|
|
|
cryptedpasswd = pwd.getpwnam(username)[1]
|
|
|
|
if cryptedpasswd:
|
2009-01-03 17:18:54 -04:00
|
|
|
if cryptedpasswd == 'x' or cryptedpasswd == '*':
|
2011-05-28 22:24:45 -03:00
|
|
|
raise ValueError('no support for shadow passwords')
|
2007-08-15 11:28:22 -03:00
|
|
|
cleartext = getpass.getpass()
|
2012-09-28 10:20:38 -03:00
|
|
|
return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
|
2007-08-15 11:28:22 -03:00
|
|
|
else:
|
2011-05-28 22:24:45 -03:00
|
|
|
return True
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-02-22 06:55:44 -04:00
|
|
|
To generate a hash of a password using the strongest available method and
|
|
|
|
check it against the original::
|
|
|
|
|
|
|
|
import crypt
|
2012-09-28 10:20:38 -03:00
|
|
|
from hmac import compare_digest as compare_hash
|
2011-02-22 06:55:44 -04:00
|
|
|
|
|
|
|
hashed = crypt.crypt(plaintext)
|
2012-09-28 10:20:38 -03:00
|
|
|
if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
|
2016-05-10 06:01:23 -03:00
|
|
|
raise ValueError("hashed version doesn't validate against original")
|