-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathHospital_hash.py
More file actions
88 lines (77 loc) · 2.89 KB
/
Hospital_hash.py
File metadata and controls
88 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Doctor Authentication System
import hashlib
# Database of Doctors (Hash Map: Username → Hashed Password + Details)
doctors_db = {
# Format: {username: {"password_hash": "...", "name": "...", "department": "..."}}
"dr_strange": {
"password_hash": hashlib.sha256("Spell@2024".encode()).hexdigest(),
"name": "Stephen Strange",
"department": "Neurosurgery"
},
"dr_house": {
"password_hash": hashlib.sha256("Lupus!123".encode()).hexdigest(),
"name": "Gregory House",
"department": "Diagnostics"
}
}
# Patient Records (Secured behind authentication)
patient_records = {
"ICU-101": {"name": "Tony Stark", "condition": "Shrapnel injury"},
"ER-205": {"name": "Bruce Banner", "condition": "Gamma radiation exposure"}
}
def register_doctor(username: str, password: str, name: str, department: str):
"""Add new doctor with hashed password"""
if username in doctors_db:
print("⚠️ Username already exists!")
return False
# Create password hash (add salt in real systems!)
password_hash = hashlib.sha256(password.encode()).hexdigest()
doctors_db[username] = {
"password_hash": password_hash,
"name": name,
"department": department
}
print(f"✅ {name} registered in {department}!")
return True
def doctor_login(username: str, password: str):
"""Authenticate doctor using hash comparison"""
if username not in doctors_db:
print("❌ User not found")
return False
# Hash the attempt
attempt_hash = hashlib.sha256(password.encode()).hexdigest()
stored_hash = doctors_db[username]["password_hash"]
if attempt_hash == stored_hash:
print(f"🔓 Access granted! Welcome Dr. {doctors_db[username]['name']}")
return True
else:
print("❌ Invalid password")
return False
def access_patient_record(username: str, patient_id: str):
"""Securely access patient data after auth"""
if doctor_login(username, input("Password: ")):
record = patient_records.get(patient_id)
if record:
print(f"\n📝 Patient {patient_id}:")
for key, value in record.items():
print(f" {key.capitalize()}: {value}")
else:
print("⚠️ Record not found")
else:
print("🚫 Access denied to patient records")
# Demo Workflow
if __name__ == "__main__":
# Register new doctor
register_doctor(
username="dr_grey",
password="Heal$oul123",
name="Meredith Grey",
department="General Surgery"
)
# Attempt login
print("\n[ Login Attempt ]")
doctor_login("dr_strange", "Spell@2024") # Correct → Success
doctor_login("dr_house", "wrong!pass") # Incorrect → Failure
# Access patient data (simulate)
print("\n[ Access Patient Record ]")
access_patient_record("dr_strange", "ICU-101")