Skip to content

Commit

Permalink
Fix docstring and typing
Browse files Browse the repository at this point in the history
modified:   src/hibpcli/leaks.py
  • Loading branch information
jugmac00 committed Dec 24, 2020
1 parent 00b4987 commit 6dcd34b
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/hibpcli/leaks.py
@@ -1,5 +1,6 @@
import hashlib
import socket
from typing import Set

import httpx
from hibpcli.exceptions import ApiError
Expand All @@ -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:
Expand All @@ -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

0 comments on commit 6dcd34b

Please sign in to comment.