Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/hashes.py: 100%
17 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
1"""Hashes."""
2# -*- coding: utf-8 -*-
4# standard
5import re
7# local
8from .utils import validator
11@validator
12def md5(value: str, /):
13 """Return whether or not given value is a valid MD5 hash.
15 Examples:
16 >>> md5('d41d8cd98f00b204e9800998ecf8427e')
17 # Output: True
18 >>> md5('900zz11')
19 # Output: ValidationFailure(func=md5, args={'value': '900zz11'})
21 Args:
22 value:
23 MD5 string to validate.
25 Returns:
26 (Literal[True]):
27 If `value` is a valid MD5 hash.
28 (ValidationFailure):
29 If `value` is an invalid MD5 hash.
31 > *New in version 0.12.1*
32 """
33 return re.match(r"^[0-9a-f]{32}$", value, re.IGNORECASE) if value else False
36@validator
37def sha1(value: str, /):
38 """Return whether or not given value is a valid SHA1 hash.
40 Examples:
41 >>> sha1('da39a3ee5e6b4b0d3255bfef95601890afd80709')
42 # Output: True
43 >>> sha1('900zz11')
44 # Output: ValidationFailure(func=sha1, args={'value': '900zz11'})
46 Args:
47 value:
48 SHA1 string to validate.
50 Returns:
51 (Literal[True]):
52 If `value` is a valid SHA1 hash.
53 (ValidationFailure):
54 If `value` is an invalid SHA1 hash.
56 > *New in version 0.12.1*
57 """
58 return re.match(r"^[0-9a-f]{40}$", value, re.IGNORECASE) if value else False
61@validator
62def sha224(value: str, /):
63 """Return whether or not given value is a valid SHA224 hash.
65 Examples:
66 >>> sha224('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f')
67 # Output: True
68 >>> sha224('900zz11')
69 # Output: ValidationFailure(func=sha224, args={'value': '900zz11'})
71 Args:
72 value:
73 SHA224 string to validate.
75 Returns:
76 (Literal[True]):
77 If `value` is a valid SHA224 hash.
78 (ValidationFailure):
79 If `value` is an invalid SHA224 hash.
81 > *New in version 0.12.1*
82 """
83 return re.match(r"^[0-9a-f]{56}$", value, re.IGNORECASE) if value else False
86@validator
87def sha256(value: str, /):
88 """Return whether or not given value is a valid SHA256 hash.
90 Examples:
91 >>> sha256(
92 ... 'e3b0c44298fc1c149afbf4c8996fb924'
93 ... '27ae41e4649b934ca495991b7852b855'
94 ... )
95 # Output: True
96 >>> sha256('900zz11')
97 # Output: ValidationFailure(func=sha256, args={'value': '900zz11'})
99 Args:
100 value:
101 SHA256 string to validate.
103 Returns:
104 (Literal[True]):
105 If `value` is a valid SHA256 hash.
106 (ValidationFailure):
107 If `value` is an invalid SHA256 hash.
109 > *New in version 0.12.1*
110 """
111 return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE) if value else False
114@validator
115def sha512(value: str, /):
116 """Return whether or not given value is a valid SHA512 hash.
118 Examples:
119 >>> sha512(
120 ... 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce'
121 ... '9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af9'
122 ... '27da3e'
123 ... )
124 # Output: True
125 >>> sha512('900zz11')
126 # Output: ValidationFailure(func=sha512, args={'value': '900zz11'})
128 Args:
129 value:
130 SHA512 string to validate.
132 Returns:
133 (Literal[True]):
134 If `value` is a valid SHA512 hash.
135 (ValidationFailure):
136 If `value` is an invalid SHA512 hash.
138 > *New in version 0.12.1*
139 """
140 return re.match(r"^[0-9a-f]{128}$", value, re.IGNORECASE) if value else False