Skip to content

Commit

Permalink
Feat add IsHash (#44)
Browse files Browse the repository at this point in the history
* Feat add IsHash

* fix IsHash logic and add hashlib tests

* Fix use HashTypes variable and regex tweaks

* allow bytes, fix literal check

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
osintalex and samuelcolvin committed Sep 15, 2022
1 parent 5cf23ce commit 42c5594
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dirty_equals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
IsPositiveFloat,
IsPositiveInt,
)
from ._other import FunctionCheck, IsIP, IsJson, IsUUID
from ._other import FunctionCheck, IsHash, IsIP, IsJson, IsUUID
from ._sequence import Contains, HasLen, IsList, IsListOrTuple, IsTuple
from ._strings import IsAnyStr, IsBytes, IsStr
from .version import VERSION
Expand Down Expand Up @@ -70,6 +70,7 @@
'FunctionCheck',
'IsJson',
'IsUUID',
'IsHash',
'IsIP',
# strings
'IsStr',
Expand Down
48 changes: 48 additions & 0 deletions dirty_equals/_other.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network, ip_network
from typing import Any, Callable, Optional, TypeVar, Union, overload
from uuid import UUID
Expand Down Expand Up @@ -148,6 +149,53 @@ def equals(self, other: Any) -> bool:
return self.func(other)


HashTypes = Literal['md5', 'sha-1', 'sha-256']


class IsHash(DirtyEquals[str]):
"""
A class that checks if a value is a valid common hash type, using a simple length and allowed characters regex.
"""

def __init__(self, hash_type: HashTypes):
"""
Args:
hash_type: The hash type to check. Must be specified.
```py title="IsHash"
from dirty_equals import IsHash
assert 'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert b'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert 'f1e069787ece74531d112559945c6871' != IsHash('sha-256')
assert 'F1E069787ECE74531D112559945C6871' == IsHash('md5')
assert '40bd001563085fc35165329ea1ff5c5ecbdbbeef' == IsHash('sha-1')
assert 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3' == IsHash('sha-256')
```
"""

allowed_hashes = HashTypes.__args__ # type: ignore[attr-defined]
if hash_type not in allowed_hashes:
raise ValueError(f"Hash type must be one of the following values: {', '.join(allowed_hashes)}")

self.hash_type = hash_type
super().__init__(hash_type)

def equals(self, other: Any) -> bool:
if isinstance(other, str):
s = other
elif isinstance(other, (bytes, bytearray)):
s = other.decode()
else:
return False
hash_type_regex_patterns = {
'md5': r'[a-fA-F\d]{32}',
'sha-1': r'[a-fA-F\d]{40}',
'sha-256': r'[a-fA-F\d]{64}',
}
return bool(re.fullmatch(hash_type_regex_patterns[self.hash_type], s))


IP = TypeVar('IP', IPv4Address, IPv4Network, IPv6Address, IPv6Network, Union[str, int, bytes])


Expand Down
2 changes: 2 additions & 0 deletions docs/types/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

::: dirty_equals.IsOneOf

::: dirty_equals.IsHash

::: dirty_equals.IsIP
56 changes: 55 additions & 1 deletion tests/test_other.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import uuid
from hashlib import md5, sha1, sha256
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network

import pytest

from dirty_equals import FunctionCheck, IsIP, IsJson, IsUUID
from dirty_equals import FunctionCheck, IsHash, IsIP, IsJson, IsUUID


@pytest.mark.parametrize(
Expand Down Expand Up @@ -183,3 +184,56 @@ def test_not_ip_repr():
def test_ip_bad_netmask():
with pytest.raises(TypeError, match='To check the netmask you must specify the IP version'):
IsIP(netmask='255.255.255.0')


@pytest.mark.parametrize(
'other,dirty',
[
('f1e069787ECE74531d112559945c6871', IsHash('md5')),
('40bd001563085fc35165329ea1FF5c5ecbdbbeef', IsHash('sha-1')),
('a665a45920422f9d417e4867eFDC4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', IsHash('sha-256')),
(b'f1e069787ECE74531d112559945c6871', IsHash('md5')),
(bytearray(b'f1e069787ECE74531d112559945c6871'), IsHash('md5')),
],
)
def test_is_hash_true(other, dirty):
assert other == dirty


@pytest.mark.parametrize(
'other,dirty',
[
('foobar', IsHash('md5')),
(b'\x81 UnicodeDecodeError', IsHash('md5')),
([1, 2, 3], IsHash('sha-1')),
('f1e069787ECE74531d112559945c6871d', IsHash('md5')),
('400bd001563085fc35165329ea1FF5c5ecbdbbeef', IsHash('sha-1')),
('a665a45920422g9d417e4867eFDC4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', IsHash('sha-256')),
],
)
def test_is_hash_false(other, dirty):
assert other != dirty


@pytest.mark.parametrize(
'hash_type',
['md5', 'sha-1', 'sha-256'],
)
def test_is_hash_md5_false_repr(hash_type):
is_hash = IsHash(hash_type)
with pytest.raises(AssertionError):
assert '123' == is_hash
assert str(is_hash) == f"IsHash('{hash_type}')"


@pytest.mark.parametrize(
'hash_func, hash_type',
[(md5, 'md5'), (sha1, 'sha-1'), (sha256, 'sha-256')],
)
def test_hashlib_hashes(hash_func, hash_type):
assert hash_func(b'dirty equals').hexdigest() == IsHash(hash_type)


def test_wrong_hash_type():
with pytest.raises(ValueError, match='Hash type must be one of the following values: md5, sha-1, sha-256'):
assert '123' == IsHash('ntlm')

0 comments on commit 42c5594

Please sign in to comment.