netutils icon indicating copy to clipboard operation
netutils copied to clipboard

`encrypt_cisco_type7` drops the salt argument when salt value is `0`

Open mathieu-mp opened this issue 2 months ago • 1 comments

Environment

  • Python version: 3.9.22
  • netutils version: 1.15.1

Expected Behavior

encrypt_cisco_type7(unencrypted_password: str, salt: t.Optional[int] = None) shall make use of the salt when their value is 0.

Observed Behavior

encrypt_cisco_type7(unencrypted_password: str, salt: t.Optional[int] = None) drops the salt when their value is 0, and randomizes a new salt between 0 and 15.

Steps to Reproduce

>>> encrypt_cisco_type7("cisco", 0)
'121A0C041104'
>>> encrypt_cisco_type7("cisco", 0)
'13061E010803'
>>> encrypt_cisco_type7("cisco", 0)
'05080F1C2243'
>>> encrypt_cisco_type7("cisco", 0)
'1511021F0725'
>>> encrypt_cisco_type7("cisco", 0)
'00071A150754'

There is a 1 on 16 chance that the salt ends up being 0.

Root cause

In netutils.password.encrypt_cisco_type7(...), the code tests for a falsy salt value and not strictly for a None value.

    if not salt:
        salt = random.randint(0, 15)  # noqa: S311

Should be:

    if salt is None:
        salt = random.randint(0, 15)  # noqa: S311

mathieu-mp avatar Nov 18 '25 16:11 mathieu-mp

Makes sense, interested in putting in a PR, potentially with a test.

itdependsnetworks avatar Nov 18 '25 17:11 itdependsnetworks