public void ShouldCheckIfEnoughNumbersAndUpper() { var options = new PasswordOptions { length = 8, UpperCase = 3, numbers = 4}; var password = GeneratePass(options); Assert.AreEqual(3, NumberOfUpperChars(password)); Assert.AreEqual(4, NumberOfDigits(password)); }
public void ShouldReturnTwoDifferentPasswordOfSameLenght() { var options = new PasswordOptions { length = 5 }; var firstPassword = GeneratePasswordUsingOptionsProvided(options); var secondPassowrd = GeneratePasswordUsingOptionsProvided(options); Assert.AreNotEqual(firstPassword, secondPassowrd); }
// Taken from: https://www.ryadel.com/en/c-sharp-random-password-generator-asp-net-core-mvc/ private string GenerateRandomPassword(PasswordOptions opts = null) { if (opts == null) { opts = new PasswordOptions() { RequireDigit = true, RequireLowercase = true, RequireNonAlphanumeric = true, RequireUppercase = true, RequiredLength = 8, RequiredUniqueChars = 1 } } ; string[] randomChars = new[] { "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "abcdefghijkmnopqrstuvwxyz", // lowercase "0123456789", // digits "!@$?_-" // non-alphanumeric }; Random rand = new Random(Environment.TickCount); List <char> chars = new List <char>(); if (opts.RequireUppercase) { chars.Insert(rand.Next(0, chars.Count), randomChars[0][rand.Next(0, randomChars[0].Length)]); } if (opts.RequireLowercase) { chars.Insert(rand.Next(0, chars.Count), randomChars[1][rand.Next(0, randomChars[1].Length)]); } if (opts.RequireDigit) { chars.Insert(rand.Next(0, chars.Count), randomChars[2][rand.Next(0, randomChars[2].Length)]); } if (opts.RequireNonAlphanumeric) { chars.Insert(rand.Next(0, chars.Count), randomChars[3][rand.Next(0, randomChars[3].Length)]); } for (int i = chars.Count; i < opts.RequiredLength || chars.Distinct().Count() < opts.RequiredUniqueChars; i++) { string rcs = randomChars[rand.Next(0, randomChars.Length)]; chars.Insert(rand.Next(0, chars.Count), rcs[rand.Next(0, rcs.Length)]); } return(new string(chars.ToArray())); } }
public string GeneratePassword(int rndNo, PasswordOptions options) { if (!options.includeSymbols && !options.includeNumbers && !options.includeLowercase && !options.includeUppercase) return ""; string ambiguous = "{}[]()/\\'\"`~,;:.<>"; string symbols = "!@#$%^&*-_+=|?" + ambiguous; string numbers = "1234567890"; string lowercase = "abcdefghijklmnopqrstuvwxyz"; string uppercase = lowercase.ToUpper(); string similar = "iIlL10oO"; string password = "", restrictedChars = ""; char nextChar = new char(); int minChar = 33, maxChar = 128; while (password.Length < rndNo) { nextChar = (char)random.Next(minChar, maxChar + 1); if (!options.includeSymbols) restrictedChars += symbols; if (!options.includeNumbers) restrictedChars += numbers; if (!options.includeLowercase) restrictedChars += lowercase; if (!options.includeUppercase) restrictedChars += uppercase; if (options.excludeSimilar && (options.includeNumbers || options.includeLowercase || options.includeUppercase)) restrictedChars += similar; if (options.excludeAmbiguous && options.includeSymbols) restrictedChars += ambiguous; if (restrictedChars.Length > 0 && IsNotRestricted(nextChar, restrictedChars)) password += nextChar; } return password; }
private void ConfigIdentity(IServiceCollection services, IConfiguration Configuration) { PasswordOptions pOpt = new PasswordOptions() { RequireDigit = false, RequiredLength = 6, RequireLowercase = false, RequireNonAlphanumeric = false, RequireUppercase = false }; services.AddIdentity <ApplicationUser, IdentityRole>(options => { options.Password = pOpt; }) .AddEntityFrameworkStores <SioDbContext>() .AddDefaultTokenProviders() .AddUserManager <UserManager <ApplicationUser> >() ; services.AddAuthorization(options => { options.AddPolicy("AddEditUser", policy => { policy.RequireClaim("Add User"); policy.RequireClaim("Edit User"); }); options.AddPolicy("DeleteUser", policy => policy.RequireClaim("Delete User")); }) ; }
public void ShouldReturnTwoRandomPasswordContainingSixRandomLeters() { var option = new PasswordOptions { length = 6, upperCaseCharacters = 3 }; var first = GeneratePassword(option); var second = GeneratePassword(option); Assert.AreNotEqual(first, second); }
public IdentityOptions() { ClaimsIdentity = new ClaimsIdentityOptions(); User = new UserOptions(); Password = new PasswordOptions(); Lockout = new LockoutOptions(); }
public void TestPasswordLength() { PasswordOptions p = new PasswordOptions(); p.length = 8; int expected = 8; int actual = GeneratePassword(p).Length; Assert.AreEqual(expected, actual); }
/// <summary> /// class RangealculateForThread public static int[][] GetStartingPoints (int threadCount, int passwdLength, PasswordOptions rangeOptions) /// </summary> public Generator(int passwdLength, PasswordOptions options, string md5, int threadCount) // StructureStringMd5 inputData { _options = options; _innerString = new char[passwdLength]; _passwdLength = passwdLength; _md5 = md5; _threadCount = threadCount; }
public static void ConfigurePasswordOptions(this PasswordOptions output, IPasswordConfiguration input) { output.RequiredLength = input.RequiredLength; output.RequireNonAlphanumeric = input.RequireNonLetterOrDigit; output.RequireDigit = input.RequireDigit; output.RequireLowercase = input.RequireLowercase; output.RequireUppercase = input.RequireUppercase; }
public static void ConfigureValidation(this PasswordOptions password) { password.RequiredLength = 8; password.RequireLowercase = false; password.RequireUppercase = false; password.RequireNonAlphanumeric = false; password.RequireDigit = false; }
private static void setPasswordOptions(PasswordOptions identityOptionsPassword, SiteSettings siteSettings) { identityOptionsPassword.RequireDigit = siteSettings.PasswordOptions.RequireDigit; identityOptionsPassword.RequireLowercase = siteSettings.PasswordOptions.RequireLowercase; identityOptionsPassword.RequireNonAlphanumeric = siteSettings.PasswordOptions.RequireNonAlphanumeric; identityOptionsPassword.RequireUppercase = siteSettings.PasswordOptions.RequireUppercase; identityOptionsPassword.RequiredLength = siteSettings.PasswordOptions.RequiredLength; }
public PasswordPolicyBuilder( IServiceProvider serviceProvider, PasswordOptions options ) { ServiceProvider = serviceProvider; Options = options; }
public void TestLowercaseWithUppercaseAndExcludeSimilar() { PasswordOptions options = new PasswordOptions(); options.includeLowercase = true; options.includeUppercase = true; options.excludeSimilar = true; Assert.AreEqual(6, GeneratePassword(6, options).Length); }
public IdentityOptions() { ClaimsIdentity = new ClaimsIdentityOptions(); User = new UserOptions(); Password = new PasswordOptions(); Lockout = new LockoutOptions(); SignIn = new SignInOptions(); }
// StructureStringMd5 inputData /// <summary> /// class RangealculateForThread public static int[][] GetStartingPoints (int threadCount, int passwdLength, PasswordOptions rangeOptions) /// </summary> public Generator(int passwdLength, PasswordOptions options, string md5, int threadCount) { _options = options; _innerString = new char[passwdLength]; _passwdLength = passwdLength; _md5 = md5; _threadCount = threadCount; }
/// <summary> /// Generates a random passphrase that is between 128 and 156 characters long. /// </summary> /// <returns></returns> public static string GeneratePassphrase(PasswordOptions opts = null) { if (opts == null) { opts = new PasswordOptions() { RequiredLength = RandomNumberGenerator.GetInt32(128, 157), RequiredUniqueChars = 10 } } ; string[] randomChars = new string[] { "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "abcdefghijkmnopqrstuvwxyz", // lowercase "0123456789", // digits "!@$?_-" // non-alphanumeric }; List <char> chars = new List <char>(); // Uppercase if (opts.RequireUppercase) { chars.Insert(RandomNumberGenerator.GetInt32(Math.Max(chars.Count, 1)), randomChars[0][RandomNumberGenerator.GetInt32(randomChars[0].Length)]); } // Lowercase if (opts.RequireLowercase) { chars.Insert(RandomNumberGenerator.GetInt32(Math.Max(chars.Count, 1)), randomChars[1][RandomNumberGenerator.GetInt32(randomChars[1].Length)]); } // Numerical if (opts.RequireDigit) { chars.Insert(RandomNumberGenerator.GetInt32(Math.Max(chars.Count, 1)), randomChars[2][RandomNumberGenerator.GetInt32(randomChars[2].Length)]); } // Symbols if (opts.RequireNonAlphanumeric) { chars.Insert(RandomNumberGenerator.GetInt32(Math.Max(chars.Count, 1)), randomChars[3][RandomNumberGenerator.GetInt32(randomChars[3].Length)]); } for (int i = chars.Count; i < opts.RequiredLength || chars.Distinct().Count() < opts.RequiredUniqueChars; i++) { string rcs = randomChars[RandomNumberGenerator.GetInt32(randomChars.Length)]; chars.Insert(RandomNumberGenerator.GetInt32(Math.Max(chars.Count, 1)), rcs[RandomNumberGenerator.GetInt32(rcs.Length)]); } return(new string(chars.ToArray())); } }
public string GeneratePass(PasswordOptions options) { string pass = ""; pass += GeneratePassWithSymbols(options.symbols, options.ambiguous); pass += GeneratePassWithinLimits(options.UpperCase, 'A', 'Z' + 1, options.similar); pass += GeneratePassWithinLimits(options.numbers, '0', '9' + 1, options.similar); pass += GeneratePassWithinLimits(options.length - pass.Length, 'a', 'z' + 1, options.similar); return ShuffleString(pass); }
public PasswordOptionsExtended(PasswordOptions inner) { RequireDigit = inner.RequireDigit; RequireLowercase = inner.RequireLowercase; RequireNonAlphanumeric = inner.RequireNonAlphanumeric; RequireUppercase = inner.RequireUppercase; RequiredLength = inner.RequiredLength; RequiredUniqueChars = inner.RequiredUniqueChars; }
public AuthenticationOptions( UserOptions userOptions, PasswordOptions passwordOptions, SignInOptions signInOptions) { this.User = userOptions; this.Password = passwordOptions; this.SignIn = signInOptions; }
public void OptionsGenerateMethodTest() { // Act var options = new PasswordOptions(16, true, true); var password = PasswordGenerator.Generate(options); // Assert Assert.Equal(options.Length, password.Length); }
public void TestPasswordWithDigits() { PasswordOptions p = new PasswordOptions(); p.length = 8; p.noOfDigits = 3; int expected = 3; int actual = CountSpecificCharacters(GeneratePassword(p), "0123456789"); Assert.AreEqual(expected, actual); }
private static PasswordRequirementsData?GetPasswordRequirements(PasswordOptions passwordOptions) => new PasswordRequirementsData() { RequiredLength = passwordOptions.RequiredLength, RequiredUniqueChars = passwordOptions.RequiredUniqueChars, RequireNonAlphanumeric = passwordOptions.RequireNonAlphanumeric, RequireLowercase = passwordOptions.RequireLowercase, RequireUppercase = passwordOptions.RequireUppercase, RequireDigit = passwordOptions.RequireDigit };
public static void SetDeveloperPasswordRequirements(this PasswordOptions options) { options.RequireDigit = false; options.RequireLowercase = false; options.RequireNonAlphanumeric = false; options.RequireUppercase = false; options.RequiredLength = 4; options.RequiredUniqueChars = 0; }
public void TestPasswordIsRandom() { PasswordOptions p = new PasswordOptions(); p.length = 8; string expected = GeneratePassword(p); Thread.Sleep(50); string actual = GeneratePassword(p); Assert.AreNotEqual(expected, actual); }
public static void SetDefaultRequirements(this PasswordOptions options) { options.RequireDigit = true; options.RequireLowercase = true; options.RequireNonAlphanumeric = true; options.RequireUppercase = true; options.RequiredLength = 6; options.RequiredUniqueChars = 1; }
public override PasswordOptions Policy(PasswordOptions options) { options.RequireDigit = true; options.RequiredLength = PinLength; options.RequireNonAlphanumeric = false; options.RequireUppercase = false; options.RequireLowercase = false; return(options); }
public async Task ValidateAsync_WhenPasswordOptionsAreNotExtendedPasswordOptions_ExpectSuccess() { var options = new PasswordOptions(); var sut = CreateMockedSut(); var result = await sut.Object.ValidateAsync(CreateMockUserManager(options).Object, new IdentityUser(), "123"); result.Succeeded.Should().BeTrue(); }
public static string GenerateRandomPassword(PasswordOptions options = null) { if (options == null) { options = new PasswordOptions() { RequiredLength = 8, RequiredUniqueChars = 4, RequireUppercase = true, RequireLowercase = true, RequireDigit = true, RequireNonAlphanumeric = true, } } ; var randomCharacters = new string[] { "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "abcdefghijkmnopqrstuvwxyz", // lowercase "0123456789", // digits "!@$?_-" }; Random random = new Random(Environment.TickCount); List <char> password = new List <char>(); if (options.RequireUppercase) { password.Insert(random.Next(0, password.Count), randomCharacters[0][random.Next(0, randomCharacters[0].Length)]); } if (options.RequireLowercase) { password.Insert(random.Next(0, password.Count), randomCharacters[1][random.Next(0, randomCharacters[1].Length)]); } if (options.RequireDigit) { password.Insert(random.Next(0, password.Count), randomCharacters[2][random.Next(0, randomCharacters[2].Length)]); } if (options.RequireNonAlphanumeric) { password.Insert(random.Next(0, password.Count), randomCharacters[3][random.Next(0, randomCharacters[3].Length)]); } //Iterate over the password to get exact required length for (int i = password.Count; i < options.RequiredLength || password.Distinct().Count() < options.RequiredUniqueChars; i++) { string randomString = randomCharacters[random.Next(0, randomCharacters.Length)]; password.Insert(random.Next(0, password.Count), randomString[random.Next(0, randomString.Length)]); } return(new string(password.ToArray())); } }
public override PasswordOptions Policy(PasswordOptions options) { options.RequireDigit = true; options.RequireLowercase = true; options.RequireNonAlphanumeric = true; options.RequireUppercase = true; options.RequiredLength = 6; options.RequiredUniqueChars = 1; return(options); }
public static string GeneratePassword(PasswordOptions Options) { string password = string.Empty; password += ReturnRandomSymbolsStringKnowingLength(Options.symbols, Options.ambiguous); password += GenerateRandomString(Options.numbers, '0', '9' + 1, Options.similar); password += GenerateRandomString(Options.upperCaseCharacters, 'A', 'Z' + 1, Options.similar); password += GenerateRandomString(Options.length - password.Length, 'a', 'z' + 1, Options.similar); password = ShuffleString(password); return password; }
/// <summary> /// Updates PasswordOptions for the Supplied Settings ID. /// </summary> /// <param name="settingsID">Settings ID for PasswordOptions.</param> /// <param name="passwordOptions">PasswordOptions Entity to be updated.</param> /// <returns>Number of Rows Affected.</returns> public int UpdatePasswordOptionsBySettingsID(int settingsID, PasswordOptions passwordOptions) { int AffectedRows = 0; using (SqlConnection connection = new SqlConnection(ConnectionString)) { using (SqlCommand command = new SqlCommand( @"Update PasswordOptions set AllowLowercaseCharacters = @AllowLowercaseCharacters, AllowUppercaseCharacters = @AllowUppercaseCharacters, AllowNumberCharacters = @AllowNumberCharacters, AllowSpecialCharacters = @AllowSpecialCharacters, AllowUnderscoreCharacters = @AllowUnderscoreCharacters, AllowSpaceCharacters = @AllowSpaceCharacters, AllowOtherCharacters = @AllowOtherCharacters, RequireLowercaseCharacters = @RequireLowercaseCharacters, RequireUppercaseCharacters = @RequireUppercaseCharacters, RequireNumberCharacters = @RequireNumberCharacters, RequireSpecialCharacters = @RequireSpecialCharacters, RequireUnderscoreCharacters = @RequireUnderscoreCharacters, RequireSpaceCharacters = @RequireSpaceCharacters, RequireOtherCharacters = @RequireOtherCharacters, MinimumCharacters = @MinimumCharacters, MaximumCharacters = @MaximumCharacters, OtherCharacters = @OtherCharacters where SettingsID = @SettingsID ", connection)) { command.Parameters.Add(new SqlParameter("SettingsID", settingsID)); command.Parameters.Add(new SqlParameter("AllowLowercaseCharacters", passwordOptions.AllowLowercaseCharacters)); command.Parameters.Add(new SqlParameter("AllowUppercaseCharacters", passwordOptions.AllowUppercaseCharacters)); command.Parameters.Add(new SqlParameter("AllowNumberCharacters", passwordOptions.AllowNumberCharacters)); command.Parameters.Add(new SqlParameter("AllowSpecialCharacters", passwordOptions.AllowSpecialCharacters)); command.Parameters.Add(new SqlParameter("AllowUnderscoreCharacters", passwordOptions.AllowUnderscoreCharacters)); command.Parameters.Add(new SqlParameter("AllowSpaceCharacters", passwordOptions.AllowSpaceCharacters)); command.Parameters.Add(new SqlParameter("AllowOtherCharacters", passwordOptions.AllowOtherCharacters)); command.Parameters.Add(new SqlParameter("RequireLowercaseCharacters", passwordOptions.RequireLowercaseCharacters)); command.Parameters.Add(new SqlParameter("RequireUppercaseCharacters", passwordOptions.RequireUppercaseCharacters)); command.Parameters.Add(new SqlParameter("RequireNumberCharacters", passwordOptions.RequireNumberCharacters)); command.Parameters.Add(new SqlParameter("RequireSpecialCharacters", passwordOptions.RequireSpecialCharacters)); command.Parameters.Add(new SqlParameter("RequireUnderscoreCharacters", passwordOptions.RequireUnderscoreCharacters)); command.Parameters.Add(new SqlParameter("RequireSpaceCharacters", passwordOptions.RequireSpaceCharacters)); command.Parameters.Add(new SqlParameter("RequireOtherCharacters", passwordOptions.RequireOtherCharacters)); command.Parameters.Add(new SqlParameter("MinimumCharacters", passwordOptions.MinimumCharacters)); command.Parameters.Add(new SqlParameter("MaximumCharacters", passwordOptions.MaximumCharacters)); command.Parameters.Add(new SqlParameter("OtherCharacters", passwordOptions.OtherCharacters)); connection.Open(); AffectedRows = command.ExecuteNonQuery(); } } return(AffectedRows); }
public static string GeneratePassword() { var options = new PasswordOptions(); bool nonAlphanumeric = options.RequireNonAlphanumeric; bool digit = options.RequireDigit; bool lowercase = options.RequireLowercase; bool uppercase = options.RequireUppercase; StringBuilder password = new StringBuilder(); Random random = new Random(); while (password.Length < options.RequiredLength) { char c = (char)random.Next(32, 126); password.Append(c); if (char.IsDigit(c)) { digit = false; } else if (char.IsLower(c)) { lowercase = false; } else if (char.IsUpper(c)) { uppercase = false; } else if (!char.IsLetterOrDigit(c)) { nonAlphanumeric = false; } } if (nonAlphanumeric) { password.Append((char)random.Next(33, 48)); } if (digit) { password.Append((char)random.Next(48, 58)); } if (lowercase) { password.Append((char)random.Next(97, 123)); } if (uppercase) { password.Append((char)random.Next(65, 91)); } return(password.ToString()); }
public void ProcessIdentityPasswordRules_WhenRequireUppercaseIsTrue_ExpectRequiredUpperAttribute() { var sut = CreateSut(); var options = new PasswordOptions { RequireUppercase = true }; sut.ProcessIdentityPasswordRules(options, testOutput); testOutput.Attributes["passwordrules"].Value.As <string>().Should().Contain("required: upper;"); }
public void TestAllChecked() { PasswordOptions options = new PasswordOptions(); options.includeSymbols = true; options.includeNumbers = true; options.includeLowercase = true; options.includeUppercase = true; options.excludeSimilar = true; options.excludeAmbiguous = true; Assert.AreEqual(50, GeneratePassword(50, options).Length); }
public static string GeneratePassword() { var PasswordOptionsConfig = ConfigurationManager.GetSection("PasswordOptions") as PasswordOptions; if (PasswordOptionsConfig == null) { PasswordOptionsConfig = new PasswordOptions() { RequiredLength = 8, RequiredUniqueChars = 4, RequireDigit = true, RequireLowercase = true, RequireNonAlphanumeric = true, RequireUppercase = true } } ; string[] randomChars = new[] { "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "abcdefghijkmnopqrstuvwxyz", // lowercase "@!#$%&" // non-alphanumeric }; Random rand = new Random(Environment.TickCount); List <string> chars = new List <string>(); if (PasswordOptionsConfig.RequireUppercase) { chars.Insert(0, randomChars[0][rand.Next(0, randomChars[0].Length)].ToString()); } if (PasswordOptionsConfig.RequireLowercase) { chars.Insert(1, randomChars[1][rand.Next(0, randomChars[1].Length)].ToString()); } if (PasswordOptionsConfig.RequireDigit) { string number = rand.Next(0, 100000).ToString("D5"); chars.Insert(2, number); } if (PasswordOptionsConfig.RequireNonAlphanumeric) { chars.Insert(3, randomChars[2][rand.Next(0, randomChars[2].Length)].ToString()); } return(String.Join(String.Empty, chars)); } }
public static string PasswordRequirementMessage(string password, PasswordOptions opts) { return(PasswordRequirementMessage( password, opts.RequiredLength, opts.RequiredUniqueChars, opts.RequireNonAlphanumeric, opts.RequireLowercase, opts.RequireUppercase, opts.RequireDigit)); }
public void ProcessIdentityPasswordRules_WhenRequireNonAlphanumericIsFalse_ExpectNoRequiredCharactersAttribute() { var sut = CreateSut(); var options = new PasswordOptions { RequireNonAlphanumeric = false }; sut.ProcessIdentityPasswordRules(options, testOutput); testOutput.Attributes["passwordrules"].Value.As <string>().Should().NotContain("required: ["); }
public PasswordGenerator(PasswordOptions passwordOptions) { _passwordOptions = passwordOptions ?? new PasswordOptions { RequiredLength = 8, RequireDigit = true, RequireLowercase = true, RequireNonAlphanumeric = true, RequireUppercase = true }; }
public void ShouldCheckIfNrOfSymbolsCorrect() { var options = new PasswordOptions { length = 8, UpperCase = 3, numbers = 2, symbols = 1 }; var tempPass = GeneratePass(options); int counter = 0; foreach (char c in tempPass) { if (!char.IsUpper(c) && !char.IsNumber(c) && !char.IsLower(c)) counter++; } Assert.AreEqual(1, counter); }
public void ProcessIdentityPasswordRules_WhenRequireDigitIsFalse_ExpectNoRequiredDigitAttribute() { var sut = CreateSut(); var options = new PasswordOptions { RequireDigit = false }; sut.ProcessIdentityPasswordRules(options, testOutput); testOutput.Attributes["passwordrules"].Value.As <string>().Should().NotContain("required: digit;"); }
/// ========================================================================================================================= /// Sample password policy implementation following the Microsoft.AspNetCore.Identity.PasswordOptions standard. /// ========================================================================================================================= public static bool IsValidPassword(string password, PasswordOptions opts) { return(IsValidPassword( password, opts.RequiredLength, opts.RequiredUniqueChars, opts.RequireNonAlphanumeric, opts.RequireLowercase, opts.RequireUppercase, opts.RequireDigit)); }
public void ShouldCheckHowManyNumbersAreInThePasswordIfNoUpperCase() { var option = new PasswordOptions { length = 6, upperCaseCharacters = 0, numbers = 2 }; string password = GeneratePassword(option); int result = 0; foreach (var item in password) { if (char.IsNumber(item)) result++; } Assert.AreEqual(2, result); }
public void ShouldCheckHowManySymbolsAreInThePassword() { var option = new PasswordOptions { length = 8, upperCaseCharacters = 3, numbers = 2, symbols =2 }; string password = GeneratePassword(option); int result = 0; foreach (var item in password) { if (!char.IsNumber(item) && !char.IsUpper(item) && !char.IsLower(item)) result++; } Assert.AreEqual(2, result); }
private static PasswordOptions GetPasswordOptions() { PasswordOptions passwordOptions = new PasswordOptions(); passwordOptions.RequireDigit = false; passwordOptions.RequiredLength = 1; passwordOptions.RequireNonAlphanumeric = false; passwordOptions.RequireUppercase = false; passwordOptions.RequireLowercase = false; passwordOptions.RequiredUniqueChars = 1; return(passwordOptions); }
public PasswordGenerator() { _passwordOptions = new PasswordOptions { RequireDigit = false, RequiredLength = 12, RequiredUniqueChars = 4, RequireLowercase = false, RequireNonAlphanumeric = false, RequireUppercase = false }; }
private static string GetPasswordChars(PasswordOptions passwordOptions) { var passwordChars = String.Empty; foreach (var pOption in passwordOptions) { passwordChars += pOption.Characters; } if (string.IsNullOrEmpty(passwordChars)) return null; return passwordChars; }
private static PasswordOptions GetCharactersToIncludeInPassword(CharacterTypes option) { var list = new PasswordOptions(); switch (option) { case CharacterTypes.Alpha_Lower: list.Add(AlphaLC); break; case CharacterTypes.Alpha_Upper: list.Add(AlphaUC); break; case CharacterTypes.Digit: list.Add(Digits); break; case CharacterTypes.AlphaLowerNumeric: list.Add(AlphaLC); list.Add(Digits); break; case CharacterTypes.AlphaUpperNumeric: list.Add(AlphaUC); list.Add(Digits); break; case CharacterTypes.AlphaNumeric: list.Add(AlphaLC); list.Add(AlphaUC); list.Add(Digits); break; case CharacterTypes.Special: list.Add(Specials); break; case CharacterTypes.AlphaNumericSpecial: list.Add(AlphaLC); list.Add(AlphaUC); list.Add(Digits); list.Add(Specials); break; default: list.Add(AlphaLC); list.Add(AlphaUC); break; } return list; }
private void btnFind_Click(object sender, EventArgs e) { if (IsPasswordValid() && IsThreadValid() && IsCheckedRange()) { btnFind.Enabled = false; string password = this.tbPassword.Text; string md5hash = CalculateMd5.CalculateMd5Hash(password); this.tbMd5.Text = md5hash; PasswordOptions options=new PasswordOptions(); if (cbRangAZ.Checked) options=options|PasswordOptions.Capital; if (cbRangeaz.Checked) options=options|PasswordOptions.Lower; if (cbRangeNumber.Checked) options=options|PasswordOptions.Numbers; int threadCount = (int) this.numThread.Value; Action<string> GetPassword; GetPassword = ReceivePassword; //Generator generator = new Generator(3, PasswordOptions.Capital, CalculateMd5.CalculateMd5Hash("ABC"), 3); Generator generator = new Generator(password.Length, options, md5hash, threadCount); _tokenSource=generator.ThreadCracker(GetPassword); } else MessageBox.Show("Input data don't correct", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); }
public static string GeneratePassword(PasswordOptions p) { string chars = "abcdefghijklmnopqrstuvwxyz"; string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string digits = "0123456789"; string symbols = "~`!@#$%^&*()_+{[}]|:;'<>?/"; if (p.removeSimilarCharacters) { chars = "abcdefghijkmnpqrstuvwxyz"; upperChars = "ABCDEFGHJKMNPQRSTUVWXYZ"; digits = "23456789"; } if (p.removeAmbiguousCharacters) { symbols = "`!@#$%^&*_+|:?"; } var stringChars = new char[p.length]; var random = new Random(); int[] usedUpperIndexes = new int[p.noOfUpper]; int[] usedDigitIndexes = new int[p.noOfDigits]; int[] usedSymbolIndexes = new int[p.noOfSymbols]; int index = 0; for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } AddOtherCharacters(ref p, upperChars, stringChars, random, usedUpperIndexes, index, p.noOfUpper, usedUpperIndexes); AddOtherCharacters(ref p, digits, stringChars, random, usedDigitIndexes, index, p.noOfDigits, usedUpperIndexes); AddOtherCharacters(ref p, symbols, stringChars, random, usedSymbolIndexes, index, p.noOfSymbols, usedUpperIndexes.Concat(usedDigitIndexes).ToArray()); var finalString = new String(stringChars); return finalString; }
public void TestPasswordWithSymbols() { PasswordOptions p = new PasswordOptions(); p.length = 8; p.noOfSymbols = 2; int expected = 2; int actual = CountSpecificCharacters(GeneratePassword(p), "~`!@#$%^&*()_+{[}]|:;'<>?/"); Assert.AreEqual(expected, actual); }
private static char[] CharForThread(int[] deltaChar, PasswordOptions rangeOptions) { char[] charForThread = new char[deltaChar.Length]; bool capitals = (rangeOptions & PasswordOptions.Capital) == PasswordOptions.Capital; bool lower = (rangeOptions & PasswordOptions.Lower) == PasswordOptions.Lower; bool numbers = (rangeOptions & PasswordOptions.Numbers) == PasswordOptions.Numbers; List<Tuple<int, int>> ranges = new List<Tuple<int, int>>(); if (numbers) ranges.Add(new Tuple<int, int>(48, 58)); if (capitals) ranges.Add(new Tuple<int, int>(65, 91)); if (lower) ranges.Add(new Tuple<int, int>(97, 123)); for (int i = 0; i < deltaChar.Length; i++) { int currentChar = deltaChar[i]; charForThread[i] = ConvertToChar(ranges, currentChar); } return charForThread; }
private static void AddOtherCharacters(ref PasswordOptions p, string typeOfCharacters, char[] stringChars, Random random, int[] usedCharacters, int index, int noOfCharacters, int [] previouslyUsed) { while (noOfCharacters > 0) { index = random.Next(p.length); if (Array.IndexOf(usedCharacters, index) == -1 && Array.IndexOf(previouslyUsed, index)== -1) { stringChars[index] = typeOfCharacters[random.Next(typeOfCharacters.Length)]; usedCharacters[noOfCharacters - 1] = index; noOfCharacters--; } } }
string GeneratePasswordUsingOptionsProvided(PasswordOptions options) { string password = ""; password += GenerateAStringOfCharacters(options.upperCaseCharacters, options.similar, 'A', 'Z'+1); password += GenerateAStringOfCharacters(options.numbers, options.similar, '0', '9'+1); password += GenerateAStringOfSymbols(options.symbols, options.ambiguous); password += GenerateAStringOfCharacters(options.length - password.Length, options.similar, 'a', 'z'+1); return SwitchCharacters(password, 5); }
public void TestPasswordWithMixedCharacters() { PasswordOptions p = new PasswordOptions(); p.length = 8; p.noOfSymbols = 2; p.noOfDigits = 3; p.noOfUpper = 1; int expected = 6; int actual = CountSpecificCharacters(GeneratePassword(p), "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()_+{[}]|:;'<>?/"); Assert.AreEqual(expected, actual); }
public void ShouldReturnTheNumberOfSymbols() { var options = new PasswordOptions { length = 7, symbols = 2 }; var password = GeneratePasswordUsingOptionsProvided(options); Assert.AreEqual(2, CountNumberOfSymbolsInString(password)); }
public void ShouldReturnTheNumberOfUppercaseLetters() { var options = new PasswordOptions { length = 7, upperCaseCharacters = 2 }; var password = GeneratePasswordUsingOptionsProvided(options); Assert.AreEqual(2, CountNumberOfUppercaseLetterssInString(password)); }
public void TestPasswordWithUpperChars() { PasswordOptions p = new PasswordOptions(); p.length = 8; p.noOfUpper = 5; int expected = 5; int actual = CountSpecificCharacters(GeneratePassword(p), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); Assert.AreEqual(expected, actual); }
public void ShouldReturnARandomPasswordContainingSixRandomLeters() { var option = new PasswordOptions { length = 6, upperCaseCharacters = 3 }; Assert.AreEqual(6,GeneratePassword(option).Length); }
public void ShouldScheckUpperCaseRandomLetersNumber() { var option = new PasswordOptions { length = 6, upperCaseCharacters = 3 }; string password = GeneratePassword(option); int result = 0; foreach (var item in password) { if (char.IsUpper(item)) result++; } Assert.AreEqual(3,result); }