Skydata001 commited on
Commit
521c3ca
·
verified ·
1 Parent(s): 51c0ae5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py
2
 
3
  from flask import Flask, render_template, request, jsonify
4
  from zxcvbn import zxcvbn
@@ -6,30 +6,31 @@ import os
6
  import random
7
  import string
8
  import time
9
- import secrets # المكتبة الأكثر أمانًا لتوليد عشوائية عالية
10
 
11
  app = Flask(__name__)
12
 
13
  # ----------------------------------------------------------------------
14
- # تعريف مجموعات الأحرف والرموز
15
  # ----------------------------------------------------------------------
16
  CHARSETS = {
17
  'lower': string.ascii_lowercase,
18
  'upper': string.ascii_uppercase,
19
  'digits': string.digits,
20
- # رموز قوية وآمنة (تجنب الرموز التي يصعب كتابتها أو التي تستخدمها قواعد البيانات)
21
  'symbols': '!@#$%^&*()-_+=[]{}|;:,.<>?',
22
- 'arabic': 'ابتثجحخدذرزسشصضطظعغفقكلمنهوي' # إضافة أحرف عربية
23
  }
24
 
25
  # ----------------------------------------------------------------------
26
- # تحميل قائمة كلمات المرور المسربة (من ملف leaked_passwords.txt)
27
  # ----------------------------------------------------------------------
 
28
 
29
  LEAKED_PASSWORDS = set()
30
 
31
  def load_leaked_passwords(file_path='leaked_passwords.txt'):
32
- # ... (كود تحميل الملف يبقى كما هو)
33
  if not os.path.exists(file_path):
34
  print(f"⚠️ الملف {file_path} غير موجود. سيتم استخدام قائمة صغيرة للاختبار.")
35
  return {"123456", "password", "كلمةسر"}
@@ -47,77 +48,65 @@ LEAKED_PASSWORDS = load_leaked_passwords()
47
 
48
 
49
  # ----------------------------------------------------------------------
50
- # دالة توليد كلمات السر الذكية
51
  # ----------------------------------------------------------------------
52
 
53
  def generate_strong_password(difficulty_level):
54
  """
55
- تولد كلمة مرور قوية بناءً على مستوى الصعوبة.
56
-
57
- المستويات: 100, 75, 50, 25 (تم تبسيطها إلى 4 مستويات)
58
  """
59
 
60
  # تحديد طول ومجموعات الأحرف بناءً على الصعوبة
61
  if difficulty_level == '100':
62
- length = secrets.choice(range(18, 22)) # طول عشوائي 18-21
63
- char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols'], CHARSETS['arabic']]
64
- min_complexity = 4 # ضمان أن يكون 4/4 في zxcvbn
 
65
  elif difficulty_level == '75':
66
- length = secrets.choice(range(14, 18)) # طول عشوائي 14-17
67
  char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols']]
68
- min_complexity = 4 # ضمان أن يكون 4/4 في zxcvbn
69
  elif difficulty_level == '50':
70
- length = secrets.choice(range(10, 14)) # طول عشوائي 10-13
71
  char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits']]
72
- min_complexity = 3 # ضمان أن يكون 3/4 في zxcvbn
73
  else: # 25%
74
- length = secrets.choice(range(8, 11)) # طول عشوائي 8-10
75
  char_pools = [CHARSETS['lower'], CHARSETS['digits']]
76
- min_complexity = 2 # ضمان أن يكون 2/4 في zxcvbn
77
 
78
 
79
- # محاولة التوليد حتى نضمن القوة وعدم التسريب (5 محاولات كحد أقصى)
80
  for _ in range(5):
81
- # تجميع كل الأحرف الممكنة
82
  all_chars = "".join(char_pools)
83
 
84
- # ضمان وجود حرف واحد على الأقل من كل مجموعة أساسية
85
  password = []
86
  for pool in char_pools:
87
  password.append(secrets.choice(pool))
88
 
89
- # ملء بقية الطول بأحرف عشوائية
90
  remaining_length = length - len(char_pools)
91
  if remaining_length > 0:
92
  password.extend([secrets.choice(all_chars) for _ in range(remaining_length)])
93
 
94
- # خلط كلمة المرور لضمان عدم وجود تسلسل (مثل: A1B2C3...)
95
  secrets.SystemRandom().shuffle(password)
96
  final_password = "".join(password)
97
 
98
- # التحقق من الشروط:
99
-
100
- # 1. التحقق من عدم التسريب (Must Not Be Leaked)
101
  if final_password.lower() in LEAKED_PASSWORDS:
102
- continue # مسربة، حاول مرة أخرى
103
 
104
- # 2. التحقق من القوة باستخدام zxcvbn (Must Be Strong Enough)
105
  zxcvbn_score = zxcvbn(final_password)['score']
106
  if zxcvbn_score >= min_complexity:
107
- # تم استيفاء جميع الشروط
108
  return final_password, zxcvbn_score
109
 
110
- # إذا فشل التوليد بعد 5 محاولات، نرجع كلمة مرور بسيطة ونحذر
111
  return "Error-TryAgain", 0
112
 
113
 
114
  # ----------------------------------------------------------------------
115
- # مسارات تطبيق Flask
116
  # ----------------------------------------------------------------------
117
 
118
  @app.route('/')
119
  def index():
120
- # استخدام ملف generator.html الجديد
121
  return render_template('generator.html')
122
 
123
  @app.route('/generate', methods=['POST'])
@@ -125,13 +114,10 @@ def generate():
125
  data = request.get_json()
126
  difficulty = data.get('difficulty', '50')
127
 
128
- # توليد كلمة المرور
129
  password, score = generate_strong_password(difficulty)
130
 
131
- # حساب النسبة المئوية
132
  percentage = int((score / 4) * 100) if score > 0 else 0
133
 
134
- # تحديد الحالة بناءً على النتيجة
135
  if score >= 3:
136
  status = "آمنة جداً (قوة: 4/4)" if score == 4 else "آمنة (قوة: 3/4)"
137
  color = "green"
@@ -140,7 +126,7 @@ def generate():
140
  color = "orange"
141
  else:
142
  status = "ضعيفة جداً"
143
- color = "red" # قد يكون هذا خطأ التوليد
144
 
145
  return jsonify({
146
  'password': password,
@@ -151,9 +137,8 @@ def generate():
151
  })
152
 
153
  # ----------------------------------------------------------------------
154
- # 3. تشغيل التطبيق
155
  # ----------------------------------------------------------------------
156
 
157
  if __name__ == '__main__':
158
- # ضروري للتشغيل على Hugging Face
159
  app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ # app.py (الكود المحدث: إزالة الأحرف العربية)
2
 
3
  from flask import Flask, render_template, request, jsonify
4
  from zxcvbn import zxcvbn
 
6
  import random
7
  import string
8
  import time
9
+ import secrets
10
 
11
  app = Flask(__name__)
12
 
13
  # ----------------------------------------------------------------------
14
+ # تعريف مجموعات الأحرف والرموز (إزالة الأحرف العربية)
15
  # ----------------------------------------------------------------------
16
  CHARSETS = {
17
  'lower': string.ascii_lowercase,
18
  'upper': string.ascii_uppercase,
19
  'digits': string.digits,
20
+ # رموز قوية وآمنة
21
  'symbols': '!@#$%^&*()-_+=[]{}|;:,.<>?',
22
+ # تم إزالة: 'arabic': 'ابتثجحخدذرزسشصضطظعغفقكلمنهوي'
23
  }
24
 
25
  # ----------------------------------------------------------------------
26
+ # تحميل قائمة كلمات المرور المسربة (يبقى كما هو)
27
  # ----------------------------------------------------------------------
28
+ # ... (كود load_leaked_passwords يبقى كما هو) ...
29
 
30
  LEAKED_PASSWORDS = set()
31
 
32
  def load_leaked_passwords(file_path='leaked_passwords.txt'):
33
+ # ... (باقي كود الدالة كما هو)
34
  if not os.path.exists(file_path):
35
  print(f"⚠️ الملف {file_path} غير موجود. سيتم استخدام قائمة صغيرة للاختبار.")
36
  return {"123456", "password", "كلمةسر"}
 
48
 
49
 
50
  # ----------------------------------------------------------------------
51
+ # دالة توليد كلمات السر الذكية (تعديل مجموعات الأحرف)
52
  # ----------------------------------------------------------------------
53
 
54
  def generate_strong_password(difficulty_level):
55
  """
56
+ تولد كلمة مرور قوية باللغة الإنجليزية فقط بناءً على مستوى الصعوبة.
 
 
57
  """
58
 
59
  # تحديد طول ومجموعات الأحرف بناءً على الصعوبة
60
  if difficulty_level == '100':
61
+ length = secrets.choice(range(18, 22))
62
+ # تم تعديل هذا السطر لحذف CHARSETS['arabic']
63
+ char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols']]
64
+ min_complexity = 4
65
  elif difficulty_level == '75':
66
+ length = secrets.choice(range(14, 18))
67
  char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits'], CHARSETS['symbols']]
68
+ min_complexity = 4
69
  elif difficulty_level == '50':
70
+ length = secrets.choice(range(10, 14))
71
  char_pools = [CHARSETS['lower'], CHARSETS['upper'], CHARSETS['digits']]
72
+ min_complexity = 3
73
  else: # 25%
74
+ length = secrets.choice(range(8, 11))
75
  char_pools = [CHARSETS['lower'], CHARSETS['digits']]
76
+ min_complexity = 2
77
 
78
 
79
+ # ... (باقي كود التوليد والتحقق يبقى كما هو) ...
80
  for _ in range(5):
 
81
  all_chars = "".join(char_pools)
82
 
 
83
  password = []
84
  for pool in char_pools:
85
  password.append(secrets.choice(pool))
86
 
 
87
  remaining_length = length - len(char_pools)
88
  if remaining_length > 0:
89
  password.extend([secrets.choice(all_chars) for _ in range(remaining_length)])
90
 
 
91
  secrets.SystemRandom().shuffle(password)
92
  final_password = "".join(password)
93
 
 
 
 
94
  if final_password.lower() in LEAKED_PASSWORDS:
95
+ continue
96
 
 
97
  zxcvbn_score = zxcvbn(final_password)['score']
98
  if zxcvbn_score >= min_complexity:
 
99
  return final_password, zxcvbn_score
100
 
 
101
  return "Error-TryAgain", 0
102
 
103
 
104
  # ----------------------------------------------------------------------
105
+ # مسارات تطبيق Flask (تبقى كما هي)
106
  # ----------------------------------------------------------------------
107
 
108
  @app.route('/')
109
  def index():
 
110
  return render_template('generator.html')
111
 
112
  @app.route('/generate', methods=['POST'])
 
114
  data = request.get_json()
115
  difficulty = data.get('difficulty', '50')
116
 
 
117
  password, score = generate_strong_password(difficulty)
118
 
 
119
  percentage = int((score / 4) * 100) if score > 0 else 0
120
 
 
121
  if score >= 3:
122
  status = "آمنة جداً (قوة: 4/4)" if score == 4 else "آمنة (قوة: 3/4)"
123
  color = "green"
 
126
  color = "orange"
127
  else:
128
  status = "ضعيفة جداً"
129
+ color = "red"
130
 
131
  return jsonify({
132
  'password': password,
 
137
  })
138
 
139
  # ----------------------------------------------------------------------
140
+ # 3. تشغيل التطبيق (يبقى كما هو)
141
  # ----------------------------------------------------------------------
142
 
143
  if __name__ == '__main__':
 
144
  app.run(host='0.0.0.0', port=7860, debug=True)