2323except ImportError :
2424 HAS_RANDOM = False
2525
26- try :
27- import crypt
28-
29- HAS_CRYPT = True
30- except (ImportError , PermissionError ):
31- HAS_CRYPT = False
32-
3326try :
3427 import passlib .context
3528
@@ -101,10 +94,6 @@ def secure_password(
10194 raise CommandExecutionError (str (exc ))
10295
10396
104- if HAS_CRYPT :
105- methods = {m .name .lower (): m for m in crypt .methods }
106- else :
107- methods = {}
10897known_methods = ["sha512" , "sha256" , "blowfish" , "md5" , "crypt" ]
10998
11099
@@ -130,26 +119,6 @@ def _gen_hash_passlib(crypt_salt=None, password=None, algorithm=None):
130119 return ctx .hash (** kwargs )
131120
132121
133- def _gen_hash_crypt (crypt_salt = None , password = None , algorithm = None ):
134- """
135- Generate /etc/shadow hash using the native crypt module
136- """
137- if crypt_salt is None :
138- # setting crypt_salt to the algorithm makes crypt generate
139- # a salt compatible with the specified algorithm.
140- crypt_salt = methods [algorithm ]
141- else :
142- if algorithm != "crypt" :
143- # all non-crypt algorithms are specified as part of the salt
144- crypt_salt = f"${ methods [algorithm ].ident } ${ crypt_salt } "
145-
146- try :
147- ret = crypt .crypt (password , crypt_salt )
148- except OSError :
149- ret = None
150- return ret
151-
152-
153122def gen_hash (crypt_salt = None , password = None , algorithm = None ):
154123 """
155124 Generate /etc/shadow hash
@@ -159,16 +128,12 @@ def gen_hash(crypt_salt=None, password=None, algorithm=None):
159128
160129 if algorithm is None :
161130 # prefer the most secure natively supported method
162- algorithm = crypt . methods [ 0 ]. name . lower () if HAS_CRYPT else known_methods [0 ]
131+ algorithm = known_methods [0 ]
163132
164133 if algorithm == "crypt" and crypt_salt and len (crypt_salt ) != 2 :
165134 log .warning ("Hash salt is too long for 'crypt' hash." )
166135
167- if HAS_CRYPT and algorithm in methods :
168- return _gen_hash_crypt (
169- crypt_salt = crypt_salt , password = password , algorithm = algorithm
170- )
171- elif HAS_PASSLIB and algorithm in known_methods :
136+ if HAS_PASSLIB and algorithm in known_methods :
172137 return _gen_hash_passlib (
173138 crypt_salt = crypt_salt , password = password , algorithm = algorithm
174139 )
@@ -177,6 +142,6 @@ def gen_hash(crypt_salt=None, password=None, algorithm=None):
177142 "Cannot hash using '{}' hash algorithm. Natively supported "
178143 "algorithms are: {}. If passlib is installed ({}), the supported "
179144 "algorithms are: {}." .format (
180- algorithm , list ( methods ) , HAS_PASSLIB , known_methods
145+ algorithm , [] , HAS_PASSLIB , known_methods
181146 )
182147 )
0 commit comments