Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daa5799

Browse files
committed
Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
by making methods() into a module attribute as it is statically calculated.
1 parent 543b7f3 commit daa5799

3 files changed

Lines changed: 60 additions & 58 deletions

File tree

Doc/library/crypt.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ are available on all platforms):
6060

6161
.. versionadded:: 3.3
6262

63+
64+
Module Attributes
65+
-----------------
66+
67+
68+
.. attribute:: methods
69+
70+
A list of available password hashing algorithms, as
71+
``crypt.METHOD_*`` objects. This list is sorted from strongest to
72+
weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
73+
74+
.. versionadded:: 3.3
75+
76+
6377
Module Functions
6478
----------------
6579

@@ -98,13 +112,6 @@ The :mod:`crypt` module defines the following functions:
98112
Before version 3.3, *salt* must be specified as a string and cannot
99113
accept ``crypt.METHOD_*`` values (which don't exist anyway).
100114

101-
.. function:: methods()
102-
103-
Return a list of available password hashing algorithms, as
104-
``crypt.METHOD_*`` objects. This list is sorted from strongest to
105-
weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
106-
107-
.. versionadded:: 3.3
108115

109116
.. function:: mksalt(method=None)
110117

Lib/crypt.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,57 @@
1-
'''Wrapper to the POSIX crypt library call and associated functionality.
2-
'''
1+
"""Wrapper to the POSIX crypt library call and associated functionality."""
32

43
import _crypt
4+
import string
5+
from random import choice
6+
from collections import namedtuple
57

6-
saltchars = 'abcdefghijklmnopqrstuvwxyz'
7-
saltchars += saltchars.upper()
8-
saltchars += '0123456789./'
98

9+
_saltchars = string.ascii_letters + string.digits + './'
1010

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
1911

20-
def __repr__(self):
21-
return '<crypt.METHOD_%s>' % self.name
12+
class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
2213

14+
"""Class representing a salt method per the Modular Crypt Format or the
15+
legacy 2-character crypt method."""
2316

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)
17+
def __repr__(self):
18+
return '<crypt.METHOD_{}>'.format(self.name)
2919

3020

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
4021

22+
def mksalt(method=None):
23+
"""Generate a salt for the specified method.
4124
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
25+
If not specified, the strongest available method will be used.
4626
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)
27+
"""
28+
if method is None:
29+
method = methods[0]
30+
s = '${}$'.format(method.ident) if method.ident else ''
31+
s += ''.join(choice(_saltchars) for _ in range(method.salt_chars))
32+
return s
5133

5234

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
35+
def crypt(word, salt=None):
36+
"""Return a string representing the one-way hash of a password, with a salt
37+
prepended.
38+
39+
If ``salt`` is not specified or is ``None``, the strongest
5640
available method will be selected and a salt generated. Otherwise,
5741
``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))
42+
returned by ``crypt.mksalt()``.
43+
44+
"""
45+
if salt is None or isinstance(salt, _Method):
46+
salt = mksalt(salt)
47+
return _crypt.crypt(word, salt)
48+
49+
50+
# available salting/crypto methods
51+
METHOD_CRYPT = _Method('CRYPT', None, 2, 13)
52+
METHOD_MD5 = _Method('MD5', '1', 8, 34)
53+
METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
54+
METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
55+
56+
methods = [METHOD_SHA512, METHOD_SHA256, METHOD_MD5, METHOD_CRYPT]
57+
methods[:-1] = [m for m in methods[:-1] if len(crypt('', m)) == m.total_size]

Lib/test/test_crypt.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@ def test_crypt(self):
1111
print('Test encryption: ', c)
1212

1313
def test_salt(self):
14-
self.assertEqual(len(crypt.saltchars), 64)
15-
for method in crypt.methods():
14+
self.assertEqual(len(crypt._saltchars), 64)
15+
for method in crypt.methods:
1616
salt = crypt.mksalt(method)
1717
self.assertEqual(len(salt),
1818
method.salt_chars + (3 if method.ident else 0))
1919

2020
def test_saltedcrypt(self):
21-
for method in crypt.methods():
21+
for method in crypt.methods:
2222
pw = crypt.crypt('assword', method)
2323
self.assertEqual(len(pw), method.total_size)
2424
pw = crypt.crypt('assword', crypt.mksalt(method))
2525
self.assertEqual(len(pw), method.total_size)
2626

2727
def test_methods(self):
28-
# Gurantee that METHOD_CRYPT is the last method in crypt.methods().
29-
methods = crypt.methods()
30-
self.assertTrue(len(methods) >= 1)
31-
self.assertEqual(crypt.METHOD_CRYPT, methods[-1])
28+
# Gurantee that METHOD_CRYPT is the last method in crypt.methods.
29+
self.assertTrue(len(crypt.methods) >= 1)
30+
self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
3231

3332
def test_main():
3433
support.run_unittest(CryptTestCase)

0 commit comments

Comments
 (0)