1. import json
  2. import hashlib
  3. def verify_user_records(filepath, record_hash_field='record_hash'):
  4. """
  5. Verifies the integrity of user records stored in a JSON file.
  6. Args:
  7. filepath (str): The path to the JSON file containing user records.
  8. record_hash_field (str): The field name in each record that stores the hash.
  9. Returns:
  10. list: A list of user records that have integrity issues (hash mismatch).
  11. Returns an empty list if all records are valid.
  12. """
  13. integrity_issues = []
  14. try:
  15. with open(filepath, 'r') as f:
  16. records = json.load(f)
  17. except FileNotFoundError:
  18. print(f"Error: File not found at {filepath}")
  19. return []
  20. except json.JSONDecodeError:
  21. print(f"Error: Invalid JSON format in {filepath}")
  22. return []
  23. for record in records:
  24. try:
  25. record_hash = record.get(record_hash_field)
  26. if record_hash is None:
  27. print(f"Warning: Record missing '{record_hash_field}'. Skipping integrity check.")
  28. integrity_issues.append(record) #Consider as invalid since hash is missing.
  29. continue
  30. # Recalculate the hash from the record data
  31. data_string = json.dumps(record, sort_keys=True).encode('utf-8')
  32. calculated_hash = hashlib.sha256(data_string).hexdigest()
  33. if calculated_hash != record_hash:
  34. print(f"Integrity issue: Record with ID {record.get('id', 'Unknown')} has incorrect hash.")
  35. integrity_issues.append(record)
  36. except Exception as e:
  37. print(f"Error processing record: {e}")
  38. integrity_issues.append(record)
  39. return integrity_issues
  40. if __name__ == '__main__':
  41. # Example usage:
  42. filepath = 'user_records.json' # Replace with your file path
  43. # Create a dummy user_records.json for testing
  44. dummy_data = [
  45. {"id": 1, "name": "Alice", "email": "alice@example.com", "record_hash": "e5b7e3b94d1e91946a34287b7428472a67873790e49b3556727c55f124821580"},
  46. {"id": 2, "name": "Bob", "email": "bob@example.com", "record_hash": "2cedc9c46a91b88377062b3d851c44e478762110454f131302fc2115001760c9"},
  47. {"id": 3, "name": "Charlie", "email": "charlie@example.com", "record_hash": "e5b7e3b94d1e91946a34287b7428472a67873790e49b3556727c55f124821580"}, #Duplicate hash
  48. {"id": 4, "name": "David", "email": "david@example.com"} #Missing record_hash
  49. ]
  50. with open(filepath, 'w') as f:
  51. json.dump(dummy_data, f, indent=4)
  52. integrity_issues = verify_user_records(filepath)
  53. if integrity_issues:
  54. print("Integrity issues found:")
  55. for record in integrity_issues:
  56. print(record)
  57. else:
  58. print("All records are valid.")

Add your comment