1"""Hashes."""
2
3# standard
4import re
5
6# local
7from .utils import validator
8
9
10@validator
11def md5(value: str, /):
12 """Return whether or not given value is a valid MD5 hash.
13
14 Examples:
15 >>> md5('d41d8cd98f00b204e9800998ecf8427e')
16 True
17 >>> md5('900zz11')
18 ValidationError(func=md5, args={'value': '900zz11'})
19
20 Args:
21 value:
22 MD5 string to validate.
23
24 Returns:
25 (Literal[True]): If `value` is a valid MD5 hash.
26 (ValidationError): If `value` is an invalid MD5 hash.
27 """
28 return re.match(r"^[0-9a-f]{32}$", value, re.IGNORECASE) if value else False
29
30
31@validator
32def sha1(value: str, /):
33 """Return whether or not given value is a valid SHA1 hash.
34
35 Examples:
36 >>> sha1('da39a3ee5e6b4b0d3255bfef95601890afd80709')
37 True
38 >>> sha1('900zz11')
39 ValidationError(func=sha1, args={'value': '900zz11'})
40
41 Args:
42 value:
43 SHA1 string to validate.
44
45 Returns:
46 (Literal[True]): If `value` is a valid SHA1 hash.
47 (ValidationError): If `value` is an invalid SHA1 hash.
48 """
49 return re.match(r"^[0-9a-f]{40}$", value, re.IGNORECASE) if value else False
50
51
52@validator
53def sha224(value: str, /):
54 """Return whether or not given value is a valid SHA224 hash.
55
56 Examples:
57 >>> sha224('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f')
58 True
59 >>> sha224('900zz11')
60 ValidationError(func=sha224, args={'value': '900zz11'})
61
62 Args:
63 value:
64 SHA224 string to validate.
65
66 Returns:
67 (Literal[True]): If `value` is a valid SHA224 hash.
68 (ValidationError): If `value` is an invalid SHA224 hash.
69 """
70 return re.match(r"^[0-9a-f]{56}$", value, re.IGNORECASE) if value else False
71
72
73@validator
74def sha256(value: str, /):
75 """Return whether or not given value is a valid SHA256 hash.
76
77 Examples:
78 >>> sha256(
79 ... 'e3b0c44298fc1c149afbf4c8996fb924'
80 ... '27ae41e4649b934ca495991b7852b855'
81 ... )
82 True
83 >>> sha256('900zz11')
84 ValidationError(func=sha256, args={'value': '900zz11'})
85
86 Args:
87 value:
88 SHA256 string to validate.
89
90 Returns:
91 (Literal[True]): If `value` is a valid SHA256 hash.
92 (ValidationError): If `value` is an invalid SHA256 hash.
93 """
94 return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE) if value else False
95
96
97@validator
98def sha384(value: str, /):
99 """Return whether or not given value is a valid SHA384 hash.
100
101 Examples:
102 >>> sha384(
103 ... 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163'
104 ... '1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7'
105 ... )
106 True
107 >>> sha384('900zz11')
108 ValidationError(func=sha384, args={'value': '900zz11'})
109
110 Args:
111 value:
112 SHA384 string to validate.
113
114 Returns:
115 (Literal[True]): If `value` is a valid SHA384 hash.
116 (ValidationError): If `value` is an invalid SHA384 hash.
117 """
118 return re.match(r"^[0-9a-f]{96}$", value, re.IGNORECASE) if value else False
119
120
121@validator
122def sha512(value: str, /):
123 """Return whether or not given value is a valid SHA512 hash.
124
125 Examples:
126 >>> sha512(
127 ... 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce'
128 ... '9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af9'
129 ... '27da3e'
130 ... )
131 True
132 >>> sha512('900zz11')
133 ValidationError(func=sha512, args={'value': '900zz11'})
134
135 Args:
136 value:
137 SHA512 string to validate.
138
139 Returns:
140 (Literal[True]): If `value` is a valid SHA512 hash.
141 (ValidationError): If `value` is an invalid SHA512 hash.
142 """
143 return re.match(r"^[0-9a-f]{128}$", value, re.IGNORECASE) if value else False