Skip to content

Commit

Permalink
Make check_needs_rehash accept bytes, too (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek authored Apr 16, 2024
1 parent abd0cf9 commit d362c8d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ What explicitly *may* change over time are the default hashing parameters and th

## [Unreleased](https://github.com/hynek/argon2-cffi/compare/23.1.0...HEAD)

### Changed

- `argon2.PasswordHasher.check_needs_rehash()` now also accepts bytes like the rest of the API.
[#174](https://github.com/hynek/argon2-cffi/pull/174)


## [23.1.0](https://github.com/hynek/argon2-cffi/compare/21.3.0...23.1.0) - 2023-08-15

### Removed
Expand Down
6 changes: 5 additions & 1 deletion src/argon2/_password_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def verify(
hash, _ensure_bytes(password, self.encoding), hash_type
)

def check_needs_rehash(self, hash: str) -> bool:
def check_needs_rehash(self, hash: str | bytes) -> bool:
"""
Check whether *hash* was created using the instance's parameters.
Expand All @@ -264,5 +264,9 @@ def check_needs_rehash(self, hash: str) -> bool:
Whether *hash* was created using the instance's parameters.
.. versionadded:: 18.2.0
.. versionchanged:: 24.1.0 Accepts bytes for *hash*.
"""
if isinstance(hash, bytes):
hash = hash.decode("ascii")

return self._parameters != extract_parameters(hash)
18 changes: 14 additions & 4 deletions tests/test_password_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,32 @@ def test_verify_invalid_hash(self):
with pytest.raises(InvalidHash):
PasswordHasher().verify("tiger", "does not matter")

def test_check_needs_rehash_no(self):
@pytest.mark.parametrize("use_bytes", [True, False])
def test_check_needs_rehash_no(self, use_bytes):
"""
Return False if the hash has the correct parameters.
"""
ph = PasswordHasher(1, 8, 1, 16, 16)

assert not ph.check_needs_rehash(ph.hash("foo"))
hash = ph.hash("foo")
if use_bytes:
hash = hash.encode()

def test_check_needs_rehash_yes(self):
assert not ph.check_needs_rehash(hash)

@pytest.mark.parametrize("use_bytes", [True, False])
def test_check_needs_rehash_yes(self, use_bytes):
"""
Return True if any of the parameters changes.
"""
ph = PasswordHasher(1, 8, 1, 16, 16)
ph_old = PasswordHasher(1, 8, 1, 8, 8)

assert ph.check_needs_rehash(ph_old.hash("foo"))
hash = ph_old.hash("foo")
if use_bytes:
hash = hash.encode()

assert ph.check_needs_rehash(hash)

def test_type_is_configurable(self):
"""
Expand Down

0 comments on commit d362c8d

Please sign in to comment.