diff -urN python3-trunk/Doc/library/crypt.rst py3-crypt2/Doc/library/crypt.rst --- python3-trunk/Doc/library/crypt.rst 2009-07-23 18:35:12.000000000 -0600 +++ py3-crypt2/Doc/library/crypt.rst 2011-01-18 21:41:52.484955969 -0700 @@ -15,26 +15,73 @@ 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 -page for further details. Possible uses include allowing Python scripts to -accept typed passwords from the user, or attempting to crack Unix passwords with -a dictionary. +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. .. index:: single: crypt(3) -Notice that the behavior of this module depends on the actual implementation of +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. +Hashing Methods +--------------- -.. function:: crypt(word, salt) +The :mod:`crypt` module defines the list of hashing methods (not all methods +are available on all platforms): - *word* will usually be a user's password as typed at a prompt or in a graphical - interface. *salt* is usually a random two-character string which will be used - to perturb the DES algorithm in one of 4096 ways. The characters in *salt* must - be in the set ``[./a-zA-Z0-9]``. Returns the hashed password as a string, which - will be composed of characters from the same alphabet as the salt (the first two - characters represent the salt itself). +.. data:: METHOD_SHA512 + + A Modular Crypt Format method with 16 character salt and 86 character + hash. This is the strongest method. + +.. versionadded:: 3.3 + +.. data:: METHOD_SHA256 + + Another Modular Crypt Format method with 16 character salt and 43 + character hash. + +.. versionadded:: 3.3 + +.. data:: METHOD_MD5 + + Another Modular Crypt Format method with 8 character salt and 22 + character hash. + +.. versionadded:: 3.3 + +.. data:: METHOD_CRYPT + + The traditional method with a 2 character salt and 13 characters of + hash. This is the weakest method. + +.. versionadded:: 3.3 + +Module Functions +---------------- + +The :mod:`crypt` module defines the following functions: + +.. function:: crypt(word, salt=None) + + *word* will usually be a user's password as typed at a prompt or in + a graphical interface. + + 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* 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 (from the `methods` + list). + + Returns the hashed password as a string, prefixed by the salt. .. index:: single: crypt(3) @@ -42,6 +89,37 @@ different sizes in the *salt*, it is recommended to use the full crypted password as salt when checking for a password. +.. versionchanged:: 3.3 + Before version 3.3, *salt* must be specified as a string and cannot + accept ``crypt.METHOD_*`` values (which don't exist anyway). + +.. data:: methods + + A list of available password hashing algorithms, as ``crypt.METHOD_*`` + objects. This list is sorted from strongest to weakest, and is + guaranteed to have at least ``crypt.METHOD_CRYPT``. + +.. versionadded:: 3.3 + +.. function:: mksalt(method=None) + + 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. + + The return value is a string either of 2 characters in length for + ``crypt.METHOD_CRYPT``, or 19 characters starting with ``$digit$`` and + 16 random characters from the set ``[./a-zA-Z0-9]``, suitable for + passing as the *salt* argument to :func:`crypt`. + + NOTE: Multiple calls to this function with the same argument will almost + certainly produce different return values. + +.. versionadded:: 3.3 + +Examples +-------- + A simple example illustrating typical use:: import crypt, getpass, pwd @@ -57,3 +135,11 @@ else: return 1 +To generate a hash of a password using the strongest available method and +check it against the original:: + + import crypt + + hashed = crypt.crypt(plaintext) + if hashed != crypt.crypt(plaintext, hashed): + raise "Hashed version doesn't validate against original" diff -urN python3-trunk/Lib/crypt.py py3-crypt2/Lib/crypt.py --- python3-trunk/Lib/crypt.py 1969-12-31 17:00:00.000000000 -0700 +++ py3-crypt2/Lib/crypt.py 2011-01-18 20:58:00.770467342 -0700 @@ -0,0 +1,60 @@ +'''Wrapper to the POSIX crypt library call and associated functionality. +''' + +import _crypt +import string + +saltchars = string.ascii_letters + string.digits + './' + + +class _MethodClass: + '''Class representing a salt method per the Modular Crypt Format or the + legacy 2-character crypt method.''' + def __init__(self, name, ident, salt_chars, total_size): + self.name = name + self.ident = ident + self.salt_chars = salt_chars + self.total_size = total_size + + def __repr__(self): + return '' % self.name + + +def mksalt(method = None): + '''Generate a salt for the specified method. If not specified, the + strongest available method will be used.''' + import random + + if method == None: + method = methods[0] + s = '$%s$' % method.ident if method.ident else '' + s += ''.join([ random.choice(saltchars) for x in range(method.salt_chars) ]) + return(s) + + +def crypt(word, salt = None): + '''Return a string representing the one-way hash of a password, preturbed + by a salt. If ``salt`` is not specified or is ``None``, the strongest + available method will be selected and a salt generated. Otherwise, + ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as + returned by ``crypt.mksalt()``.''' + if salt == None: + salt = mksalt() + elif isinstance(salt, _MethodClass): + salt = mksalt(salt) + return(_crypt.crypt(word, salt)) + + +# available salting/crypto methods +METHOD_CRYPT = _MethodClass('CRYPT', None, 2, 13) +METHOD_MD5 = _MethodClass('MD5', '1', 8, 34) +METHOD_SHA256 = _MethodClass('SHA256', '5', 16, 63) +METHOD_SHA512 = _MethodClass('SHA512', '6', 16, 106) + + +# build a list of methods, strongest to weakest, guaranteed to have at +# least METHOD_CRYPT +methods = sorted([ x for x in locals().values() + if isinstance(x, _MethodClass) and x != METHOD_CRYPT and + len(crypt('', x)) == x.total_size ], + key = lambda x: x.total_size, reverse = True) + [ METHOD_CRYPT ] diff -urN python3-trunk/Lib/test/test_crypt.py py3-crypt2/Lib/test/test_crypt.py --- python3-trunk/Lib/test/test_crypt.py 2010-03-19 17:06:52.000000000 -0600 +++ py3-crypt2/Lib/test/test_crypt.py 2011-01-18 20:56:52.904145483 -0700 @@ -10,6 +10,23 @@ if support.verbose: print('Test encryption: ', c) + def test_salt(self): + self.assertEqual(len(crypt.saltchars), 64) + for method in crypt.methods: + salt = crypt.mksalt(method) + self.assertEqual(len(salt), + method.salt_chars + (3 if method.ident else 0)) + + def test_saltedcrypt(self): + for method in crypt.methods: + pw = crypt.crypt('assword', method) + self.assertEqual(len(pw), method.total_size) + pw = crypt.crypt('assword', crypt.mksalt(method)) + self.assertEqual(len(pw), method.total_size) + + def test_methods(self): + self.assertTrue(len(crypt.methods) > 1) + def test_main(): support.run_unittest(CryptTestCase) diff -urN python3-trunk/Modules/Setup.dist py3-crypt2/Modules/Setup.dist --- python3-trunk/Modules/Setup.dist 2010-09-08 19:01:39.000000000 -0600 +++ py3-crypt2/Modules/Setup.dist 2011-01-18 13:43:50.599940565 -0700 @@ -207,7 +207,7 @@ # # First, look at Setup.config; configure may have set this for you. -#crypt cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems +#_crypt _cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems # Some more UNIX dependent modules -- off by default, since these diff -urN python3-trunk/Modules/_cryptmodule.c py3-crypt2/Modules/_cryptmodule.c --- python3-trunk/Modules/_cryptmodule.c 1969-12-31 17:00:00.000000000 -0700 +++ py3-crypt2/Modules/_cryptmodule.c 2011-01-18 13:43:50.599940565 -0700 @@ -0,0 +1,62 @@ +/* cryptmodule.c - by Steve Majewski + */ + +#include "Python.h" + +#include + +#ifdef __VMS +#include +#endif + +/* Module crypt */ + + +static PyObject *crypt_crypt(PyObject *self, PyObject *args) +{ + char *word, *salt; +#ifndef __VMS + extern char * crypt(const char *, const char *); +#endif + + if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) { + return NULL; + } + /* On some platforms (AtheOS) crypt returns NULL for an invalid + salt. Return None in that case. XXX Maybe raise an exception? */ + return Py_BuildValue("s", crypt(word, salt)); + +} + +PyDoc_STRVAR(crypt_crypt__doc__, +"crypt(word, salt) -> string\n\ +word will usually be a user's password. salt is a 2-character string\n\ +which will be used to select one of 4096 variations of DES. The characters\n\ +in salt must be either \".\", \"/\", or an alphanumeric character. Returns\n\ +the hashed password as a string, which will be composed of characters from\n\ +the same alphabet as the salt."); + + +static PyMethodDef crypt_methods[] = { + {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__}, + {NULL, NULL} /* sentinel */ +}; + + +static struct PyModuleDef cryptmodule = { + PyModuleDef_HEAD_INIT, + "_crypt", + NULL, + -1, + crypt_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit__crypt(void) +{ + return PyModule_Create(&cryptmodule); +} diff -urN python3-trunk/setup.py py3-crypt2/setup.py --- python3-trunk/setup.py 2011-01-02 16:29:39.000000000 -0700 +++ py3-crypt2/setup.py 2011-01-18 13:43:50.599940565 -0700 @@ -636,7 +636,7 @@ libs = ['crypt'] else: libs = [] - exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) ) + exts.append( Extension('_crypt', ['_cryptmodule.c'], libraries=libs) ) # CSV files exts.append( Extension('_csv', ['_csv.c']) )