Skip to content

Commit

Permalink
Add Argon2 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Feb 12, 2024
1 parent 6132d72 commit b6f1c88
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/reference/pwdlib.exceptions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Reference
# Reference - Exceptions

::: pwdlib.exceptions
options:
Expand Down
11 changes: 11 additions & 0 deletions docs/reference/pwdlib.hashers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Reference - Hashers

::: pwdlib.hashers.argon2
options:
show_root_heading: true
show_source: false

::: pwdlib.hashers.bcrypt
options:
show_root_heading: true
show_source: false
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ nav:
- Reference:
- pwdlib: reference/pwdlib.md
- pwdlib.exceptions: reference/pwdlib.exceptions.md
- pwdlib.hashers: reference/pwdlib.hashers.md
29 changes: 27 additions & 2 deletions pwdlib/hashers/argon2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,33 @@


class Argon2Hasher(HasherProtocol):
def __init__(self) -> None:
self._hasher = PasswordHasher() # TODO: handle parameters
def __init__(
self,
time_cost: int = argon2.DEFAULT_TIME_COST,
memory_cost: int = argon2.DEFAULT_MEMORY_COST,
parallelism: int = argon2.DEFAULT_PARALLELISM,
hash_len: int = argon2.DEFAULT_HASH_LENGTH,
salt_len: int = argon2.DEFAULT_RANDOM_SALT_LENGTH,
type: argon2.Type = argon2.Type.ID,
) -> None:
"""
Args:
time_cost: Defines the amount of computation realized and
therefore the execution time, given in number of iterations.
memory_cost: Defines the memory usage, given in kibibytes.
parallelism: Defines the number of parallel threads (*changes*
the resulting hash value).
hash_len: Length of the hash in bytes.
salt_len: Length of random salt to be generated for each
password.
type: Argon2 type to use. Only change for interoperability
with legacy systems.
"""
self._hasher = PasswordHasher(
time_cost, memory_cost, parallelism, hash_len, salt_len, "utf-8", type
)

@classmethod
def identify(cls, hash: typing.Union[str, bytes]) -> bool:
Expand Down
8 changes: 4 additions & 4 deletions pwdlib/hashers/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import typing


def ensure_str(v: typing.Union[str, bytes]) -> str:
return v.decode("utf-8") if isinstance(v, bytes) else typing.cast(str, v)
def ensure_str(v: typing.Union[str, bytes], *, encoding: str = "utf-8") -> str:
return v.decode(encoding) if isinstance(v, bytes) else typing.cast(str, v)


def ensure_bytes(v: typing.Union[str, bytes]) -> bytes:
return v.encode("utf-8") if isinstance(v, str) else v
def ensure_bytes(v: typing.Union[str, bytes], *, encoding: str = "utf-8") -> bytes:
return v.encode(encoding) if isinstance(v, str) else v


class HasherProtocol(typing.Protocol):
Expand Down
5 changes: 5 additions & 0 deletions pwdlib/hashers/bcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class BcryptHasher(HasherProtocol):
def __init__(
self, rounds: int = 12, prefix: typing.Literal["2a", "2b"] = "2b"
) -> None:
"""
Args:
rounds: The number of rounds to use for hashing.
prefix: The prefix to use for hashing.
"""
self.rounds = rounds
self.prefix = prefix.encode("utf-8")

Expand Down

0 comments on commit b6f1c88

Please sign in to comment.