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

Skip to content

Commit afa2973

Browse files
committed
Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose
1 parent 39b1e5d commit afa2973

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

Lib/crypt.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Wrapper to the POSIX crypt library call and associated functionality."""
22

33
import _crypt
4-
import string
5-
from random import choice
6-
from collections import namedtuple
4+
import string as _string
5+
from random import SystemRandom as _SystemRandom
6+
from collections import namedtuple as _namedtuple
77

88

9-
_saltchars = string.ascii_letters + string.digits + './'
9+
_saltchars = _string.ascii_letters + _string.digits + './'
10+
_sr = _SystemRandom()
1011

1112

12-
class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
13+
class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
1314

1415
"""Class representing a salt method per the Modular Crypt Format or the
1516
legacy 2-character crypt method."""
@@ -18,7 +19,6 @@ def __repr__(self):
1819
return '<crypt.METHOD_{}>'.format(self.name)
1920

2021

21-
2222
def mksalt(method=None):
2323
"""Generate a salt for the specified method.
2424
@@ -28,7 +28,7 @@ def mksalt(method=None):
2828
if method is None:
2929
method = methods[0]
3030
s = '${}$'.format(method.ident) if method.ident else ''
31-
s += ''.join(choice(_saltchars) for _ in range(method.salt_chars))
31+
s += ''.join(_sr.sample(_saltchars, method.salt_chars))
3232
return s
3333

3434

@@ -60,3 +60,4 @@ def crypt(word, salt=None):
6060
methods.append(_method)
6161
methods.append(METHOD_CRYPT)
6262
del _result, _method
63+

Misc/NEWS

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ What's New in Python 3.3.0 Beta 2?
77

88
*Release date: xx-xxx-2012*
99

10+
Core and Builtins
11+
-----------------
12+
13+
14+
Library
15+
-------
16+
17+
- Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic
18+
purpose.
19+
1020
Extension Modules
1121
-----------------
1222

0 commit comments

Comments
 (0)