Spaces:
Sleeping
Sleeping
| # app.py | |
| from flask import Flask, render_template, request, jsonify | |
| import re | |
| import os # لإدارة الملفات والتحقق من وجودها | |
| app = Flask(__name__) | |
| # ---------------------------------------------------------------------- | |
| # تحميل قائمة كلمات المرور المسربة عند بدء التشغيل | |
| # ---------------------------------------------------------------------- | |
| # استخدام مجموعة (Set) لسرعة البحث العالية جداً | |
| 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", "كلمةسر", "محمد", "qwerty"} | |
| 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}") | |
| # تنفيذ دالة التحميل عند بدء التطبيق | |
| # سيتم تخزين جميع الكلمات في متغير LEAKED_PASSWORDS | |
| LEAKED_PASSWORDS = load_leaked_passwords() | |
| # ---------------------------------------------------------------------- | |
| # 1. منطق تحليل قوة كلمة المرور والتحقق من التسريب | |
| # ---------------------------------------------------------------------- | |
| def check_password_strength(password): | |
| """تحليل قوة كلمة المرور وحساب النسبة المئوية.""" | |
| score = 0 | |
| feedback = [] | |
| # ⚠️ أقصى نقاط يمكن تحقيقها هي 6 | |
| MAX_SCORE = 6 | |
| # أ. قواعد قوة كلمة المرور | |
| if len(password) >= 12: | |
| score += 3 | |
| feedback.append("✔️ طول ممتاز (12 حرفًا أو أكثر).") | |
| elif len(password) >= 8: | |
| score += 2 | |
| feedback.append("⭐ طول جيد (8-11 حرفًا).") | |
| else: | |
| feedback.append("❌ كلمة المرور قصيرة جداً (يجب أن تكون 8 أحرف على الأقل).") | |
| # تنوع الأحرف (نقاط إضافية) | |
| if re.search(r"[a-z]", password): score += 1 | |
| if re.search(r"[A-Z]", password): score += 1 | |
| if re.search(r"\d", password): score += 1 | |
| if re.search(r"[^a-zA-Z0-9\s]", password): score += 1 # الرموز والأحرف العربية وغيرها | |
| # ب. التحقق من التسريب (في المجموعة الضخمة) | |
| # ملاحظة: يتم التحقق من القائمة الصغيرة إذا لم يكن الملف النصي موجوداً | |
| is_leaked = password.lower() in LEAKED_PASSWORDS | |
| # ------------------ | |
| # ج. تحديد الحالة وحساب النسبة المئوية | |
| # ------------------ | |
| if is_leaked: | |
| status = "ضعيفة جداً" | |
| color = "red" | |
| percentage = 0 | |
| feedback.append("🚨 **خطر!** تم العثور عليها ضمن كلمات المرور المسربة أو الشائعة جداً.") | |
| else: | |
| # حساب النسبة المئوية | |
| percentage = round((score / MAX_SCORE) * 100) | |
| if percentage > 100: percentage = 100 | |
| if score >= 5: | |
| status = "آمنة" | |
| color = "green" | |
| elif score >= 3: | |
| status = "جيدة" | |
| color = "orange" | |
| else: | |
| status = "ضعيفة" | |
| color = "red" | |
| # نُرجع النسبة المئوية أيضاً الآن | |
| return status, color, feedback, percentage | |
| # ---------------------------------------------------------------------- | |
| # 2. مسارات تطبيق Flask | |
| # ---------------------------------------------------------------------- | |
| def index(): | |
| return render_template('index.html') | |
| def check(): | |
| data = request.get_json() | |
| password = data.get('password', '') | |
| # استقبال النسبة المئوية | |
| status, color, feedback, percentage = check_password_strength(password) | |
| # إرجاع النتيجة | |
| return jsonify({ | |
| 'status': status, | |
| 'color': color, | |
| 'feedback': feedback, | |
| 'percentage': percentage # إرجاع النسبة | |
| }) | |
| # ---------------------------------------------------------------------- | |
| # 3. تشغيل التطبيق (على المنفذ 7860 للتوافق مع Hugging Face) | |
| # ---------------------------------------------------------------------- | |
| if __name__ == '__main__': | |
| # التشغيل على host='0.0.0.0' و port=7860 | |
| app.run(host='0.0.0.0', port=7860, debug=True) |