PythiloLesson 6 of 8 · progress kept in this browser

Hash a stable representation of data

Two dictionaries can describe the same JSON object even if their insertion orders differ. Hashing a casual string representation would treat them as different. Canonical encoding fixes key order, whitespace and character encoding before hashing. Reject NaN and infinity because they are not JSON numbers, and use a cryptographic digest rather than Python's process-dependent hash.

import json
print(json.dumps({"b":2,"a":1},
                 sort_keys=True))
Your task

Write content_id(value). Encode a JSON-compatible value with sorted keys, separators (",", ":"), ensure_ascii=False, allow_nan=False; UTF-8 encode it and return its SHA-256 hexadecimal digest.