Introduction
Imagine sending a message or saving important information, but it gets changed or corrupted without you knowing. Data integrity checks help catch these mistakes to keep information accurate and trustworthy.
Think of sending a sealed letter with a wax stamp. The stamp shows if the letter was opened or changed during delivery. If the stamp is broken, you know the letter might not be safe.
┌─────────────────────────────┐
│ Original Data │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Generate Integrity Code │
│ (checksum, hash, parity) │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Store or Transmit Data + │
│ Integrity Code │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Receive or Retrieve Data │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Recalculate Integrity Code │
└─────────────┬───────────────┘
│
▼
┌──────────────┬──────────────┐
│ │ │
│ Codes Match │ Codes Differ│
│ (Pass) │ (Fail) │
│ │ │
└──────┬───────┴──────┬───────┘
│ │
▼ ▼
Use Data Reject or Request
Data Againimport hashlib def calculate_hash(data: str) -> str: return hashlib.sha256(data.encode()).hexdigest() original_data = "Hello, world!" hash_original = calculate_hash(original_data) # Simulate data transmission received_data = "Hello, world!" hash_received = calculate_hash(received_data) if hash_original == hash_received: print("Data integrity verified: no changes detected.") else: print("Data integrity check failed: data was altered.")