Skip to content

Commit d53e3e1

Browse files
committed
add sha256
1 parent afbc881 commit d53e3e1

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pyvalueobjects.abstract.nullablevalueobject import build_nullable
2+
from pyvalueobjects.security.sha256_hash import Sha256Hash
3+
4+
_nullable_cls = build_nullable(Sha256Hash)
5+
6+
7+
class NullableSha256Hash(_nullable_cls):
8+
9+
def __init__(self, value: str = None):
10+
super().__init__(value)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re
2+
3+
from pyvalueobjects.errors.ValueObjectError import ValueObjectError
4+
from pyvalueobjects.strings.non_empty_string import NonEmptyString
5+
6+
7+
class Sha256Hash(NonEmptyString):
8+
9+
__MATCHER = re.compile(r'^[a-fA-F0-9]{64}$')
10+
11+
def __init__(self, value: str):
12+
super().__init__(value)
13+
14+
def _validate(self, value: str):
15+
super()._validate(value)
16+
try:
17+
is_correct = Sha256Hash.__MATCHER.match(value)
18+
if not is_correct:
19+
raise ValueObjectError('Value must be a valid SHA256 hash (64 hexadecimal characters).')
20+
except ValueError:
21+
raise ValueObjectError('Value must be a valid SHA256 hash (64 hexadecimal characters).')

0 commit comments

Comments
 (0)