Mercurial > cpython-lru_cache-weakref
annotate Lib/crypt.py @ 83447:678564fc0962 3.3
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 19 Apr 2013 05:45:27 +0300 |
| parents | a0cfb8c7dfb3 |
| children | e8a314fe248b |
| 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 |
|
77814
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
4 import string as _string |
|
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
5 from random import SystemRandom as _SystemRandom |
|
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
6 from collections import namedtuple as _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 |
|
77814
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
9 _saltchars = _string.ascii_letters + _string.digits + './' |
|
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
10 _sr = _SystemRandom() |
|
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
|
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 |
|
77814
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
13 class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')): |
|
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
|
14 |
|
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 """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
|
16 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
|
17 |
|
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 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
|
19 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
|
20 |
|
68097
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 '' |
|
77814
74a1110a3b50
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Christian Heimes <christian@cheimes.de>
parents:
68106
diff
changeset
|
31 s += ''.join(_sr.sample(_saltchars, method.salt_chars)) |
|
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
|
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 |
