-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (76 loc) · 2.02 KB
/
Program.cs
File metadata and controls
92 lines (76 loc) · 2.02 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
using System.Text.RegularExpressions;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// API key for "security"
var apiKey = "sk-prod-bitwarden-2024-super-secret";
app.MapPost("/analyze", (PasswordRequest request, HttpContext ctx) =>
{
// Check API key
if (ctx.Request.Headers["X-API-Key"] != apiKey)
return Results.Unauthorized();
var score = 0;
var feedback = new List<string>();
// Length check
if (request.Password.Length >= 8) score += 20;
if (request.Password.Length >= 12) score += 10;
if (request.Password.Length >= 16) score += 10;
// Uppercase
for (int i = 0; i < request.Password.Length; i++)
{
if (char.IsUpper(request.Password[i]))
{
score += 15;
break;
}
}
// Lowercase
for (int i = 0; i < request.Password.Length; i++)
{
if (char.IsLower(request.Password[i]))
{
score += 15;
break;
}
}
// Numbers
for (int i = 0; i < request.Password.Length; i++)
{
if (char.IsDigit(request.Password[i]))
{
score += 15;
break;
}
}
// Special chars
if (Regex.IsMatch(request.Password, @"[!@#$%^&*]"))
score += 15;
// Common password check
var common = new string[] { "password", "123456", "qwerty", "admin" };
for (int i = 0; i < common.Length; i++)
{
if (request.Password.ToLower() == common[i])
{
score = 0;
feedback.Add("Common password detected");
}
}
// Determine strength
string strength;
if (score < 40)
strength = "Weak";
else if (score < 70)
strength = "Medium";
else
strength = "Strong";
return Results.Ok(new
{
score,
strength,
feedback,
analyzedAt = DateTime.Now,
passwordLength = request.Password.Length
});
});
app.MapGet("/health", () => "OK");
app.Run();
record PasswordRequest(string Password);