mv JWK-canonicalization util.security.fingerprint
This commit is contained in:
parent
4020ab9f55
commit
6577ac3e62
3 changed files with 37 additions and 18 deletions
35
util/security/fingerprint.py
Normal file
35
util/security/fingerprint.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import collections
|
||||
import json
|
||||
|
||||
from hashlib import sha256
|
||||
|
||||
|
||||
def canonicalize(json_obj):
|
||||
"""This function canonicalizes a Python object that will be serialized as JSON.
|
||||
|
||||
Args:
|
||||
json_obj (object): the Python object that will later be serialized as JSON.
|
||||
|
||||
Returns:
|
||||
object: json_obj now sorted to its canonical form.
|
||||
|
||||
"""
|
||||
if isinstance(json_obj, collections.MutableMapping):
|
||||
sorted_obj = sorted({key: canonicalize(val) for key, val in json_obj.items()}.items())
|
||||
return collections.OrderedDict(sorted_obj)
|
||||
elif isinstance(json_obj, (list, tuple)):
|
||||
return [canonicalize(val) for val in json_obj]
|
||||
return json_obj
|
||||
|
||||
|
||||
def canonical_kid(jwk):
|
||||
"""This function returns the SHA256 hash of a canonical JWK.
|
||||
|
||||
Args:
|
||||
jwk (object): the JWK for which a kid will be generated.
|
||||
|
||||
Returns:
|
||||
string: the unique kid for the given JWK.
|
||||
|
||||
"""
|
||||
return sha256(json.dumps(canonicalize(jwk), separators=(',', ':'))).hexdigest()
|
Reference in a new issue