Issue #10924: Adding salt and Modular Crypt Format to crypt library. · pythoncapi/cpython@e2dfefb · GitHub
Skip to content

Commit e2dfefb

Browse files
author
Sean Reifscheider
committed
Issue python#10924: Adding salt and Modular Crypt Format to crypt library.
1 parent f304278 commit e2dfefb

7 files changed

Lines changed: 183 additions & 13 deletions

File tree

Doc/library/crypt.rst

Lines changed: 97 additions & 9 deletions

Lib/crypt.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''Wrapper to the POSIX crypt library call and associated functionality.
2+
'''
3+
4+
import _crypt
5+
6+
saltchars = 'abcdefghijklmnopqrstuvwxyz'
7+
saltchars += saltchars.upper()
8+
saltchars += '0123456789./'
9+
10+
11+
class _MethodClass:
12+
'''Class representing a salt method per the Modular Crypt Format or the
13+
legacy 2-character crypt method.'''
14+
def __init__(self, name, ident, salt_chars, total_size):
15+
self.name = name
16+
self.ident = ident
17+
self.salt_chars = salt_chars
18+
self.total_size = total_size
19+
20+
def __repr__(self):
21+
return '<crypt.METHOD_%s>' % self.name
22+
23+
24+
# available salting/crypto methods
25+
METHOD_CRYPT = _MethodClass('CRYPT', None, 2, 13)
26+
METHOD_MD5 = _MethodClass('MD5', '1', 8, 34)
27+
METHOD_SHA256 = _MethodClass('SHA256', '5', 16, 63)
28+
METHOD_SHA512 = _MethodClass('SHA512', '6', 16, 106)
29+
30+
31+
def methods():
32+
'''Return a list of methods that are available in the platform ``crypt()``
33+
library, sorted from strongest to weakest. This is guaranteed to always
34+
return at least ``[METHOD_CRYPT]``'''
35+
method_list = [ METHOD_SHA512, METHOD_SHA256, METHOD_MD5 ]
36+
ret = [ method for method in method_list
37+
if len(crypt('', method)) == method.total_size ]
38+
ret.append(METHOD_CRYPT)
39+
return ret
40+
41+
42+
def mksalt(method = None):
43+
'''Generate a salt for the specified method. If not specified, the
44+
strongest available method will be used.'''
45+
import random
46+
47+
if method == None: method = methods()[0]
48+
s = '$%s$' % method.ident if method.ident else ''
49+
s += ''.join([ random.choice(saltchars) for x in range(method.salt_chars) ])
50+
return(s)
51+
52+
53+
def crypt(word, salt = None):
54+
'''Return a string representing the one-way hash of a password, preturbed
55+
by a salt. If ``salt`` is not specified or is ``None``, the strongest
56+
available method will be selected and a salt generated. Otherwise,
57+
``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
58+
returned by ``crypt.mksalt()``.'''
59+
if salt == None: salt = mksalt()
60+
elif isinstance(salt, _MethodClass): salt = mksalt(salt)
61+
return(_crypt.crypt(word, salt))

Lib/test/test_crypt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ def test_crypt(self):
1010
if support.verbose:
1111
print('Test encryption: ', c)
1212

13+
def test_salt(self):
14+
self.assertEqual(len(crypt.saltchars), 64)
15+
for method in crypt.methods():
16+
salt = crypt.mksalt(method)
17+
self.assertEqual(len(salt),
18+
method.salt_chars + (3 if method.ident else 0))
19+
20+
def test_saltedcrypt(self):
21+
for method in crypt.methods():
22+
pw = crypt.crypt('assword', method)
23+
self.assertEqual(len(pw), method.total_size)
24+
pw = crypt.crypt('assword', crypt.mksalt(method))
25+
self.assertEqual(len(pw), method.total_size)
26+
27+
def test_methods(self):
28+
self.assertTrue(len(crypt.methods()) > 1)
29+
1330
def test_main():
1431
support.run_unittest(CryptTestCase)
1532

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #10924: Adding salt and Modular Crypt Format to crypt library.
31+
Moved old C wrapper to _crypt, and added a Python wrapper with
32+
enhanced salt generation and simpler API for password generation.
33+
3034
- Issue #11074: Make 'tokenize' so it can be reloaded.
3135

3236
- Issue #11085: Moved collections abstract base classes into a separate

Modules/Setup.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ _symtable symtablemodule.c
207207
#
208208
# First, look at Setup.config; configure may have set this for you.
209209

210-
#crypt cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems
210+
#_crypt _cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems
211211

212212

213213
# Some more UNIX dependent modules -- off by default, since these
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static PyMethodDef crypt_methods[] = {
4545

4646
static struct PyModuleDef cryptmodule = {
4747
PyModuleDef_HEAD_INIT,
48-
"crypt",
48+
"_crypt",
4949
NULL,
5050
-1,
5151
crypt_methods,
@@ -56,7 +56,7 @@ static struct PyModuleDef cryptmodule = {
5656
};
5757

5858
PyMODINIT_FUNC
59-
PyInit_crypt(void)
59+
PyInit__crypt(void)
6060
{
6161
return PyModule_Create(&cryptmodule);
6262
}

setup.py

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)