From 7d4834ac4dd708b87187caff8eb59e783e8c2111 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Tue, 28 Dec 2021 10:14:38 +0100 Subject: [mod] webutils.py: remove dead code secret_key can't be bytes (see settings_default.py) --- searx/webutils.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'searx/webutils.py') diff --git a/searx/webutils.py b/searx/webutils.py index 737e5a82f..11a101806 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -77,14 +77,7 @@ def get_result_templates(templates_path): def new_hmac(secret_key, url): - try: - secret_key_bytes = bytes(secret_key, 'utf-8') - except TypeError as err: - if isinstance(secret_key, bytes): - secret_key_bytes = secret_key - else: - raise err - return hmac.new(secret_key_bytes, url, hashlib.sha256).hexdigest() + return hmac.new(secret_key.encode(), url, hashlib.sha256).hexdigest() def prettify_url(url, max_length=74): -- cgit v1.2.3 From 8f3a7feb47a84344a190ce83e629afde1181f6ae Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Tue, 28 Dec 2021 13:44:28 +0100 Subject: [mod] implement is_hmac_of() in webutils / close to new_hmac() Signed-off-by: Markus Heiser , Alexandre Flament --- searx/webutils.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'searx/webutils.py') diff --git a/searx/webutils.py b/searx/webutils.py index 11a101806..068582858 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -80,6 +80,11 @@ def new_hmac(secret_key, url): return hmac.new(secret_key.encode(), url, hashlib.sha256).hexdigest() +def is_hmac_of(secret_key, value, hmac_to_check): + hmac_of_value = new_hmac(secret_key, value) + return len(hmac_of_value) == len(hmac_to_check) and hmac.compare_digest(hmac_of_value, hmac_to_check) + + def prettify_url(url, max_length=74): if len(url) > max_length: chunk_len = int(max_length / 2 + 1) -- cgit v1.2.3