-
Notifications
You must be signed in to change notification settings - Fork 1
/
entropy.php
105 lines (94 loc) · 3.15 KB
/
entropy.php
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
<?php
function entropy($s) {
// Nested function to calculate the range
function calculate_range($s) {
$sol = 0;
$flags = ['lower' => false, 'upper' => false, 'number' => false, 'special' => false];
$length = strlen($s);
for ($i = 0; $i < $length; $i++) {
$char = $s[$i];
if (ctype_upper($char)) {
$flags['upper'] = true;
} elseif (ctype_lower($char)) {
$flags['lower'] = true;
} elseif (ctype_digit($char)) {
$flags['number'] = true;
} elseif (!ctype_alnum($char)) {
$flags['special'] = true;
}
}
if ($flags['upper'] && $flags['lower']) {
$sol += 52;
} else {
$sol += 26; // Simplified to add either upper or lower case
}
if ($flags['number']) {
$sol += 10;
}
if ($flags['special']) {
$sol += 32;
}
return $sol;
}
return strlen($s) * log(calculate_range($s), 2);
}
function generate_strong_password($length) {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $alphabet[random_int(0, strlen($alphabet) - 1)];
}
return $password;
}
function main($password) {
$entropy_est = entropy($password);
if ($entropy_est <= 30) {
return "Weak! Suggested Password: " . generate_strong_password(12);
} elseif ($entropy_est > 30 && $entropy_est <= 60) {
return "Medium! Suggested Password: " . generate_strong_password(12);
} else {
return "Strong";
}
}
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$password = $_POST['password'];
$message = main($password);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Strength Checker</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/entropy.css">
</head>
<body>
<div class="container mt-5">
<h1>Password Strength Checker</h1>
<div class="row">
<div class="col-md-6">
<h2>Check Your Password</h2>
<form method="POST" action="entropy.php">
<div class="form-group">
<label for="passwordInput">Enter Password:</label>
<input type="text" class="form-control" id="passwordInput" name="password" placeholder="Enter password" required>
</div>
<button type="submit" class="btn btn-primary">Check Password</button>
<button onclick="window.location.href='dashboard1.php';" class="btn btn-secondary">Back to Dashboard</button>
</form>
</div>
<div class="col-md-6">
<h2>Results</h2>
<p id="results">
<!-- Results will be displayed here after submitting the form. -->
<?php if (!empty($message)) { echo "<p class='alert alert-info'>$message</p>"; } ?>
</p>
</div>
</div>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>