-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
134 lines (109 loc) · 4.44 KB
/
Copy pathapp.py
File metadata and controls
134 lines (109 loc) · 4.44 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from flask import Flask, render_template, request, redirect, url_for, session
import os
app = Flask(__name__)
app.secret_key = "super_secret_chat_key"
MESSAGES_FILE = "messages.txt"
USERS_FILE = "users.txt"
# Encryption helper: 5-letter Caeser-style shift cipher using maketrans
ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
SHIFTED = "fghijklmnopqrstuvwxyzabcDEFG...0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Standard 5-character rotation mapping
# Build translation tables for shift = +5
NORMAL_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
SHIFTED_CHARS = "fghijklmnopqrstuvwxyzabcdeFGHIJKLMNOPQRSTUVWXYZABCDE5678901234"
ENCRYPT_TRANS = str.maketrans(NORMAL_CHARS, SHIFTED_CHARS)
DECRYPT_TRANS = str.maketrans(SHIFTED_CHARS, NORMAL_CHARS)
def encrypt(text):
return text.translate(ENCRYPT_TRANS)
def decrypt(text):
return text.translate(DECRYPT_TRANS)
# Ensure data storage files exist
for file_path in [MESSAGES_FILE, USERS_FILE]:
if not os.path.exists(file_path):
with open(file_path, "w", encoding="utf-8") as f:
pass
def user_exists(username):
if not os.path.exists(USERS_FILE):
return False
with open(USERS_FILE, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
decrypted_line = decrypt(line)
if ":" in decrypted_line:
stored_user, _ = decrypted_line.split(":", 1)
if stored_user == username:
return True
return False
def save_user(username, password):
if not user_exists(username):
user_line = f"{username}:{password}"
encrypted_line = encrypt(user_line)
with open(USERS_FILE, "a", encoding="utf-8") as f:
f.write(encrypted_line + "\n")
return True
return False
def check_credentials(username, password):
if not os.path.exists(USERS_FILE):
return False
with open(USERS_FILE, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
decrypted_line = decrypt(line)
if ":" in decrypted_line:
stored_user, stored_pass = decrypted_line.split(":", 1)
if stored_user == username and stored_pass == password:
return True
return False
def save_message(username, message):
with open(MESSAGES_FILE, "a", encoding="utf-8") as f:
f.write(f"{username}: {message}\n")
def load_messages():
if os.path.exists(MESSAGES_FILE):
with open(MESSAGES_FILE, "r", encoding="utf-8") as f:
return [line.strip() for line in f.readlines() if line.strip()]
return []
@app.route("/", methods=["GET", "POST"])
def home():
if "username" not in session:
return redirect(url_for("login"))
if request.method == "POST":
user_message = request.form.get("message", "").strip()
if user_message:
save_message(session["username"], user_message)
return redirect(url_for("home"))
messages_list = load_messages()
return render_template("index.html", messages=messages_list, user=session["username"])
@app.route("/login", methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
username = request.form.get("username", "").strip()
password = request.form.get("password", "").strip()
if check_credentials(username, password):
session["username"] = username
return redirect(url_for("home"))
else:
error = "Invalid username or password"
return render_template("login.html", error=error)
@app.route("/signup", methods=["GET", "POST"])
@app.route("/signup", methods=["GET", "POST"])
def signup():
error = None
if request.method == "POST":
username = request.form.get("username", "").strip()
password = request.form.get("password", "").strip()
if username and password:
if user_exists(username):
error = "Username already exists. Please choose another."
else:
save_user(username, password)
session["username"] = username
return redirect(url_for("home"))
return render_template("signup.html", error=error)
@app.route("/logout")
def logout():
session.pop("username", None)
return redirect(url_for("login"))
if __name__ == "__main__":
app.run(debug=True)