Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,46 @@ mac_address
.. autofunction:: mac_address


md5
-----------

.. module:: validators.hashes

.. autofunction:: md5


sha1
-----------

.. module:: validators.hashes

.. autofunction:: sha1


sha224
-----------

.. module:: validators.hashes

.. autofunction:: sha224


sha256
-----------

.. module:: validators.hashes

.. autofunction:: sha256


sha512
-----------

.. module:: validators.hashes

.. autofunction:: sha512


slug
----

Expand Down
22 changes: 22 additions & 0 deletions tests/test_md5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import pytest

import validators


@pytest.mark.parametrize('value', [
'd41d8cd98f00b204e9800998ecf8427e',
'D41D8CD98F00B204E9800998ECF8427E'
])
def test_returns_true_on_valid_md5(value):
assert validators.md5(value)


@pytest.mark.parametrize('value', [
'z41d8cd98f00b204e9800998ecf8427e',
'z8cd98f00b204e9800998ecf8427e',
'z4aaaa1d8cd98f00b204e9800998ecf8427e'
])
def test_returns_failed_validation_on_invalid_md5(value):
result = validators.md5(value)
assert isinstance(result, validators.ValidationFailure)
22 changes: 22 additions & 0 deletions tests/test_sha1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import pytest

import validators


@pytest.mark.parametrize('value', [
'da39a3ee5e6b4b0d3255bfef95601890afd80709',
'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
])
def test_returns_true_on_valid_sha1(value):
assert validators.sha1(value)


@pytest.mark.parametrize('value', [
'za39a3ee5e6b4b0d3255bfef95601890afd80709',
'da39e5e6b4b0d3255bfef95601890afd80709',
'daaaa39a3ee5e6b4b0d3255bfef95601890afd80709'
])
def test_returns_failed_validation_on_invalid_sha1(value):
result = validators.sha1(value)
assert isinstance(result, validators.ValidationFailure)
22 changes: 22 additions & 0 deletions tests/test_sha224.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import pytest

import validators


@pytest.mark.parametrize('value', [
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f',
'D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F'
])
def test_returns_true_on_valid_sha224(value):
assert validators.sha224(value)


@pytest.mark.parametrize('value', [
'z14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f',
'd028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f',
'daaa14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f'
])
def test_returns_failed_validation_on_invalid_sha224(value):
result = validators.sha224(value)
assert isinstance(result, validators.ValidationFailure)
22 changes: 22 additions & 0 deletions tests/test_sha256.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import pytest

import validators


@pytest.mark.parametrize('value', [
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855'
])
def test_returns_true_on_valid_sha256(value):
assert validators.sha256(value)


@pytest.mark.parametrize('value', [
'z3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'ec44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'eaaaa3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
])
def test_returns_failed_validation_on_invalid_sha256(value):
result = validators.sha256(value)
assert isinstance(result, validators.ValidationFailure)
37 changes: 37 additions & 0 deletions tests/test_sha512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
import pytest

import validators


@pytest.mark.parametrize('value', [
(
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d'
'13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
),
(
'CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D'
'13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E'
)
])
def test_returns_true_on_valid_sha512(value):
assert validators.sha512(value)


@pytest.mark.parametrize('value', [
(
'zf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d'
'13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
),
(
'cf8357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c'
'5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
),
(
'cf8aaaa3e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce4'
'7d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
)
])
def test_returns_failed_validation_on_invalid_sha512(value):
result = validators.sha512(value)
assert isinstance(result, validators.ValidationFailure)
1 change: 1 addition & 0 deletions validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .domain import domain # noqa
from .email import email # noqa
from .extremes import Max, Min # noqa
from .hashes import md5, sha1, sha224, sha256, sha512 # noqa
from .i18n import fi_business_id, fi_ssn # noqa
from .iban import iban # noqa
from .ip_address import ipv4, ipv6 # noqa
Expand Down
121 changes: 121 additions & 0 deletions validators/hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import re

from .utils import validator

md5_regex = re.compile(
r"^[0-9a-f]{32}$",
re.IGNORECASE
)
sha1_regex = re.compile(
r"^[0-9a-f]{40}$",
re.IGNORECASE
)
sha224_regex = re.compile(
r"^[0-9a-f]{56}$",
re.IGNORECASE
)
sha256_regex = re.compile(
r"^[0-9a-f]{64}$",
re.IGNORECASE
)
sha512_regex = re.compile(
r"^[0-9a-f]{128}$",
re.IGNORECASE
)


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

Examples::

>>> md5('d41d8cd98f00b204e9800998ecf8427e')
True

>>> md5('900zz11')
ValidationFailure(func=md5, args={'value': '900zz11'})

:param value: MD5 string to validate
"""
return md5_regex.match(value)


@validator
def sha1(value):
"""
Return whether or not given value is a valid SHA1 hash.

Examples::

>>> sha1('da39a3ee5e6b4b0d3255bfef95601890afd80709')
True

>>> sha1('900zz11')
ValidationFailure(func=sha1, args={'value': '900zz11'})

:param value: SHA1 string to validate
"""
return sha1_regex.match(value)


@validator
def sha224(value):
"""
Return whether or not given value is a valid SHA224 hash.

Examples::

>>> sha224('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f')
True

>>> sha224('900zz11')
ValidationFailure(func=sha224, args={'value': '900zz11'})

:param value: SHA224 string to validate
"""
return sha224_regex.match(value)


@validator
def sha256(value):
"""
Return whether or not given value is a valid SHA256 hash.

Examples::

>>> sha256(
... 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b'
... '855'
... )
True

>>> sha256('900zz11')
ValidationFailure(func=sha256, args={'value': '900zz11'})

:param value: SHA256 string to validate
"""
return sha256_regex.match(value)


@validator
def sha512(value):
"""
Return whether or not given value is a valid SHA512 hash.

Examples::

>>> sha512(
... 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce'
... '9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af9'
... '27da3e'
... )
True

>>> sha512('900zz11')
ValidationFailure(func=sha512, args={'value': '900zz11'})

:param value: SHA512 string to validate
"""
return sha512_regex.match(value)