Skip to content

Commit

Permalink
Use legacycrypt where crypt isn't available
Browse files Browse the repository at this point in the history
Since Python 3.13 has removed the crypt module, add legacycrypt to the
optional requirements, attempting to import it if the crypt module
isn't found.

Fixes networktocode#594
  • Loading branch information
s-t-e-v-e-n-k committed Dec 6, 2024
1 parent b35bad3 commit 95569f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions netutils/password.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Functions for working with Passwords."""

# TODO: Swap out crypt prior to py3.13
import crypt # pylint: disable=deprecated-module
import random
import secrets
import string
Expand Down Expand Up @@ -247,6 +245,13 @@ def encrypt_cisco_type5(unencrypted_password: str, salt: t.Optional[str] = None,
'$1$MHkb$v2MFmDkQX66TTxLkFF50K/'
>>>
"""
try:
import crypt # pylint: disable=deprecated-module
except ModuleNotFoundError:
try:
import legacycrypt as crypt
except ModuleNotFoundError:
raise ValueError("Crypt module not available")
if not salt:
salt = "".join(secrets.choice(ALPHABET) for _ in range(salt_len))
elif not set(salt) <= set(ALPHABET):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ napalm = {version = "^4.0.0", optional = true}
jsonschema = {version = "^4.17.3", optional = true}

[tool.poetry.extras]
optionals = ["jsonschema", "napalm"]
optionals = ["jsonschema", "napalm", "legacycrypt"]

[tool.poetry.group.dev.dependencies]
bandit = "*"
Expand Down

0 comments on commit 95569f6

Please sign in to comment.