Skip to content

Commit e9802f1

Browse files
committed
add support for SHA-512-256 algorithm
1 parent 15c9da3 commit e9802f1

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

python_digest/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@
2323
_REQUIRED_DIGEST_CHALLENGE_PARTS = ['realm', 'nonce', 'stale', 'algorithm', 'opaque', 'qop']
2424
DigestChallenge = namedtuple('DigestChallenge', _REQUIRED_DIGEST_CHALLENGE_PARTS)
2525

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+
2631
def validate_uri(digest_uri, request_path):
2732
digest_url_components = urlparse(digest_uri)
2833
return unquote(digest_url_components[2]) == request_path
2934

3035
def get_hash_func(algorithm):
3136
if algorithm == 'SHA-256':
3237
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
3344
elif algorithm == 'SHA-512':
3445
return hashlib.sha512
3546
# default to 'MD5'
@@ -219,7 +230,7 @@ def parse_digest_response(digest_response_string):
219230
part_name: part for part_name, part in six.iteritems(parts)
220231
if part_name in _REQUIRED_DIGEST_RESPONSE_PARTS
221232
})
222-
if digest_response.algorithm not in ('MD5', 'SHA-256', 'SHA-512'):
233+
if digest_response.algorithm not in _AVAILABLE_HASH_FUNCS:
223234
return None
224235
if 'auth' != digest_response.qop:
225236
return None
@@ -269,7 +280,7 @@ def parse_digest_challenge(authentication_header):
269280
part_name: part for part_name, part in six.iteritems(parts)
270281
if part_name in _REQUIRED_DIGEST_CHALLENGE_PARTS
271282
})
272-
if digest_challenge.algorithm not in ('MD5', 'SHA-256', 'SHA-512'):
283+
if digest_challenge.algorithm not in _AVAILABLE_HASH_FUNCS:
273284
return None
274285
if 'auth' != digest_challenge.qop:
275286
return None

0 commit comments

Comments
 (0)