tayaapi.blogg.se

Secure password generator
Secure password generator












secure password generator

secure password generator

Public static string RandomString(int length) public static Random random = new Random() Note the Random is a global static variable.

  • Take random characters from the concatenated array for the required length.
  • Random numerical characters for the required length divided by 2.
  • Random uppercase characters for the required length divided by 3.
  • Random lowercase characters for the required length divided by 3.
  • Random special characters for the required length divided by 4.
  • The following is pretty quick and works well.

    Secure password generator generator#

    Return new Random(seed).Next(minimumValue, maximumValue) Ĭonsume in your code thusly: var generator = new PasswordGenerator()

    secure password generator

    Generate uniformly distributed random integers within a given range. Public int Next(int minimumValue, int maximumValue) Do not use Next() % maximumValue because the distribution is not OK Var result = BitConverter.ToInt32(randomBuffer, 0) Private readonly RNGCryptoServiceProvider _rngProvider = new RNGCryptoServiceProvider() and high entropy or I will hunt you down with a pitchfork!! Only RNGCryptoServiceProvider() is safe. Never ever ever never use Random() in the generation of anything that requires true security/randomness Var shuffeldChars = source.ShuffleSecure().ToArray() Public static string ShuffleTextSecure(this string source) Int randomIndex = (counter, sourceArray.Length) Public static IEnumerable ShuffleSecure(this IEnumerable source)įor (int counter = 0 counter < sourceArray.Length counter++) New Lazy(() => new RandomSecureVersion()) Private string GetRandomString(string possibleChars, int lenght)įor (var position = 0 position RandomSecure = Var result = unshuffeledResult.ShuffleTextSecure() Shuffle the result so the order of the characters are unpredictable Var unshuffeledResult = minimumChars + rest Var rest = GetRandomString(_allAvailableChars, lengthOfPassword - minimumChars.Length) GetRandomString(AllSpecialChars, MinimumSpecialChars) GetRandomString(AllNumericChars, MinimumNumericChars) + GetRandomString(AllUpperCaseChars, MinimumUpperCaseChars) + Var minimumChars = GetRandomString(AllLowerCaseChars, MinimumLowerCaseChars) + add random charactes of all catagories Get the required number of characters of each catagory and Var lengthOfPassword = _randomSecure.Next(MinimumLengthPassword, MaximumLengthPassword) If (minimumLowerCaseChars 0 || _minimumNumberOfChars = 0 ? allChars : string.Empty Throw new ArgumentException("The minimumLength is bigger than the maximum length.", If (minimumLengthPassword maximumLengthPassword) Define characters that are valid and reject ambiguous characters such as ilo, IO and 1 or 0ĪllLowerCaseChars = GetCharRange('a', 'z', exclusiveChars: "ilo") ĪllUpperCaseChars = GetCharRange('A', 'Z', exclusiveChars: "IO") ĪllNumericChars = GetCharRange('2', '9') Private readonly RandomSecureVersion _randomSecure = new RandomSecureVersion() Private readonly string _allAvailableChars Not sure where I found this but here's a class to generate high entropy, truly random strings that can be used as passwords.














    Secure password generator