Blame SOURCES/00165-crypt-module-salt-backport.patch

ae2451
diff -up Python-2.7.3/Doc/library/crypt.rst.crypt-module-salt-backport Python-2.7.3/Doc/library/crypt.rst
ae2451
--- Python-2.7.3/Doc/library/crypt.rst.crypt-module-salt-backport	2012-04-09 19:07:28.000000000 -0400
ae2451
+++ Python-2.7.3/Doc/library/crypt.rst	2013-02-19 16:44:20.465334062 -0500
ae2451
@@ -16,9 +16,9 @@
ae2451
 
ae2451
 This module implements an interface to the :manpage:`crypt(3)` routine, which is
ae2451
 a one-way hash function based upon a modified DES algorithm; see the Unix man
ae2451
-page for further details.  Possible uses include allowing Python scripts to
ae2451
-accept typed passwords from the user, or attempting to crack Unix passwords with
ae2451
-a dictionary.
ae2451
+page for further details.  Possible uses include storing hashed passwords
ae2451
+so you can check passwords without storing the actual password, or attempting
ae2451
+to crack Unix passwords with a dictionary.
ae2451
 
ae2451
 .. index:: single: crypt(3)
ae2451
 
ae2451
@@ -27,15 +27,81 @@ the :manpage:`crypt(3)` routine in the r
ae2451
 extensions available on the current implementation will also  be available on
ae2451
 this module.
ae2451
 
ae2451
+Hashing Methods
ae2451
+---------------
ae2451
 
ae2451
-.. function:: crypt(word, salt)
ae2451
+The :mod:`crypt` module defines the list of hashing methods (not all methods
ae2451
+are available on all platforms):
ae2451
+
ae2451
+.. data:: METHOD_SHA512
ae2451
+
ae2451
+   A Modular Crypt Format method with 16 character salt and 86 character
ae2451
+   hash.  This is the strongest method.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+.. data:: METHOD_SHA256
ae2451
+
ae2451
+   Another Modular Crypt Format method with 16 character salt and 43
ae2451
+   character hash.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+.. data:: METHOD_MD5
ae2451
+
ae2451
+   Another Modular Crypt Format method with 8 character salt and 22
ae2451
+   character hash.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+.. data:: METHOD_CRYPT
ae2451
+
ae2451
+   The traditional method with a 2 character salt and 13 characters of
ae2451
+   hash.  This is the weakest method.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+
ae2451
+Module Attributes
ae2451
+-----------------
ae2451
+
ae2451
+
ae2451
+.. attribute:: methods
ae2451
+
ae2451
+   A list of available password hashing algorithms, as
ae2451
+   ``crypt.METHOD_*`` objects.  This list is sorted from strongest to
ae2451
+   weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+
ae2451
+Module Functions
ae2451
+----------------
ae2451
+
ae2451
+The :mod:`crypt` module defines the following functions:
ae2451
+
ae2451
+.. function:: crypt(word, salt=None)
ae2451
 
ae2451
    *word* will usually be a user's password as typed at a prompt or  in a graphical
ae2451
-   interface.  *salt* is usually a random two-character string which will be used
ae2451
-   to perturb the DES algorithm in one of 4096 ways.  The characters in *salt* must
ae2451
-   be in the set ``[./a-zA-Z0-9]``.  Returns the hashed password as a string, which
ae2451
-   will be composed of characters from the same alphabet as the salt (the first two
ae2451
-   characters represent the salt itself).
ae2451
+   interface.  The optional *salt* is either a string as returned from
ae2451
+   :func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all
ae2451
+   may be available on all platforms), or a full encrypted password
ae2451
+   including salt, as returned by this function.  If *salt* is not
ae2451
+   provided, the strongest method will be used (as returned by
ae2451
+   :func:`methods`.
ae2451
+
ae2451
+   Checking a password is usually done by passing the plain-text password
ae2451
+   as *word* and the full results of a previous :func:`crypt` call,
ae2451
+   which should be the same as the results of this call.
ae2451
+
ae2451
+   *salt* (either a random 2 or 16 character string, possibly prefixed with
ae2451
+   ``$digit$`` to indicate the method) which will be used to perturb the
ae2451
+   encryption algorithm.  The characters in *salt* must be in the set
ae2451
+   ``[./a-zA-Z0-9]``, with the exception of Modular Crypt Format which
ae2451
+   prefixes a ``$digit$``.
ae2451
+
ae2451
+   Returns the hashed password as a string, which will be composed of
ae2451
+   characters from the same alphabet as the salt.
ae2451
 
ae2451
    .. index:: single: crypt(3)
ae2451
 
ae2451
@@ -43,6 +109,27 @@ this module.
ae2451
    different sizes in the *salt*, it is recommended to use  the full crypted
ae2451
    password as salt when checking for a password.
ae2451
 
ae2451
+.. versionchanged:: 3.3
ae2451
+   Before version 3.3, *salt*  must be specified as a string and cannot
ae2451
+   accept ``crypt.METHOD_*`` values (which don't exist anyway).
ae2451
+
ae2451
+
ae2451
+.. function:: mksalt(method=None)
ae2451
+
ae2451
+   Return a randomly generated salt of the specified method.  If no
ae2451
+   *method* is given, the strongest method available as returned by
ae2451
+   :func:`methods` is used.
ae2451
+
ae2451
+   The return value is a string either of 2 characters in length for
ae2451
+   ``crypt.METHOD_CRYPT``, or 19 characters starting with ``$digit$`` and
ae2451
+   16 random characters from the set ``[./a-zA-Z0-9]``, suitable for
ae2451
+   passing as the *salt* argument to :func:`crypt`.
ae2451
+
ae2451
+.. versionadded:: 3.3
ae2451
+
ae2451
+Examples
ae2451
+--------
ae2451
+
ae2451
 A simple example illustrating typical use::
ae2451
 
ae2451
    import crypt, getpass, pwd
ae2451
@@ -59,3 +146,11 @@ A simple example illustrating typical us
ae2451
        else:
ae2451
            return 1
ae2451
 
ae2451
+To generate a hash of a password using the strongest available method and
ae2451
+check it against the original::
ae2451
+
ae2451
+   import crypt
ae2451
+
ae2451
+   hashed = crypt.crypt(plaintext)
ae2451
+   if hashed != crypt.crypt(plaintext, hashed):
ae2451
+      raise "Hashed version doesn't validate against original"
ae2451
diff -up Python-2.7.3/Lib/crypt.py.crypt-module-salt-backport Python-2.7.3/Lib/crypt.py
ae2451
--- Python-2.7.3/Lib/crypt.py.crypt-module-salt-backport	2013-02-19 16:44:20.465334062 -0500
ae2451
+++ Python-2.7.3/Lib/crypt.py	2013-02-19 16:49:56.425311089 -0500
ae2451
@@ -0,0 +1,71 @@
ae2451
+"""Wrapper to the POSIX crypt library call and associated functionality.
ae2451
+
ae2451
+Note that the ``methods`` and ``METHOD_*`` attributes are non-standard
ae2451
+extensions to Python 2.7, backported from 3.3"""
ae2451
+
ae2451
+import _crypt
ae2451
+import string as _string
ae2451
+from random import SystemRandom as _SystemRandom
ae2451
+from collections import namedtuple as _namedtuple
ae2451
+
ae2451
+
ae2451
+_saltchars = _string.ascii_letters + _string.digits + './'
ae2451
+_sr = _SystemRandom()
ae2451
+
ae2451
+
ae2451
+class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
ae2451
+
ae2451
+    """Class representing a salt method per the Modular Crypt Format or the
ae2451
+    legacy 2-character crypt method."""
ae2451
+
ae2451
+    def __repr__(self):
ae2451
+        return '<crypt.METHOD_%s>' % self.name
ae2451
+
ae2451
+
ae2451
+def mksalt(method=None):
ae2451
+    """Generate a salt for the specified method.
ae2451
+
ae2451
+    If not specified, the strongest available method will be used.
ae2451
+
ae2451
+    This is a non-standard extension to Python 2.7, backported from 3.3
ae2451
+    """
ae2451
+    if method is None:
ae2451
+        method = methods[0]
ae2451
+    s = '$%s$' % method.ident if method.ident else ''
ae2451
+    s += ''.join(_sr.sample(_saltchars, method.salt_chars))
ae2451
+    return s
ae2451
+
ae2451
+
ae2451
+def crypt(word, salt=None):
ae2451
+    """Return a string representing the one-way hash of a password, with a salt
ae2451
+    prepended.
ae2451
+
ae2451
+    If ``salt`` is not specified or is ``None``, the strongest
ae2451
+    available method will be selected and a salt generated.  Otherwise,
ae2451
+    ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
ae2451
+    returned by ``crypt.mksalt()``.
ae2451
+
ae2451
+    Note that these are non-standard extensions to Python 2.7's crypt.crypt()
ae2451
+    entrypoint, backported from 3.3: the standard Python 2.7 crypt.crypt()
ae2451
+    entrypoint requires two strings as the parameters, and does not support
ae2451
+    keyword arguments.
ae2451
+    """
ae2451
+    if salt is None or isinstance(salt, _Method):
ae2451
+        salt = mksalt(salt)
ae2451
+    return _crypt.crypt(word, salt)
ae2451
+
ae2451
+
ae2451
+#  available salting/crypto methods
ae2451
+METHOD_CRYPT = _Method('CRYPT', None, 2, 13)
ae2451
+METHOD_MD5 = _Method('MD5', '1', 8, 34)
ae2451
+METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
ae2451
+METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
ae2451
+
ae2451
+methods = []
ae2451
+for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
ae2451
+    _result = crypt('', _method)
ae2451
+    if _result and len(_result) == _method.total_size:
ae2451
+        methods.append(_method)
ae2451
+methods.append(METHOD_CRYPT)
ae2451
+del _result, _method
ae2451
+
ae2451
diff -up Python-2.7.3/Lib/test/test_crypt.py.crypt-module-salt-backport Python-2.7.3/Lib/test/test_crypt.py
ae2451
--- Python-2.7.3/Lib/test/test_crypt.py.crypt-module-salt-backport	2012-04-09 19:07:31.000000000 -0400
ae2451
+++ Python-2.7.3/Lib/test/test_crypt.py	2013-02-19 16:44:20.465334062 -0500
ae2451
@@ -10,6 +10,25 @@ class CryptTestCase(unittest.TestCase):
ae2451
         if test_support.verbose:
ae2451
             print 'Test encryption: ', c
ae2451
 
ae2451
+    def test_salt(self):
ae2451
+        self.assertEqual(len(crypt._saltchars), 64)
ae2451
+        for method in crypt.methods:
ae2451
+            salt = crypt.mksalt(method)
ae2451
+            self.assertEqual(len(salt),
ae2451
+                    method.salt_chars + (3 if method.ident else 0))
ae2451
+
ae2451
+    def test_saltedcrypt(self):
ae2451
+        for method in crypt.methods:
ae2451
+            pw = crypt.crypt('assword', method)
ae2451
+            self.assertEqual(len(pw), method.total_size)
ae2451
+            pw = crypt.crypt('assword', crypt.mksalt(method))
ae2451
+            self.assertEqual(len(pw), method.total_size)
ae2451
+
ae2451
+    def test_methods(self):
ae2451
+        # Gurantee that METHOD_CRYPT is the last method in crypt.methods.
ae2451
+        self.assertTrue(len(crypt.methods) >= 1)
ae2451
+        self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
ae2451
+
ae2451
 def test_main():
ae2451
     test_support.run_unittest(CryptTestCase)
ae2451
 
ae2451
diff -up Python-2.7.3/Modules/cryptmodule.c.crypt-module-salt-backport Python-2.7.3/Modules/cryptmodule.c
ae2451
--- Python-2.7.3/Modules/cryptmodule.c.crypt-module-salt-backport	2012-04-09 19:07:34.000000000 -0400
ae2451
+++ Python-2.7.3/Modules/cryptmodule.c	2013-02-19 16:44:20.466334063 -0500
ae2451
@@ -43,7 +43,7 @@ static PyMethodDef crypt_methods[] = {
ae2451
 };
ae2451
 
ae2451
 PyMODINIT_FUNC
ae2451
-initcrypt(void)
ae2451
+init_crypt(void)
ae2451
 {
ae2451
-    Py_InitModule("crypt", crypt_methods);
ae2451
+    Py_InitModule("_crypt", crypt_methods);
ae2451
 }
ae2451
diff -up Python-2.7.3/Modules/Setup.dist.crypt-module-salt-backport Python-2.7.3/Modules/Setup.dist
ae2451
--- Python-2.7.3/Modules/Setup.dist.crypt-module-salt-backport	2013-02-19 16:44:20.463334063 -0500
ae2451
+++ Python-2.7.3/Modules/Setup.dist	2013-02-19 16:44:20.466334063 -0500
ae2451
@@ -221,7 +221,7 @@ _ssl _ssl.c \
ae2451
 #
ae2451
 # First, look at Setup.config; configure may have set this for you.
ae2451
 
ae2451
-crypt cryptmodule.c # -lcrypt	# crypt(3); needs -lcrypt on some systems
ae2451
+_crypt _cryptmodule.c -lcrypt	# crypt(3); needs -lcrypt on some systems
ae2451
 
ae2451
 
ae2451
 # Some more UNIX dependent modules -- off by default, since these
ae2451
diff -up Python-2.7.3/setup.py.crypt-module-salt-backport Python-2.7.3/setup.py
ae2451
--- Python-2.7.3/setup.py.crypt-module-salt-backport	2013-02-19 16:44:20.425334067 -0500
ae2451
+++ Python-2.7.3/setup.py	2013-02-19 16:44:20.466334063 -0500
ae2451
@@ -693,7 +693,7 @@ class PyBuildExt(build_ext):
ae2451
             libs = ['crypt']
ae2451
         else:
ae2451
             libs = []
ae2451
-        exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
ae2451
+        exts.append( Extension('_crypt', ['_cryptmodule.c'], libraries=libs) )
ae2451
 
ae2451
         # CSV files
ae2451
         exts.append( Extension('_csv', ['_csv.c']) )