Skydata001's picture
Update app.py
521c3ca verified
# app.py (الكود المحدث: إزالة الأحرف العربية)
from flask import Flask, render_template, request, jsonify
from zxcvbn import zxcvbn
import os
import random
import string
import time
import secrets
app = Flask(__name__)
# ----------------------------------------------------------------------
# تعريف مجموعات الأحرف والرموز (إزالة الأحرف العربية)
# ----------------------------------------------------------------------
CHARSETS = {
'lower': string.ascii_lowercase,
'upper': string.ascii_uppercase,
'digits': string.digits,
# رموز قوية وآمنة
'symbols': '!@#$%^&*()-_+=[]{}|;:,.<>?',
# تم إزالة: 'arabic': 'ابتثجحخدذرزسشصضطظعغفقكلمنهوي'
}
# ----------------------------------------------------------------------
# تحميل قائمة كلمات المرور المسربة (يبقى كما هو)
# ----------------------------------------------------------------------
# ... (كود load_leaked_passwords يبقى كما هو) ...
LEAKED_PASSWORDS = set()
def load_leaked_passwords(file_path='leaked_passwords.txt'):
# ... (باقي كود الدالة كما هو)
if not os.path.exists(file_path):
print(f"⚠️ الملف {file_path} غير موجود. سيتم استخدام قائمة صغيرة للاختبار.")
return {"123456", "password", "كلمةسر"}
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
LEAKED_PASSWORDS.add(line.strip().lower())
print(f"✅ تم تحميل {len(LEAKED_PASSWORDS)} كلمة مرور مسربة بنجاح.")
except Exception as e:
print(f"❌ حدث خطأ أثناء تحميل الملف: {e}. يتم استخدام قائمة الاختبار.")
return {"123456", "password"}
LEAKED_PASSWORDS = load_leaked_passwords()
# ----------------------------------------------------------------------
# دالة توليد كلمات السر الذكية (تعديل مجموعات الأحرف)
# ----------------------------------------------------------------------
def generate_strong_password(difficulty_level):
"""
تولد كلمة مرور قوية باللغة الإنجليزية فقط بناءً على مستوى الصعوبة.
"""
# تحديد طول ومجموعات الأحرف بناءً على الصعوبة
if difficulty_level == '100':
length = secrets.choice(range(18, 22))
# تم تعديل هذا السطر لحذف CHARSETS['arabic']
char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols']]
min_complexity = 4
elif difficulty_level == '75':
length = secrets.choice(range(14, 18))
char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols']]
min_complexity = 4
elif difficulty_level == '50':
length = secrets.choice(range(10, 14))
char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits']]
min_complexity = 3
else: # 25%
length = secrets.choice(range(8, 11))
char_pools = [CHARSETS['lower'], CHARSETS['digits']]
min_complexity = 2
# ... (باقي كود التوليد والتحقق يبقى كما هو) ...
for _ in range(5):
all_chars = "".join(char_pools)
password = []
for pool in char_pools:
password.append(secrets.choice(pool))
remaining_length = length - len(char_pools)
if remaining_length > 0:
password.extend([secrets.choice(all_chars) for _ in range(remaining_length)])
secrets.SystemRandom().shuffle(password)
final_password = "".join(password)
if final_password.lower() in LEAKED_PASSWORDS:
continue
zxcvbn_score = zxcvbn(final_password)['score']
if zxcvbn_score >= min_complexity:
return final_password, zxcvbn_score
return "Error-TryAgain", 0
# ----------------------------------------------------------------------
# مسارات تطبيق Flask (تبقى كما هي)
# ----------------------------------------------------------------------
@app.route('/')
def index():
return render_template('generator.html')
@app.route('/generate', methods=['POST'])
def generate():
data = request.get_json()
difficulty = data.get('difficulty', '50')
password, score = generate_strong_password(difficulty)
percentage = int((score / 4) * 100) if score > 0 else 0
if score >= 3:
status = "آمنة جداً (قوة: 4/4)" if score == 4 else "آمنة (قوة: 3/4)"
color = "green"
elif score == 2:
status = "جيدة (قوة: 2/4)"
color = "orange"
else:
status = "ضعيفة جداً"
color = "red"
return jsonify({
'password': password,
'status': status,
'color': color,
'percentage': percentage,
'difficulty': difficulty
})
# ----------------------------------------------------------------------
# 3. تشغيل التطبيق (يبقى كما هو)
# ----------------------------------------------------------------------
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)