import json
import hashlib
def verify_user_records(filepath, record_hash_field='record_hash'):
"""
Verifies the integrity of user records stored in a JSON file.
Args:
filepath (str): The path to the JSON file containing user records.
record_hash_field (str): The field name in each record that stores the hash.
Returns:
list: A list of user records that have integrity issues (hash mismatch).
Returns an empty list if all records are valid.
"""
integrity_issues = []
try:
with open(filepath, 'r') as f:
records = json.load(f)
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
return []
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {filepath}")
return []
for record in records:
try:
record_hash = record.get(record_hash_field)
if record_hash is None:
print(f"Warning: Record missing '{record_hash_field}'. Skipping integrity check.")
integrity_issues.append(record) #Consider as invalid since hash is missing.
continue
# Recalculate the hash from the record data
data_string = json.dumps(record, sort_keys=True).encode('utf-8')
calculated_hash = hashlib.sha256(data_string).hexdigest()
if calculated_hash != record_hash:
print(f"Integrity issue: Record with ID {record.get('id', 'Unknown')} has incorrect hash.")
integrity_issues.append(record)
except Exception as e:
print(f"Error processing record: {e}")
integrity_issues.append(record)
return integrity_issues
if __name__ == '__main__':
# Example usage:
filepath = 'user_records.json' # Replace with your file path
# Create a dummy user_records.json for testing
dummy_data = [
{"id": 1, "name": "Alice", "email": "alice@example.com", "record_hash": "e5b7e3b94d1e91946a34287b7428472a67873790e49b3556727c55f124821580"},
{"id": 2, "name": "Bob", "email": "bob@example.com", "record_hash": "2cedc9c46a91b88377062b3d851c44e478762110454f131302fc2115001760c9"},
{"id": 3, "name": "Charlie", "email": "charlie@example.com", "record_hash": "e5b7e3b94d1e91946a34287b7428472a67873790e49b3556727c55f124821580"}, #Duplicate hash
{"id": 4, "name": "David", "email": "david@example.com"} #Missing record_hash
]
with open(filepath, 'w') as f:
json.dump(dummy_data, f, indent=4)
integrity_issues = verify_user_records(filepath)
if integrity_issues:
print("Integrity issues found:")
for record in integrity_issues:
print(record)
else:
print("All records are valid.")
Add your comment