Delete identity_verification.py
Browse files- identity_verification.py +0 -93
identity_verification.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
import hashlib
|
| 2 |
-
import os
|
| 3 |
-
import pyotp
|
| 4 |
-
import face_recognition
|
| 5 |
-
from cryptography.fernet import Fernet
|
| 6 |
-
import logging
|
| 7 |
-
|
| 8 |
-
# Security Configuration
|
| 9 |
-
SALT = os.urandom(32)
|
| 10 |
-
FERNET_KEY = Fernet.generate_key()
|
| 11 |
-
fernet = Fernet(FERNET_KEY)
|
| 12 |
-
SECRET_KEY = pyotp.random_base32()
|
| 13 |
-
|
| 14 |
-
# Logging setup
|
| 15 |
-
logging.basicConfig(filename="security.log", level=logging.INFO,
|
| 16 |
-
format="%(asctime)s - %(levelname)s - %(message)s")
|
| 17 |
-
|
| 18 |
-
# Secure User Database (Encrypted)
|
| 19 |
-
user_data = {}
|
| 20 |
-
|
| 21 |
-
# Utility Functions
|
| 22 |
-
def hash_password(password: str) -> str:
|
| 23 |
-
"""Hash the password using SHA-256 with a unique salt."""
|
| 24 |
-
salted_password = password.encode() + SALT
|
| 25 |
-
return hashlib.sha256(salted_password).hexdigest()
|
| 26 |
-
|
| 27 |
-
def encrypt_data(data: str) -> str:
|
| 28 |
-
"""Encrypt sensitive data using AES-256 (Fernet)."""
|
| 29 |
-
return fernet.encrypt(data.encode()).decode()
|
| 30 |
-
|
| 31 |
-
def decrypt_data(encrypted_data: str) -> str:
|
| 32 |
-
"""Decrypt sensitive data using AES-256 (Fernet)."""
|
| 33 |
-
return fernet.decrypt(encrypted_data.encode()).decode()
|
| 34 |
-
|
| 35 |
-
# User Registration
|
| 36 |
-
def register_user(username: str, password: str):
|
| 37 |
-
"""Register a user with hashed password and encrypted storage."""
|
| 38 |
-
if username in user_data:
|
| 39 |
-
logging.warning(f"User '{username}' already exists.")
|
| 40 |
-
return "[Error] User already exists."
|
| 41 |
-
hashed_password = hash_password(password)
|
| 42 |
-
user_data[username] = {"password": encrypt_data(hashed_password), "2FA": None}
|
| 43 |
-
logging.info(f"User '{username}' registered securely.")
|
| 44 |
-
return f"[Success] User '{username}' registered securely."
|
| 45 |
-
|
| 46 |
-
# Two-Factor Authentication (2FA) Setup
|
| 47 |
-
def setup_2fa(username: str):
|
| 48 |
-
"""Generate and store 2FA secret key for the user."""
|
| 49 |
-
if username not in user_data:
|
| 50 |
-
logging.warning(f"User '{username}' not found.")
|
| 51 |
-
return "[Error] User not found."
|
| 52 |
-
user_data[username]["2FA"] = SECRET_KEY
|
| 53 |
-
logging.info(f"2FA setup for user '{username}'.")
|
| 54 |
-
return f"[2FA] Scan this OTP Key in your Authenticator: {SECRET_KEY}"
|
| 55 |
-
|
| 56 |
-
# Login with Identity Verification
|
| 57 |
-
def login(username: str, password: str, otp_code: str):
|
| 58 |
-
"""Verify user identity using password and 2FA."""
|
| 59 |
-
if username not in user_data:
|
| 60 |
-
logging.warning(f"User '{username}' not found.")
|
| 61 |
-
return "[Error] User not found."
|
| 62 |
-
|
| 63 |
-
# Verify Password
|
| 64 |
-
stored_password = decrypt_data(user_data[username]["password"])
|
| 65 |
-
if stored_password != hash_password(password):
|
| 66 |
-
logging.warning(f"Invalid password for user '{username}'.")
|
| 67 |
-
return "[Error] Invalid password."
|
| 68 |
-
|
| 69 |
-
# Verify 2FA (Time-based OTP)
|
| 70 |
-
totp = pyotp.TOTP(user_data[username]["2FA"])
|
| 71 |
-
if not totp.verify(otp_code):
|
| 72 |
-
logging.warning(f"Invalid OTP for user '{username}'.")
|
| 73 |
-
return "[Error] Invalid OTP."
|
| 74 |
-
|
| 75 |
-
logging.info(f"User '{username}' logged in securely.")
|
| 76 |
-
return f"[Success] User '{username}' logged in securely."
|
| 77 |
-
|
| 78 |
-
# Biometric Face Recognition
|
| 79 |
-
def verify_face():
|
| 80 |
-
"""Verify user face against saved authorized face."""
|
| 81 |
-
try:
|
| 82 |
-
known_image = face_recognition.load_image_file("authorized_face.jpg")
|
| 83 |
-
unknown_image = face_recognition.load_image_file("attempt.jpg")
|
| 84 |
-
|
| 85 |
-
known_encoding = face_recognition.face_encodings(known_image)[0]
|
| 86 |
-
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
|
| 87 |
-
|
| 88 |
-
result = face_recognition.compare_faces([known_encoding], unknown_encoding)[0]
|
| 89 |
-
logging.info(f"Face verification result: {result}")
|
| 90 |
-
return result
|
| 91 |
-
except Exception as e:
|
| 92 |
-
logging.error(f"Face verification failed: {e}")
|
| 93 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|