Created
April 11, 2021 17:29
-
-
Save behnood-eghbali/f7560f0a0fed6f6b35b0f6c91eedf1b8 to your computer and use it in GitHub Desktop.
simple random password generator in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
public class PasswordGenerator | |
{ | |
public static void Main() | |
{ | |
int passwordLength = 15; | |
string password = GeneratePassword(passwordLength); | |
Console.WriteLine(password); | |
} | |
public static string GeneratePassword(int length) | |
{ | |
const string characters = "!@#$%^&*()<>?-_=+;:,.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var passwordBuilder = new StringBuilder(10, 20); | |
var randomNumber = new Random(); | |
for (int i = 0; i < length; i++) | |
{ | |
int index = randomNumber.Next(characters.Length); | |
passwordBuilder.Append(characters[index]); | |
} | |
return passwordBuilder.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment