Skip to content

Commit 876b939

Browse files
rodrigo.nogueiralk-geimfari
authored andcommitted
feat: add SHA-3 family hash algorithms
Add SHA-3 hash algorithms to the Algorithm enum: - SHA3-224, SHA3-256, SHA3-384, SHA3-512 - SHAKE128, SHAKE256 (variable-length XOF) Updated hash() method to handle SHAKE algorithms that require a length parameter for hexdigest(). All algorithms are available in Python 3.6+ hashlib, compatible with mimesis's Python >=3.10 requirement.
1 parent b7d77dd commit 876b939

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Version 19.1.0
22
--------------
33

4-
- Added support for E.164 phone number formatting to the ``Person`` providers ``phone_number()`` method.
4+
- Added support for E.164 phone number formatting to the ``Person`` provider's ``phone_number()`` method.
55
- Added ``secondary_address()`` method to the ``Address`` provider.
6+
- Add SHA-3 family hash algorithms (``SHA3_224``, ``SHA3_256``, ``SHA3_384``, ``SHA3_512``, ``SHAKE128``, ``SHAKE256``) to the ``Algorithm`` enum.
67

78
Version 19.0.0
89
--------------
@@ -15,6 +16,8 @@ Version 19.0.0
1516
- Add ``SchemaBuilder`` for generating relational data.
1617
- Add ``ip_v4_cidr()``, ``ip_v6_cidr()`` and ``cloud_region()`` methods for the ``Internet`` provider.
1718

19+
20+
1821
Version 18.0.0
1922
--------------
2023

mimesis/enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ class Algorithm(Enum):
178178
SHA512 = "sha512"
179179
BLAKE2B = "blake2b"
180180
BLAKE2S = "blake2s"
181+
SHA3_224 = "sha3_224"
182+
SHA3_256 = "sha3_256"
183+
SHA3_384 = "sha3_384"
184+
SHA3_512 = "sha3_512"
185+
SHAKE128 = "shake_128"
186+
SHAKE256 = "shake_256"
181187

182188

183189
class TLDType(Enum):

mimesis/providers/cryptographic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def hash(self, algorithm: Algorithm | None = None) -> str: # noqa: A003
4848
key = self.validate_enum(algorithm, Algorithm)
4949
func = getattr(hashlib, key)
5050
value = func(self.uuid().encode())
51+
if key in ("shake_128", "shake_256"):
52+
return str(value.hexdigest(32))
5153
return str(value.hexdigest())
5254

5355
def token_bytes(self, entropy: int = 32) -> bytes:

tests/test_providers/test_universal/test_cryptographic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def test_uuid(self, crypto):
3939
(Algorithm.SHA512, 128),
4040
(Algorithm.BLAKE2S, 64),
4141
(Algorithm.BLAKE2B, 128),
42+
(Algorithm.SHA3_224, 56),
43+
(Algorithm.SHA3_256, 64),
44+
(Algorithm.SHA3_384, 96),
45+
(Algorithm.SHA3_512, 128),
46+
(Algorithm.SHAKE128, 64),
47+
(Algorithm.SHAKE256, 64),
4248
],
4349
)
4450
def test_hash(self, crypto, algorithm, length):

0 commit comments

Comments
 (0)