|
23 | 23 | _REQUIRED_DIGEST_CHALLENGE_PARTS = ['realm', 'nonce', 'stale', 'algorithm', 'opaque', 'qop'] |
24 | 24 | DigestChallenge = namedtuple('DigestChallenge', _REQUIRED_DIGEST_CHALLENGE_PARTS) |
25 | 25 |
|
| 26 | +_AVAILABLE_HASH_FUNCS = ['MD5', 'SHA-256', 'SHA-512'] |
| 27 | +if 'sha512_256' in hashlib.algorithms_available: |
| 28 | + _AVAILABLE_HASH_FUNCS.append('SHA-512-256') |
| 29 | +print(_AVAILABLE_HASH_FUNCS) |
| 30 | + |
26 | 31 | def validate_uri(digest_uri, request_path): |
27 | 32 | digest_url_components = urlparse(digest_uri) |
28 | 33 | return unquote(digest_url_components[2]) == request_path |
29 | 34 |
|
30 | 35 | def get_hash_func(algorithm): |
31 | 36 | if algorithm == 'SHA-256': |
32 | 37 | return hashlib.sha256 |
| 38 | + elif algorithm == 'SHA-512-256': |
| 39 | + def hash_wrapper(data): |
| 40 | + hash_obj = hashlib.new('sha512_256') |
| 41 | + hash_obj.update(data) |
| 42 | + return hash_obj |
| 43 | + return hash_wrapper |
33 | 44 | elif algorithm == 'SHA-512': |
34 | 45 | return hashlib.sha512 |
35 | 46 | # default to 'MD5' |
@@ -219,7 +230,7 @@ def parse_digest_response(digest_response_string): |
219 | 230 | part_name: part for part_name, part in six.iteritems(parts) |
220 | 231 | if part_name in _REQUIRED_DIGEST_RESPONSE_PARTS |
221 | 232 | }) |
222 | | - if digest_response.algorithm not in ('MD5', 'SHA-256', 'SHA-512'): |
| 233 | + if digest_response.algorithm not in _AVAILABLE_HASH_FUNCS: |
223 | 234 | return None |
224 | 235 | if 'auth' != digest_response.qop: |
225 | 236 | return None |
@@ -269,7 +280,7 @@ def parse_digest_challenge(authentication_header): |
269 | 280 | part_name: part for part_name, part in six.iteritems(parts) |
270 | 281 | if part_name in _REQUIRED_DIGEST_CHALLENGE_PARTS |
271 | 282 | }) |
272 | | - if digest_challenge.algorithm not in ('MD5', 'SHA-256', 'SHA-512'): |
| 283 | + if digest_challenge.algorithm not in _AVAILABLE_HASH_FUNCS: |
273 | 284 | return None |
274 | 285 | if 'auth' != digest_challenge.qop: |
275 | 286 | return None |
|
0 commit comments