Skip to content

Commit

Permalink
patch: moves base58 and base64 into encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yozachar committed Apr 3, 2024
1 parent 3b36ec7 commit 1aba4f7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
11 changes: 6 additions & 5 deletions src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from .cron import cron
from .domain import domain
from .email import email
from .hashes import base58, base64, md5, sha1, sha224, sha256, sha512
from .encoding import base58, base64
from .hashes import md5, sha1, sha224, sha256, sha512
from .hostname import hostname
from .i18n import es_cif, es_doi, es_nie, es_nif, fi_business_id, fi_ssn, fr_department, fr_ssn
from .iban import iban
Expand All @@ -25,9 +26,8 @@
__all__ = (
# ...
"between",
# crypto addresses
# crypto_addresses
"btc_address",
# "eth_address",
# cards
"amex",
"card_number",
Expand All @@ -45,9 +45,10 @@
"domain",
# ...
"email",
# hashes
# encodings
"base58",
"base64",
# hashes
"md5",
"sha1",
"sha224",
Expand All @@ -66,7 +67,7 @@
"fr_ssn",
# ...
"iban",
# ip addresses
# ip_addresses
"ipv4",
"ipv6",
# ...
Expand Down
53 changes: 53 additions & 0 deletions src/validators/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Encoding."""

# standard
import re

# local
from .utils import validator


@validator
def base58(value: str, /):
"""Return whether or not given value is a valid base58 encoding.
Examples:
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
# Output: True
>>> base58('cUSECm5YzcXJwP')
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})
Args:
value:
base58 string to validate.
Returns:
(Literal[True]): If `value` is a valid base58 encoding.
(ValidationError): If `value` is an invalid base58 encoding.
"""
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False


@validator
def base64(value: str, /):
"""Return whether or not given value is a valid base64 encoding.
Examples:
>>> base64('Y2hhcmFjdGVyIHNldA==')
# Output: True
>>> base64('cUSECm5YzcXJwP')
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})
Args:
value:
base64 string to validate.
Returns:
(Literal[True]): If `value` is a valid base64 encoding.
(ValidationError): If `value` is an invalid base64 encoding.
"""
return (
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
if value
else False
)
46 changes: 0 additions & 46 deletions src/validators/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,6 @@
from .utils import validator


@validator
def base58(value: str, /):
"""Return whether or not given value is a valid base58 hash.
Examples:
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
# Output: True
>>> base58('cUSECm5YzcXJwP')
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})
Args:
value:
base58 string to validate.
Returns:
(Literal[True]): If `value` is a valid base58 hash.
(ValidationError): If `value` is an invalid base58 hash.
"""
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False


@validator
def base64(value: str, /):
"""Return whether or not given value is a valid base64 hash.
Examples:
>>> base64('Y2hhcmFjdGVyIHNldA==')
# Output: True
>>> base64('cUSECm5YzcXJwP')
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})
Args:
value:
base64 string to validate.
Returns:
(Literal[True]): If `value` is a valid base64 hash.
(ValidationError): If `value` is an invalid base64 hash.
"""
return (
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
if value
else False
)


@validator
def md5(value: str, /):
"""Return whether or not given value is a valid MD5 hash.
Expand Down

0 comments on commit 1aba4f7

Please sign in to comment.