From 6dcd34b9e5cd91d44a236e91133cdf4646bb70b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Thu, 24 Dec 2020 15:37:56 +0100 Subject: [PATCH] Fix docstring and typing modified: src/hibpcli/leaks.py --- src/hibpcli/leaks.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/hibpcli/leaks.py b/src/hibpcli/leaks.py index 7afa6c5..92fccc7 100644 --- a/src/hibpcli/leaks.py +++ b/src/hibpcli/leaks.py @@ -1,5 +1,6 @@ import hashlib import socket +from typing import Set import httpx from hibpcli.exceptions import ApiError @@ -16,8 +17,20 @@ def __contains__(self, password: str) -> bool: self._pages[hash_head] = self._load_page(hash_head) return hash_tail in self._pages[hash_head] - def _load_page(self, hash_head: str) -> dict: - """Load a list of leaked passwords for a given hash_head and return a set.""" + @staticmethod + def _generate_hash(password: str) -> str: + # using sha1 here is no security issue + # the API uses it, so there is no other way to access the data + hash_object = hashlib.sha1(bytes(password, "UTF-8")) # nosec + hex_digest = hash_object.hexdigest().upper() + return hex_digest + + @staticmethod + def _load_page(hash_head: str) -> Set: + """Load a list of hash/occurrence pairs for a given hash_head. + + Returns a set of hash_tails only. + """ try: result = httpx.get(f"https://api.pwnedpasswords.com/range/{hash_head}").text except socket.gaierror: @@ -28,10 +41,3 @@ def _load_page(self, hash_head: str) -> dict: # leak, separated by a colon # cut off string - information after the hash is of no interest return {line[:35] for line in result.splitlines()} - - def _generate_hash(self, password: str) -> str: - # using sha1 here is no security issue - # the API uses it, so there is no other way to access the data - hash_object = hashlib.sha1(bytes(password, "UTF-8")) # nosec - hex_digest = hash_object.hexdigest().upper() - return hex_digest