From 8e98bf65081fdf27ca0d781753315fcff7e90544 Mon Sep 17 00:00:00 2001 From: Ben Allen Date: Thu, 9 May 2024 16:40:45 -0400 Subject: [PATCH] Fix double .encode() --- routeros_api/api.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/routeros_api/api.py b/routeros_api/api.py index 1cf1137..ba6bd30 100644 --- a/routeros_api/api.py +++ b/routeros_api/api.py @@ -25,9 +25,7 @@ def __init__(self, host, username='admin', password='', port=None, plaintext_log self.host = host self.username = username self.password = password - self.plaintext_login = plaintext_login - self.ssl_context = ssl_context # Use SSL? Ignored when using a context, so we will set it for simple reference when port-switching: if ssl_context is not None: @@ -83,12 +81,12 @@ def __init__(self, communicator): self.communicator = communicator def login(self, login, password, plaintext_login): + if isinstance(login, str): + login = login.encode() + if isinstance(password, str): + password = password.encode() response = None if plaintext_login: - if isinstance(login, str): - login = login.encode() - if isinstance(password, str): - password = password.encode() response = self.get_binary_resource('/').call('login', {'name': login, 'password': password}) else: response = self.get_binary_resource('/').call('login') @@ -96,11 +94,11 @@ def login(self, login, password, plaintext_login): token = binascii.unhexlify(response.done_message['ret']) hasher = hashlib.md5() hasher.update(b'\x00') - hasher.update(password.encode()) + hasher.update(password) hasher.update(token) hashed = b'00' + hasher.hexdigest().encode('ascii') self.get_binary_resource('/').call( - 'login', {'name': login.encode(), 'response': hashed}) + 'login', {'name': login, 'response': hashed}) def get_resource(self, path, structure=None): if structure is None: