-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpchecker.py
More file actions
134 lines (110 loc) · 3.53 KB
/
pchecker.py
File metadata and controls
134 lines (110 loc) · 3.53 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
# -- coding: utf-8 --
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from contextlib import closing
from flask import jsonify
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def is_spec_char(char):
return ord(char) not in range(ord('a'), ord('a') + 26) \
and ord(char) not in range(ord('A'), ord('A') + 26) \
and ord(char) not in range(ord('0'), ord('0') + 10)
def rating_function_alpha(username, password):
code = rating_function_code(username, password)
if code == 1:
return '1 - TooShortPassword'
elif code == 2:
return '2 - BadPassword'
elif code == 3:
return '3 - GoodPassword'
else:
return '4 - StrongPassword'
def rating_function_code(username, password):
score = rating_function_score(username, password)
if score == -100:
return 1
elif 0 < score <= 34:
return 2
elif 34 < score <= 68:
return 3
else:
return 4
def rating_function_score(username, password):
score = 0
if len(password) < 4:
#-100 es TooShortPassword
return -100
elif password == username:
#0 asegura TooShortPassword
return 0
score += len(password) * 4
checked = []
#Por cada char le resta al puntaje el número de veces que se repite
#más de una vez
for char in password:
if char not in checked:
score -= password.count(char) - 1
checked.append(char)
#SI hay 3 números, +5 ptos.
num_count = 0
added = False
for char in password:
try:
if int(char) in range(10):
num_count += 1
except ValueError:
pass
if num_count == 3 and not added:
score += 5
added = True
#Si hay 2 caracteres especiales, +5
specchar_count = 0
added = False
for char in password:
if is_spec_char(char):
specchar_count += 1
if specchar_count == 2 and not added:
score += 5
added = True
#Hay mayúsculas y minúsculas
if not password.lower() == password \
and not password.upper() == password:
score += 10
#Hay números y letras
if num_count > 0 and not num_count + specchar_count == len(password):
score += 15
#Hay caracteres especiales y números
if specchar_count > 0 and num_count > 0:
score += 15
#Hay caracteres normales y especiales
if specchar_count > 0 and not num_count + specchar_count == len(password):
score += 15
#Si sólo es caracteres
if password.isalpha():
score -= 10
#Si sólo es números
if password.isdigit():
score -= 10
if score > 100:
score = 100
return score
RATING_FUNCTIONS = {'alpha': rating_function_alpha,
'code': rating_function_code,
'score': rating_function_score}
@app.route('/')
def show_start():
return render_template('input_password.html')
@app.route('/rate', methods=['GET', 'POST'])
def rate_password():
score = (RATING_FUNCTIONS[request.form['mode']](
request.form['username'], request.form['password']),
rating_function_code(request.form['username'],
request.form['password']))
print request.form['mode']
return jsonify(score=score[0])
# return render_template('show_score.html', score=score)
if __name__ == '__main__':
app.run()