Skip to content

Commit ed55ba8

Browse files
committed
Remove use of crypt, removed module in py3.13
1 parent 0ad9b3b commit ed55ba8

File tree

2 files changed

+4
-38
lines changed

2 files changed

+4
-38
lines changed

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ looseversion
1919
tornado>=6.3.3
2020
aiohttp>=3.9.0
2121
croniter>=0.3.0,!=0.3.22; sys_platform != 'win32'
22+
passlib
2223

2324
# We need contextvars for salt-ssh.
2425
# Even on python versions which ships with contextvars in the standard library!

salt/utils/pycrypto.py

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
except 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-
3326
try:
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 = {}
10897
known_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-
153122
def 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

Comments
 (0)