Spaces:
Sleeping
Sleeping
| # app.py | |
| from flask import Flask, render_template, request, jsonify | |
| # استيراد مكتبة الفحص الذكي الجديدة | |
| from zxcvbn import zxcvbn | |
| import os | |
| app = Flask(__name__) | |
| # ---------------------------------------------------------------------- | |
| # تحميل قائمة كلمات المرور المسربة | |
| # ---------------------------------------------------------------------- | |
| 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", "admin", "تيكتوك", "مرحبا123", "iloveyou"} | |
| 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() | |
| # ---------------------------------------------------------------------- | |
| # 1. منطق تحليل قوة كلمة المرور الذكي (باستخدام zxcvbn) | |
| # ---------------------------------------------------------------------- | |
| def check_password_strength(password): | |
| """تحليل القوة الذكي والتحقق من التسريب.""" | |
| # 1. التحقق من التسريب (الأولوية القصوى) | |
| is_leaked = password.lower() in LEAKED_PASSWORDS | |
| if is_leaked: | |
| status = "ضعيفة جداً" | |
| color = "red" | |
| percentage = 0 | |
| feedback = ["🚨 **خطر!** تم العثور عليها ضمن كلمات المرور المسربة أو الشائعة جداً."] | |
| return status, color, feedback, percentage | |
| # 2. تحليل القوة باستخدام zxcvbn (النتيجة من 0 إلى 4) | |
| results = zxcvbn(password) | |
| zxcvbn_score = results['score'] | |
| # 3. تحويل النقاط إلى نسبة مئوية وحالة | |
| percentage = int((zxcvbn_score / 4) * 100) | |
| feedback = [] | |
| # إضافة تحذيرات ونصائح النموذج إلى قائمة النصائح | |
| if results['feedback']['warning']: | |
| feedback.append(f"💡 تحذير: {results['feedback']['warning']}") | |
| if results['feedback']['suggestions']: | |
| feedback.extend([f"🌟 نصيحة: {s}" for s in results['feedback']['suggestions']]) | |
| # تحديد الحالة بناءً على النتيجة | |
| if zxcvbn_score >= 3: | |
| status = "آمنة" | |
| color = "green" | |
| elif zxcvbn_score == 2: | |
| status = "جيدة" | |
| color = "orange" | |
| else: # Score 0 or 1 | |
| status = "ضعيفة" | |
| color = "red" | |
| return status, color, feedback, percentage | |
| # ---------------------------------------------------------------------- | |
| # 2. مسارات تطبيق Flask | |
| # ---------------------------------------------------------------------- | |
| def index(): | |
| """مسار عرض الواجهة الأمامية (HTML).""" | |
| # يجب أن يكون الملف 'password-strength.html' داخل مجلد 'templates' | |
| return render_template('password-strength.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. تشغيل التطبيق | |
| # ---------------------------------------------------------------------- | |
| if __name__ == '__main__': | |
| # ضروري للتشغيل على Hugging Face | |
| app.run(host='0.0.0.0', port=7860, debug=True) |