🔄 Simple password generator class library in C# 6.0, use for generate your own password! :green_book:
Password Generator
class library.Password
class, because its imposible create instance of static class.Password
class library in your project shown below.
Created class library with name PasswordGenerator
(you must add the using PasswordGenerator;
)which contains one static class
which contains 1 staic field and 1 static method.
Method has 1 parameter which is the length of password.
In snippet shown the static class
with his members.
using System;
namespace PasswordGenerator
{
/// <summary>
/// This class library used for generate random passwords.
/// Used all digits, uppercase and downcase letters.
/// Can send any parameter to 'Generate' method, which will generate password with sended count length.
/// </summary>
public static class Password
{
private static string pass = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static string Generate(int length)
{
Random Rdpass = new Random();
string password = String.Empty;
for (int i = 0; i < length; i++)
{
password += pass[Rdpass.Next(0, pass.Length)];
}
return password;
}
}
}
In the solution have added new project where testing class library. The testing was completed in Console
application.
using System;
using PasswordGenerator; // add this 'using' to use class library.
namespace TestPasswordGenerator
{
class Program
{
static void Main()
{
//Parameter is lenght of your own password. Example 1.
string mypassword = Password.Generate(4);
Console.WriteLine(mypassword);
// Or second one example below. Example 2
Console.WriteLine(Password.Generate(12));
// Delay !!
Console.Read();
}
}
}
Added new non-static
class named by HardPassword
with new non-static
method Generate
.
There is a snippet of non-static
class shown below.
public class HardPassword
{
private string pass = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()-_=+|}{}:;',./";
public string Generate(int length)
{
if (length>pass.Length)
{
throw new ArgumentOutOfRangeException("No much characters");
}
Random rdpass = new Random();
string password = string.Empty;
for (int i = 0; i < length; i++)
{
password += pass[rdpass.Next(rdpass.Next(i, pass.Length))];
}
return password;
}
}
Updated too the test project shown by Console Application.
static void Main()
{
Console.WriteLine();
//Parameter is lenght of your own password. Example 1.
string mypassword = Password.Generate(45);
Console.WriteLine("\t First password with length 4 | {0}", mypassword);
// Or second one example below. Example 2
Console.WriteLine("\t Second password with length 12 | {0}", Password.Generate(12));
var mypass = new HardPassword();
Console.WriteLine("\t Instance created by non-static class | {0}" ,mypass.Generate(34));
// Delay !!
Console.Read();
}
The result in Console window shown in picture.
For more information contact.