Example #1
0
        static void Main(string[] args)
        {
            {
                var opts = new RandomPasswordOptions()
                {
                    AtLeastOneNumber    = true,
                    AtLeastOneSpecial   = false,
                    AtLeastOneUppercase = true,
                    Length     = 8,
                    AvoidChars = new[] { 'l', 'I', 'O', '0' }
                };
                var pass = RandomPassword(opts);
                System.Console.WriteLine($"pass [{pass}] in {opts.LoopCount} loop count");
            }

            {
                var opts = new RandomPasswordOptions()
                {
                    AllowLetter         = false,
                    AtLeastOneUppercase = false,
                    Length = 4
                };
                var pass = RandomPassword(opts);
                System.Console.WriteLine($"pin [{pass}] in {opts.LoopCount} loop count");
            }
        }
        /// <summary>
        /// Generates the password.
        /// </summary>
        /// <returns></returns>
        private static string GeneratePassword(int passwordLength,
                                               RandomPasswordOptions option)
        {
            if (passwordLength < 0)
            {
                return(null);
            }

            var passwordChars = GetCharacters(option);

            if (string.IsNullOrEmpty(passwordChars))
            {
                return(null);
            }

            var password = new char[passwordLength];

            var random = GetRandom();

            for (int i = 0; i < passwordLength; i++)
            {
                var index        = random.Next(passwordChars.Length);
                var passwordChar = passwordChars[index];

                password[i] = passwordChar;
            }

            return(new string(password));
        }
 /// <summary>
 /// Gets the characters selected by the option
 /// </summary>
 /// <returns></returns>
 private string GetCharacters(RandomPasswordOptions option)
 {
     switch (option) {
     case RandomPasswordOptions.Alpha:
         return PASSWORD_CHARS_Alpha;
     case RandomPasswordOptions.Numeric:
         return PASSWORD_CHARS_NUMERIC;
     case RandomPasswordOptions.AlphaNumeric:
         return PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC;
     case RandomPasswordOptions.AlphaNumericSpecial:
         return PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC +
                          PASSWORD_CHARS_SPECIAL;
     default:
         break;
     }
     return string.Empty;
 }
Example #4
0
        /// <SUMMARY>
        /// Gets the characters selected by the option
        /// </SUMMARY>
        /// <RETURNS></RETURNS>
        private static string GetCharacters(RandomPasswordOptions option)
        {
            switch (option)
            {
            case RandomPasswordOptions.Alpha:
                return(PASSWORD_CHARS_Alpha);

            case RandomPasswordOptions.Numeric:
                return(PASSWORD_CHARS_NUMERIC);

            case RandomPasswordOptions.AlphaNumeric:
                return(PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC);

            case RandomPasswordOptions.AlphaNumericSpecial:
                return(PASSWORD_CHARS_NUMERIC);   //PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC + PASSWORD_CHARS_SPECIAL;
            }
            return(string.Empty);
        }
        /// <SUMMARY>   
        /// Generates the password.   
        /// </SUMMARY>   
        /// <RETURNS></RETURNS>   
        private static string GeneratePassword(int passwordLength,
                                               RandomPasswordOptions option)
        {
            if (passwordLength < 0) return null;

            var passwordChars = GetCharacters(option);

            if (string.IsNullOrEmpty(passwordChars)) return null;

            var password = new char[passwordLength];

            var random = GetRandom();

            for (int i = 0; i < passwordLength; i++)
            {
                var index = random.Next(passwordChars.Length);
                var passwordChar = passwordChars[index];

                password[i] = passwordChar;
            }

            return new string(password);
        }
        /// <summary>
        /// Generates a random password.
        /// </summary>
        /// <returns>Randomly generated password.</returns>
        public string Generate(int passwordLength = DEFAULT_PASSWORD_LENGTH, 
                                      RandomPasswordOptions option = RandomPasswordOptions.AlphaNumericSpecial)
        {
            if (passwordLength < 0)
                return null;

            var passwordChars = GetCharacters (option);

            if (string.IsNullOrEmpty (passwordChars))
                return null;

            var password = new char[passwordLength];

            var random = GetRandom ();

            for (int i = 0; i < passwordLength; i++) {
                var index = random.Next (passwordChars.Length);
                var passwordChar = passwordChars [index];

                password [i] = passwordChar;
            }

            return new string (password);
        }
Example #7
0
 /// <SUMMARY>
 /// Generates a random password.
 /// </SUMMARY>
 /// <RETURNS>Randomly generated password.</RETURNS>
 public static string Generate(int passwordLength, RandomPasswordOptions option)
 {
     return(GeneratePassword(passwordLength, option));
 }
Example #8
0
        /// <summary>
        /// Generates a random password with the default length.
        /// </summary>
        /// <returns>Randomly generated password.</returns>
        public string Generate(RandomPasswordOptions option)
        {
            var rdn = new Random(DateTime.Now.Millisecond);

            return(Generate(rdn.Next(8, 99), option));
        }
 /// <summary>
 /// Generates a random password with the default length.
 /// </summary>
 /// <returns>Randomly generated password.</returns>
 public static string Generate(RandomPasswordOptions option)
 {
     return(Generate(DEFAULT_PASSWORD_LENGTH, option));
 }
Example #10
0
            /// <summary>
            /// Generate random password using defaults ( allow numbers, lowercase, uppercase, 12 of length, no special symbol )
            /// </summary>
            public static string RandomPassword(RandomPasswordOptions opts = null)
            {
                if (opts == null)
                {
                    opts = new RandomPasswordOptions();
                }

                var sb = new StringBuilder();

                var rnd = new Random();

                var loopCount = 0;

                {
                    var cntRequired = 0;
                    if (opts.AtLeastOneNumber)
                    {
                        ++cntRequired;
                    }
                    if (opts.AtLeastOneSpecial)
                    {
                        ++cntRequired;
                    }
                    if (opts.AtLeastOneUppercase)
                    {
                        ++cntRequired;
                    }
                    if (cntRequired > opts.Length)
                    {
                        throw new ArgumentException($"invalid length not enough for required at least {cntRequired} characters");
                    }
                    if (opts.Length == 0)
                    {
                        throw new ArgumentException($"invalid 0 length");
                    }
                }

                while (true)
                {
                    if (loopCount > 100)
                    {
                        throw new Exception($"unable to find suitable password in {loopCount} step");
                    }

                    ++opts.LoopCount;

                    sb.Clear();

                    var specialCount   = 0;
                    var numberCount    = 0;
                    var uppercaseCount = 0;

                    while (sb.Length < opts.Length)
                    {
                        var n = rnd.Next(48, 122);
                        var c = Convert.ToChar(n);

                        if (opts.AvoidChars != null && opts.AvoidChars.Any(a => c == a))
                        {
                            continue;
                        }

                        if (char.IsNumber(c) && (opts.AllowNumber || (opts.AtLeastOneNumber && numberCount == 0)))
                        {
                            sb.Append(c);
                            ++numberCount;
                        }
                        else if (char.IsLetter(c) && opts.AllowLetter)
                        {
                            if (char.IsLower(c) && opts.AllowLowercase)
                            {
                                sb.Append(c);
                            }
                            else if (opts.AllowUppercase || (opts.AtLeastOneUppercase && uppercaseCount == 0))
                            {
                                sb.Append(c);
                                ++uppercaseCount;
                            }
                        }
                        else if (specialCount < opts.MaxSpecial && (opts.AllowSpecial || (opts.AtLeastOneSpecial && specialCount == 0)))
                        {
                            sb.Append(c);
                            specialCount++;
                        }
                    }

                    if (opts.AtLeastOneNumber && numberCount == 0)
                    {
                        continue;
                    }
                    if (opts.AtLeastOneSpecial && specialCount == 0)
                    {
                        continue;
                    }
                    if (opts.AtLeastOneUppercase && uppercaseCount == 0)
                    {
                        continue;
                    }

                    break;
                }

                return(sb.ToString());
            }
 /// <SUMMARY>   
 /// Generates a random password.   
 /// </SUMMARY>   
 /// <RETURNS>Randomly generated password.</RETURNS>   
 public static string Generate(int passwordLength,
                               RandomPasswordOptions option)
 {
     return GeneratePassword(passwordLength, option);
 }
 /// <SUMMARY>   
 /// Generates a random password with the default length.   
 /// </SUMMARY>   
 /// <RETURNS>Randomly generated password.</RETURNS>   
 public static string Generate(RandomPasswordOptions option)
 {
     return Generate(DEFAULT_PASSWORD_LENGTH, option);
 }