11"""Wrapper to the POSIX crypt library call and associated functionality."""
22
33import _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-
2222def 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 )
6161methods .append (METHOD_CRYPT )
6262del _result , _method
63+
0 commit comments