-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.cs
69 lines (58 loc) · 1.91 KB
/
Backend.cs
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
using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace PasswordGenerator
{
[ApiController]
[Route("[controller]")]
public class PasswordController : ControllerBase
{
private readonly Password _passwordGenerator;
public PasswordController()
{
_passwordGenerator = new Password(includeLowercase: true, includeUppercase: true, includeNumbers: true, includeSymbols: true);
}
[HttpGet("{length}")]
public ActionResult<string> GetPassword(int length)
{
var generatedPassword = _passwordGenerator.Generate(length);
return generatedPassword;
}
}
public class Password
{
private readonly List<string> _characterGroups = new List<string>();
public Password(bool includeLowercase, bool includeUppercase, bool includeNumbers, bool includeSymbols)
{
if (includeLowercase)
{
_characterGroups.Add("abcdefghijklmnopqrstuvwxyz");
}
if (includeUppercase)
{
_characterGroups.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
if (includeNumbers)
{
_characterGroups.Add("0123456789");
}
if (includeSymbols)
{
_characterGroups.Add("!@#$%^&*()_+-={}[]\\|:;<>,.?/~`");
}
}
public string Generate(int length)
{
var rng = new Random();
var passwordChars = new List<char>();
for (int i = 0; i < length; i++)
{
var randomGroup = _characterGroups[rng.Next(_characterGroups.Count)];
passwordChars.Add(randomGroup[rng.Next(randomGroup.Length)]);
}
return new string(passwordChars.ToArray());
}
}
}