aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2012-06-27 15:36:46 +0200
committerChristian Heimes <christian@cheimes.de>2012-06-27 15:36:46 +0200
commitafa2973d7af4231b753144048328dc4d024a3b5d (patch)
tree8f9302431979a7e8bd59f13038ea00d2a5168921 /Lib/crypt.py
parentbuild_all_use_profile: Build using -fprofile-correction to automatically (diff)
downloadcpython-afa2973d7af4231b753144048328dc4d024a3b5d.tar.gz
cpython-afa2973d7af4231b753144048328dc4d024a3b5d.tar.bz2
cpython-afa2973d7af4231b753144048328dc4d024a3b5d.zip
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
Diffstat (limited to 'Lib/crypt.py')
-rw-r--r--Lib/crypt.py15
1 files changed, 8 insertions, 7 deletions
diff --git a/Lib/crypt.py b/Lib/crypt.py
index e65b0cbe4d4..ce8c85b7796 100644
--- a/Lib/crypt.py
+++ b/Lib/crypt.py
@@ -1,15 +1,16 @@
"""Wrapper to the POSIX crypt library call and associated functionality."""
import _crypt
-import string
-from random import choice
-from collections import namedtuple
+import string as _string
+from random import SystemRandom as _SystemRandom
+from collections import namedtuple as _namedtuple
-_saltchars = string.ascii_letters + string.digits + './'
+_saltchars = _string.ascii_letters + _string.digits + './'
+_sr = _SystemRandom()
-class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
+class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
"""Class representing a salt method per the Modular Crypt Format or the
legacy 2-character crypt method."""
@@ -18,7 +19,6 @@ class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
return '<crypt.METHOD_{}>'.format(self.name)
-
def mksalt(method=None):
"""Generate a salt for the specified method.
@@ -28,7 +28,7 @@ def mksalt(method=None):
if method is None:
method = methods[0]
s = '${}$'.format(method.ident) if method.ident else ''
- s += ''.join(choice(_saltchars) for _ in range(method.salt_chars))
+ s += ''.join(_sr.sample(_saltchars, method.salt_chars))
return s
@@ -60,3 +60,4 @@ for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
methods.append(_method)
methods.append(METHOD_CRYPT)
del _result, _method
+