Skip to content

Commit cd01b4b

Browse files
authored
add a new validator for sha384 hash (#387)
1 parent bae643f commit cd01b4b

File tree

7 files changed

+80
-5
lines changed

7 files changed

+80
-5
lines changed

CHANGES.md

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ Note to self: Breaking changes must increment either
99
1010
-->
1111

12+
## 0.32.0 (2024-07-10)
13+
14+
_**Breaking**_
15+
16+
> No breaking changes were introduced in this version.
17+
18+
_**Features**_
19+
20+
- feat: add validator for `sha384` hash by @msamsami in [#387](https://github.com/python-validators/validators/pull/387)
21+
22+
_**Maintenance**_
23+
24+
- maint: bump version by @msamsami in [#387](https://github.com/python-validators/validators/pull/387)
25+
26+
**Full Changelog**: [`0.31.0...0.32.0`](https://github.com/python-validators/validators/compare/0.31.0...0.32.0)
27+
28+
---
29+
1230
## 0.31.0 (2024-07-08)
1331

1432
_**Breaking**_

SECURITY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
| Version | Supported |
66
| ---------- | ------------------ |
7-
| `>=0.31.0` | :white_check_mark: |
7+
| `>=0.32.0` | :white_check_mark: |
88

99
## Reporting a Vulnerability
1010

docs/api/hashes.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
::: validators.hashes.sha1
55
::: validators.hashes.sha224
66
::: validators.hashes.sha256
7+
::: validators.hashes.sha384
78
::: validators.hashes.sha512

docs/api/hashes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ hashes
66
.. autofunction:: sha1
77
.. autofunction:: sha224
88
.. autofunction:: sha256
9+
.. autofunction:: sha384
910
.. autofunction:: sha512

src/validators/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .email import email
1111
from .encoding import base16, base32, base58, base64
1212
from .finance import cusip, isin, sedol
13-
from .hashes import md5, sha1, sha224, sha256, sha512
13+
from .hashes import md5, sha1, sha224, sha256, sha384, sha512
1414
from .hostname import hostname
1515
from .i18n import (
1616
es_cif,
@@ -73,6 +73,7 @@
7373
"sha1",
7474
"sha224",
7575
"sha256",
76+
"sha384",
7677
"sha512",
7778
# ...
7879
"hostname",
@@ -107,4 +108,4 @@
107108
"validator",
108109
)
109110

110-
__version__ = "0.31.0"
111+
__version__ = "0.32.0"

src/validators/hashes.py

+24
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ def sha256(value: str, /):
9494
return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE) if value else False
9595

9696

97+
@validator
98+
def sha384(value: str, /):
99+
"""Return whether or not given value is a valid SHA384 hash.
100+
101+
Examples:
102+
>>> sha384(
103+
... 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163'
104+
... '1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7'
105+
... )
106+
# Output: True
107+
>>> sha384('900zz11')
108+
# Output: ValidationError(func=sha384, args={'value': '900zz11'})
109+
110+
Args:
111+
value:
112+
SHA384 string to validate.
113+
114+
Returns:
115+
(Literal[True]): If `value` is a valid SHA384 hash.
116+
(ValidationError): If `value` is an invalid SHA384 hash.
117+
"""
118+
return re.match(r"^[0-9a-f]{96}$", value, re.IGNORECASE) if value else False
119+
120+
97121
@validator
98122
def sha512(value: str, /):
99123
"""Return whether or not given value is a valid SHA512 hash.

tests/test_hashes.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
# local
7-
from validators import ValidationError, base58, base64, md5, sha1, sha224, sha256, sha512
7+
from validators import ValidationError, base58, base64, md5, sha1, sha224, sha256, sha384, sha512
88

99
# ==> base58 <== #
1010

@@ -158,7 +158,37 @@ def test_returns_failed_validation_on_invalid_sha256(value: str):
158158
assert isinstance(sha256(value), ValidationError)
159159

160160

161-
# ==> sha256 <== #
161+
# ==> sha384 <== #
162+
163+
164+
@pytest.mark.parametrize(
165+
"value",
166+
[
167+
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
168+
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7",
169+
"bfd76c0ebbd006fee583410547c1887b0292be76d582d96c242d2a792723e3fd6fd061f9d5cfd13b8f961358e6adba4a",
170+
"F21EF1F8DBF806106813C8504AF864D8D9BFDFA8D67FA9B7DFF1C5B61C2584394A05897C4F157CEEE0E8FBC29205BB8B",
171+
],
172+
)
173+
def test_returns_true_on_valid_sha384(value: str):
174+
"""Test returns true on valid sha384."""
175+
assert sha384(value)
176+
177+
178+
@pytest.mark.parametrize(
179+
"value",
180+
[
181+
"zb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
182+
"c753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
183+
"cb00aaaa753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
184+
],
185+
)
186+
def test_returns_failed_validation_on_invalid_sha384(value: str):
187+
"""Test returns failed validation on invalid sha384."""
188+
assert isinstance(sha384(value), ValidationError)
189+
190+
191+
# ==> sha512 <== #
162192

163193

164194
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)