Skip to content

Commit

Permalink
add a new validator for sha384 hash (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
msamsami committed Jul 10, 2024
1 parent bae643f commit cd01b4b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
18 changes: 18 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ Note to self: Breaking changes must increment either
-->

## 0.32.0 (2024-07-10)

_**Breaking**_

> No breaking changes were introduced in this version.
_**Features**_

- feat: add validator for `sha384` hash by @msamsami in [#387](https://github.com/python-validators/validators/pull/387)

_**Maintenance**_

- maint: bump version by @msamsami in [#387](https://github.com/python-validators/validators/pull/387)

**Full Changelog**: [`0.31.0...0.32.0`](https://github.com/python-validators/validators/compare/0.31.0...0.32.0)

---

## 0.31.0 (2024-07-08)

_**Breaking**_
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

| Version | Supported |
| ---------- | ------------------ |
| `>=0.31.0` | :white_check_mark: |
| `>=0.32.0` | :white_check_mark: |

## Reporting a Vulnerability

Expand Down
1 change: 1 addition & 0 deletions docs/api/hashes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
::: validators.hashes.sha1
::: validators.hashes.sha224
::: validators.hashes.sha256
::: validators.hashes.sha384
::: validators.hashes.sha512
1 change: 1 addition & 0 deletions docs/api/hashes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ hashes
.. autofunction:: sha1
.. autofunction:: sha224
.. autofunction:: sha256
.. autofunction:: sha384
.. autofunction:: sha512
5 changes: 3 additions & 2 deletions src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .email import email
from .encoding import base16, base32, base58, base64
from .finance import cusip, isin, sedol
from .hashes import md5, sha1, sha224, sha256, sha512
from .hashes import md5, sha1, sha224, sha256, sha384, sha512
from .hostname import hostname
from .i18n import (
es_cif,
Expand Down Expand Up @@ -73,6 +73,7 @@
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
# ...
"hostname",
Expand Down Expand Up @@ -107,4 +108,4 @@
"validator",
)

__version__ = "0.31.0"
__version__ = "0.32.0"
24 changes: 24 additions & 0 deletions src/validators/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ def sha256(value: str, /):
return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE) if value else False


@validator
def sha384(value: str, /):
"""Return whether or not given value is a valid SHA384 hash.
Examples:
>>> sha384(
... 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163'
... '1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7'
... )
# Output: True
>>> sha384('900zz11')
# Output: ValidationError(func=sha384, args={'value': '900zz11'})
Args:
value:
SHA384 string to validate.
Returns:
(Literal[True]): If `value` is a valid SHA384 hash.
(ValidationError): If `value` is an invalid SHA384 hash.
"""
return re.match(r"^[0-9a-f]{96}$", value, re.IGNORECASE) if value else False


@validator
def sha512(value: str, /):
"""Return whether or not given value is a valid SHA512 hash.
Expand Down
34 changes: 32 additions & 2 deletions tests/test_hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

# local
from validators import ValidationError, base58, base64, md5, sha1, sha224, sha256, sha512
from validators import ValidationError, base58, base64, md5, sha1, sha224, sha256, sha384, sha512

# ==> base58 <== #

Expand Down Expand Up @@ -158,7 +158,37 @@ def test_returns_failed_validation_on_invalid_sha256(value: str):
assert isinstance(sha256(value), ValidationError)


# ==> sha256 <== #
# ==> sha384 <== #


@pytest.mark.parametrize(
"value",
[
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7",
"bfd76c0ebbd006fee583410547c1887b0292be76d582d96c242d2a792723e3fd6fd061f9d5cfd13b8f961358e6adba4a",
"F21EF1F8DBF806106813C8504AF864D8D9BFDFA8D67FA9B7DFF1C5B61C2584394A05897C4F157CEEE0E8FBC29205BB8B",
],
)
def test_returns_true_on_valid_sha384(value: str):
"""Test returns true on valid sha384."""
assert sha384(value)


@pytest.mark.parametrize(
"value",
[
"zb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
"c753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
"cb00aaaa753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
],
)
def test_returns_failed_validation_on_invalid_sha384(value: str):
"""Test returns failed validation on invalid sha384."""
assert isinstance(sha384(value), ValidationError)


# ==> sha512 <== #


@pytest.mark.parametrize(
Expand Down

0 comments on commit cd01b4b

Please sign in to comment.