-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
import json
import hashlib
def calculate_checksum(record):
# Exclude the checksum field itself
record_without_checksum = {k: v for k, v in record.items() if k != "checksum"}
# Sort keys to ensure deterministic order
serialized_record = json.dumps(record_without_checksum, sort_keys=True)
# Compute SHA-256 hash
return hashlib.sha256(serialized_record.encode('utf-8')).hexdigest()
# Example usage
record = {
"type": "output",
"ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:1",
"label": "Output",
"spendable": "false"
}
# Add the checksum to the record
record["checksum"] = calculate_checksum(record)
# Simulate a change to the record
modified_record = {
"type": "output",
"ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:1",
"label": "Outp", # simulate truncated data
"spendable": "false"
}
# Check if the modified record's checksum matches
is_changed = record.get("checksum") != calculate_checksum(modified_record)
print("Record has", "changed" if is_changed else "not changed")
Metadata
Metadata
Assignees
Labels
No labels