| 1 | using System.Security.Cryptography; |
| 2 | |
| 3 | namespace SimPas2_Windows.Managers |
| 4 | { |
| 5 | public class GeneratorManager |
| 6 | { |
| 7 | public GeneratorManager() |
| 8 | { |
| 9 | } |
| 10 | |
| 11 | public string GeneratePassword(bool isSecure, int length) |
| 12 | { |
| 13 | const string charset_risky = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 14 | const string charset_secure = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; |
| 15 | string charset = isSecure ? charset_secure : charset_risky; |
| 16 | |
| 17 | byte[] random = RandomNumberGenerator.GetBytes(length); |
| 18 | char[] res = new char[length]; |
| 19 | |
| 20 | for (int i = 0; i < length; i++) |
| 21 | { |
| 22 | res[i] = charset[random[i] % charset.Length]; |
| 23 | } |
| 24 | |
| 25 | return new string(res); |
| 26 | } |
| 27 | } |
| 28 | } |