Skip to content

Instantly share code, notes, and snippets.

@guibranco
Last active October 19, 2017 18:16
Show Gist options
  • Save guibranco/ae33e01e2e01ce4743954720fc988529 to your computer and use it in GitHub Desktop.
Save guibranco/ae33e01e2e01ce4743954720fc988529 to your computer and use it in GitHub Desktop.
Create a hash (aka password) of a fixed size, using one number, ony symbol, one upper letter and many lower letters
const String Letters = "abcdefghijklymnopqrstuwxyz"; //bota o resto ai
const String Numbers = "0123456789";
const String Symbols = "@$!?"; //põe mais se quiser
const Int32 Size = 10;
public static String CreatePassword()
{
var random = new Random();
var password = new StringBuilder(Size);
var upperUsed = false;
var numberUsed = false;
var symbolUsed = false;
for (var i = 0; i <= Size; i++)
{
var rand = random.Next(1, Size);
switch (rand)
{
case 1:
if (upperUsed)
break;
password.Append(Letters[random.Next(0, Letters.Length)].ToString().ToUpper());
upperUsed = true;
continue;
case 2:
if (numberUsed)
break;
password.Append(Numbers[random.Next(0, Numbers.Length)]);
numberUsed = true;
continue;
case 3:
if (symbolUsed)
break;
password.Append(Symbols[random.Next(0, Symbols.Length)]);
symbolUsed = true;
continue;
}
password.Append(Letters[random.Next(0, Letters.Length)]);
if (i < Size)
continue;
if (!symbolUsed || !numberUsed || !upperUsed) //não usou os caracteres obrigatórios, começa de novo
{
password.Clear();
i = 0;
}
}
return password.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment