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

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