annotate Lib/crypt.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 62994662676a
children 74a1110a3b50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
1 """Wrapper to the POSIX crypt library call and associated functionality."""
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
2
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
3 import _crypt
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
4 import string
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
5 from random import choice
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
6 from collections import namedtuple
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
7
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
8
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
9 _saltchars = string.ascii_letters + string.digits + './'
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
10
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
11
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
12 class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
13
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
14 """Class representing a salt method per the Modular Crypt Format or the
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
15 legacy 2-character crypt method."""
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
16
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
17 def __repr__(self):
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
18 return '<crypt.METHOD_{}>'.format(self.name)
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
19
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
20
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
21
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
22 def mksalt(method=None):
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
23 """Generate a salt for the specified method.
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
24
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
25 If not specified, the strongest available method will be used.
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
26
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
27 """
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
28 if method is None:
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
29 method = methods[0]
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
30 s = '${}$'.format(method.ident) if method.ident else ''
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
31 s += ''.join(choice(_saltchars) for _ in range(method.salt_chars))
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
32 return s
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
33
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
34
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
35 def crypt(word, salt=None):
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
36 """Return a string representing the one-way hash of a password, with a salt
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
37 prepended.
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
38
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
39 If ``salt`` is not specified or is ``None``, the strongest
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
40 available method will be selected and a salt generated. Otherwise,
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
41 ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
42 returned by ``crypt.mksalt()``.
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
43
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
44 """
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
45 if salt is None or isinstance(salt, _Method):
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
46 salt = mksalt(salt)
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
47 return _crypt.crypt(word, salt)
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
48
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
49
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
50 # available salting/crypto methods
68105
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
51 METHOD_CRYPT = _Method('CRYPT', None, 2, 13)
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
52 METHOD_MD5 = _Method('MD5', '1', 8, 34)
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
53 METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
0586c699d467 Make Lib/crypt.py meet PEP 8 standards. This also led to a tweak in the new API
Brett Cannon <bcannon@gmail.com>
parents: 68097
diff changeset
54 METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
68097
372b2ef0ff37 Issue #10924: Adding salt and Modular Crypt Format to crypt library.
Sean Reifscheider <jafo@tummy.com>
parents:
diff changeset
55
68106
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
56 methods = []
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
57 for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
58 _result = crypt('', _method)
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
59 if _result and len(_result) == _method.total_size:
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
60 methods.append(_method)
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
61 methods.append(METHOD_CRYPT)
62994662676a A crypt algorithm may not be available by returning None.
Brett Cannon <bcannon@gmail.com>
parents: 68105
diff changeset
62 del _result, _method