Fingerprint the configuration you intend to compare
Hash canonical configuration fields while excluding collection metadata and irrelevant ordering.
Define what a change means
Hashing an exported file detects byte changes, including whitespace and collection timestamps. Sometimes that is exactly what you need. For configuration comparison, however, this lesson defines a semantic fingerprint over selected fields: device, interface, administrative state, description and VLAN set.
Exclude export metadata such as collected_at and operational counters. Sort interface rows by normalized name, deduplicate and sort VLAN integers, and keep descriptions exactly as supplied. A space or Unicode character in a description remains a meaningful change under this contract.
Canonical bytes make the hash reproducible
Construct a fresh canonical object and encode it with sorted JSON keys, no optional whitespace, UTF-8 and ensure_ascii disabled. The SHA-256 digest then describes these specified bytes. Two equivalent exports produce the same digest even when their original dictionaries or lists were ordered differently.
Validate duplicate interfaces and invalid policy values rather than hashing an ambiguous object. Do not mutate the original export while sorting; it may still be needed for evidence or display.
A matching digest means the selected configuration fields match under this normalization. It does not authenticate who supplied the export, and it deliberately says nothing about excluded counters or timestamps. A trustworthy baseline still needs a separate process for deciding what should be accepted as intended state.
import json, hashlib, re
value = {"device":"edge-a", "interfaces":[]}
data = json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",",":")).encode("utf-8")
print(hashlib.sha256(data).hexdigest())
Put it into practice.
Define config_fingerprint(config) returning a lowercase SHA-256 hex digest. Keep only normalized device and interfaces containing normalized interface, exact admin (up/down), exact string description and deduplicated numeric-sorted VLANs (integer 1..4094, excluding bool). Require nonempty identities and unique normalized interfaces; invalid values raise ValueError. Sort interfaces by name; exclude all other fields. Hash UTF-8 JSON with ensure_ascii=False, sort_keys=True, separators=(", ", ":"), allow_nan=False. Preserve inputs.
See an explained solution
import json, hashlib, re
def config_fingerprint(config):
device = config["device"].strip().casefold()
rows, seen = [], set()
if not device:
raise ValueError("Device is empty")
for row in config["interfaces"]:
name = row["interface"].strip().casefold()
vlans = row["vlans"]
if not name or name in seen or row["admin"] not in ("up","down") or not isinstance(vlans,list) or any(type(v) is not int or not 1 <= v <= 4094 for v in vlans) or not isinstance(row["description"],str):
raise ValueError("Invalid or duplicate interface")
seen.add(name)
rows.append({"interface":name,"admin":row["admin"],"description":row["description"],"vlans":sorted(set(vlans))})
canonical = {"device":device,"interfaces":sorted(rows,key=lambda row:row["interface"])}
data = json.dumps(canonical,ensure_ascii=False,sort_keys=True,separators=(",",":"),allow_nan=False).encode("utf-8")
return hashlib.sha256(data).hexdigest()
if __name__ == "__main__":
print(config_fingerprint({"device":"edge-a","interfaces":[]}))
The fingerprint deliberately captures configuration meaning under a documented policy. Excluded collection metadata does not create false drift, while meaningful description and VLAN changes do.