Skip to content

Commit 4eb9419

Browse files
author
Mathieu Payrol
committed
fix: correctly handle salt=0 in encrypt_cisco_type7
The function `encrypt_cisco_type7` was incorrectly handling the integer `0` as a missing argument because of the thruthiness check (`if not salt:`). This resulted in a random salt being used when the user explicitely requested salt `0`. The condition has been updated to `if salt is None` to differentiate between a provided `0` and the default `None`.
1 parent 985fd84 commit 4eb9419

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

changes/741.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the logic error where `salt=0` was ignored in `encrypt_cisco_type7`

netutils/password.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def encrypt_cisco_type7(unencrypted_password: str, salt: t.Optional[int] = None)
292292
if len(unencrypted_password) > ENCRYPT_TYPE7_LENGTH:
293293
raise ValueError("Password must not exceed 25 characters.")
294294

295-
if not salt:
295+
if salt is None:
296296
salt = random.randint(0, 15) # noqa: S311
297297
# Start building the encrypted password - pre-pend the 2 decimal digit offset.
298298
encrypted_password = format(salt, "02d")

tests/unit/test_password.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
"sent": {"unencrypted_password": "cisco", "salt": 10},
8787
"received": "104D000A0618",
8888
},
89+
{
90+
"sent": {"unencrypted_password": "cisco", "salt": 0},
91+
"received": "00071A150754",
92+
},
8993
]
9094

9195
ENCRYPT_CISCO_TYPE9 = [

0 commit comments

Comments
 (0)