예제 #1
0
        public void PasswordPolicy_IsValid_succeeds()
        {
            var policy = new PasswordPolicy();

            Assert.IsTrue(policy.IsValid("geoR;ge123bbbbb"));
            Assert.IsTrue(policy.IsValid("Fred234()_"));
            Assert.IsTrue(policy.IsValid("Goingonahike123!@#"));
        }
예제 #2
0
        public void CanMatchMaxNoOfAllowedCharacterRepetitions()
        {
            // Arrange
            PasswordPolicy policy = new PasswordPolicy
            {
                MaxNoOfAllowedCharacterRepetitions = 2
            };

            // Act + Assert
            Assert.IsTrue(policy.IsValid("aabb"));
            Assert.IsFalse(policy.IsValid("xaaay"));
        }
    public void CanMatchPasswordLength()
    {
      // Arrange
      PasswordPolicy policy = new PasswordPolicy
      {
        MinPasswordLength = 5
      };

      // Act + Assert
      Assert.IsTrue(policy.IsValid("12345"));
      Assert.IsFalse(policy.IsValid("1234"));
    }
예제 #4
0
        public void CanMatchMinNoOfNumbers()
        {
            // Arrange
            PasswordPolicy policy = new PasswordPolicy
            {
                MinNoOfNumbers = 3
            };

            // Act + Assert
            Assert.IsTrue(policy.IsValid("a12b3"));
            Assert.IsFalse(policy.IsValid("a1xb3"));
        }
예제 #5
0
        public void CanMatchPasswordLength()
        {
            // Arrange
            PasswordPolicy policy = new PasswordPolicy
            {
                MinPasswordLength = 5
            };

            // Act + Assert
            Assert.IsTrue(policy.IsValid("12345"));
            Assert.IsFalse(policy.IsValid("1234"));
        }
    public void CanMatchMinNoOfNumbers()
    {
      // Arrange
      PasswordPolicy policy = new PasswordPolicy
      {
        MinNoOfNumbers = 3
      };

      // Act + Assert
      Assert.IsTrue(policy.IsValid("a12b3"));
      Assert.IsFalse(policy.IsValid("a1xb3"));
    }
    public void CanMatchMinNoOfMinNoOfLowerCaseChars()
    {
      // Arrange
      PasswordPolicy policy = new PasswordPolicy
      {
        MinNoOfLowerCaseChars = 3
      };

      // Act + Assert
      Assert.IsTrue(policy.IsValid("1a2bc"));
      Assert.IsFalse(policy.IsValid("1A2bC"));
      Assert.IsFalse(policy.IsValid("1A2BC"));
    }
예제 #8
0
        public void CanMatchMinNoOfMinNoOfLowerCaseChars()
        {
            // Arrange
            PasswordPolicy policy = new PasswordPolicy
            {
                MinNoOfLowerCaseChars = 3
            };

            // Act + Assert
            Assert.IsTrue(policy.IsValid("1a2bc"));
            Assert.IsFalse(policy.IsValid("1A2bC"));
            Assert.IsFalse(policy.IsValid("1A2BC"));
        }
예제 #9
0
        public void PasswordPolicy_IsValid_fails_invalidchars()
        {
            var policy = new PasswordPolicy();

            policy.InvalidChars.Add('@');

            Assert.IsFalse(policy.IsValid("geoR;ge12@3bbbbb"));
        }
예제 #10
0
        public void TestParseTwo()
        {
            var parsedLine = new PasswordPolicy("1-3 a: abcde");

            Assert.Equal(1, parsedLine.Minimum);
            Assert.Equal(3, parsedLine.Maximum);
            Assert.Equal('a', parsedLine.Character);
            Assert.Equal("abcde", parsedLine.Password);
            Assert.True(parsedLine.IsValid());
        }
예제 #11
0
        public void TestParse()
        {
            var parsedLine = new PasswordPolicy(Input[0]);

            Assert.Equal(3, parsedLine.Minimum);
            Assert.Equal(11, parsedLine.Maximum);
            Assert.Equal('z', parsedLine.Character);
            Assert.Equal("zzzzzdzzzzlzz", parsedLine.Password);
            Assert.True(parsedLine.IsValid());
        }
예제 #12
0
        static void Main(string[] args)
        {
            IPasswordPolicy password = new PasswordPolicy(new NewPolicy());

            for (int i = 0; i < 10; i++)
            {
                var pswd = password.Generate();
                Console.WriteLine(password.ToString());
                Console.WriteLine(pswd.GetPassword());
                Console.WriteLine(pswd.SecurePassword);

                pswd.SavePassword(x =>
                {
                    Console.WriteLine("Password save method :: " + x);
                });

                Console.WriteLine("Password strength :: " + pswd.GetPasswordStrength());
                Console.WriteLine("Password shuffle text :: " + pswd.ShuffleText());
                Console.WriteLine("Password Validate as per policy :: " + password.IsValid(pswd.SecurePassword));
            }

            var userNameClass = new UserNameClass();

            userNameClass = password.AutoSetPassword <UserNameClass>(userNameClass, x => x.Password);

            List <UserNameClass> userNames = new List <UserNameClass>();

            userNames.Add(new UserNameClass
            {
                UserName = "******"
            });

            userNames.Add(new UserNameClass
            {
                UserName = "******"
            });

            var userPassword = password.AutoSetPassword <UserNameClass>(userNames, x => x.Password);

            var userNameClasss = password.RenderPassword <UserNameClass>(userNames, (u, s) =>
            {
                u.Password = s.SecurePassword;
            });

            string uniq = password.RenderUniquePassword(x =>
            {
                Console.WriteLine(x);
                return(true);
            }, 2);

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
예제 #13
0
        private void CreateNUButton_Click(object sender, EventArgs e)
        {
            //New User Form Parameters = No Boxes Blank, PW and ConfirmPW must be same, PW requirements
            //Validate all Text Box entries and New User create in SQL Database, return to LogIn Page

            var  username       = usernameBox.Text;
            var  password       = passwordBox.Text;
            var  confirmPw      = confirmPwBox.Text;
            var  firstname      = firstNameBox.Text;
            var  lastname       = lastnameBox.Text;
            var  email          = emailBox.Text;
            var  address        = addressBox.Text;
            var  city           = cityBox.Text;
            var  state          = stateBox.Text;
            var  zipcode        = zipCodeBox.Text;
            bool passwordPolicy = PasswordPolicy.IsValid(password);


            if (username == "" || password == "" || confirmPw == "" || firstname == "" || lastname == "" || email == "" || address == "" ||
                city == "" || state == "" || zipcode == "")
            {
                MessageBox.Show("Empty Fields Detected! Please fill up all the fields");
                return;
            }

            if (password != confirmPw)
            {
                MessageBox.Show("Password and Confirm does not match");
                return;
            }

            if (passwordPolicy == false)
            {
                MessageBox.Show("Password must be at least 7 characters, must include at least one upper " +
                                "case letter, one lower case letter, one special character " +
                                "and one numeric digit.");
                return;
            }

            bool r = validate_NewUser(username, password, firstname, lastname, email, address, city, state, zipcode);

            if (r)
            {
                MessageBox.Show("New User Created!");
                var logInPage = new LogIn();
                Hide();
                logInPage.Show();
            }
            else
            {
                MessageBox.Show("Fail");
            }
        }
        public override bool IsValid(object value)
        {
            string pwd = value as string;

            if (pwd == null)
            {
                return(false);
            }

            PasswordPolicy policy = Configuration.Settings.GetPasswordPolicy();

            return(policy.IsValid(pwd));
        }
        public async Task PasswordPolicy_Invalidates_Correct_Length_But_Valid_Caps_And_Invalid_Numeric()
        {
            // Arrange
            var passwordPolicy = new PasswordPolicy();

            // Act
            var result = await passwordPolicy.IsValid(new PasswordModel { Password = "******" });

            var policyViolations = passwordPolicy.PolicyViolations.ToList();

            // Assert
            Assert.False(result);
            Assert.Equal(1, policyViolations.Count);
            Assert.Equal("PasswordComplexity", policyViolations.First().Key);
        }
        public async Task PasswordPolicy_Validates_Correct_Password()
        {
            // Arrange
            var passwordPolicy = new PasswordPolicy();

            // Act
            var result = await passwordPolicy.IsValid(new PasswordModel()
            {
                Password = "******"
            });

            var policyViolations = passwordPolicy.PolicyViolations.ToList();

            // Assert
            Assert.True(result);
            Assert.Equal(0, policyViolations.Count);
        }
예제 #17
0
        private void SetPassword(string newPassword, PasswordPolicy passwordPolicy)
        {
            if (newPassword != null)
            {
                if (!passwordPolicy.IsValid(newPassword))
                {
                    throw new InvalidPasswordException(passwordPolicy.GetDescription(_.Auth.Password));
                }

                string algorithm = Configuration.Settings.PasswordHashAlgorithm;
                byte[] salt, hash;
                GeneratePasswordHash(newPassword, algorithm, out salt, out hash);

                Publish(new PasswordChangedEvent(Id, salt, hash, algorithm));
            }
            else
            {
                Publish(new PasswordChangedEvent(Id, null, null, null));
            }
        }
예제 #18
0
        private void ResetPWButton_Click(object sender, EventArgs e)
        {
            var  password       = newPWBox.Text;
            var  confirmPw      = confirmPWBox.Text;
            var  username       = usernameBox.Text;
            bool passwordPolicy = PasswordPolicy.IsValid(password);

            if (password == "" || confirmPw == "" || username == "")
            {
                MessageBox.Show("Empty Fields Detected! Please fill up all the fields");
                return;
            }

            if (password != confirmPw)
            {
                MessageBox.Show("Password and Confirm does not match");
                return;
            }

            if (passwordPolicy == false)
            {
                MessageBox.Show("Password must be at least 7 characters, must include at least one upper " +
                                "case letter, one lower case letter, one special character " +
                                "and one numeric digit.");
                return;
            }
            bool r = validate_passwordReset(username, password);

            if (r)
            {
                MessageBox.Show("New Password Created!");
                var logInPage = new LogIn();
                Hide();
                logInPage.Show();
            }
            else
            {
                MessageBox.Show("Fail");
            }
        }
예제 #19
0
        protected override string SolvePartOne()
        {
            /*
             * list of passwords and the policy when that password was set
             *
             * 1-3 a: abcde // valid: contains 2 'a's
             * 1-3 b: cdefg // not valid: needs at least 1 'b'
             * 2-9 c: ccccccccc // valid: contains exactly 9 'c's
             */

            // format: "[int]-[int] [char]: [string]"

            // format notes: only one letter per policy, numbers can be two-digit (can't get away with parsing a single char)
            string[] input = Input.Split("\n", StringSplitOptions.RemoveEmptyEntries);

            // counts the number of valid passwords in input
            int nValidPasswords = 0;

            // Each line gives the password policy and then the password.
            foreach (string line in input)
            {
                PasswordPolicy passwordPolicy = new PasswordPolicy(line.Split(new[] { "-", " ", ": " }, StringSplitOptions.RemoveEmptyEntries));
                if (passwordPolicy.IsValid())
                {
                    nValidPasswords++;
                }
            }



            // The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid.
            // For example, "1-3 a:" means that the password must contain 'a' at least 1 time and at most 3 times.

            // How many passwords are valid according to their policies?
            return(nValidPasswords.ToString() + " are valid sled passwords");
        }
예제 #20
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == textBox3.Text)
                {
                    if (PasswordPolicy.IsValid(textBox2.Text))
                    {
                        string passWord = textBox2.Text;
                        string userName = textBox1.Text;
                        string userType = textBox4.Text;

                        if (userType.Equals("admin", StringComparison.InvariantCultureIgnoreCase))
                        {
                            frmAuth objFrmAuth = new frmAuth(userName, passWord);
                            this.Hide();
                            objFrmAuth.Show();
                        }
                        else if (userType.Equals("faculty", StringComparison.InvariantCultureIgnoreCase))
                        {
                            sqlConn.Open();

                            SqlCommand cmd = new SqlCommand("insert into tbl_Login(username, password, login_type) values ('" + userName + "','" + passWord + "', 2)", sqlConn);
                            cmd.ExecuteScalar();

                            MessageBox.Show("User Created!!");

                            frmLogin objFrmLogin = new frmLogin();
                            this.Hide();
                            objFrmLogin.Show();

                            sqlConn.Close();
                        }
                        else if (userType.Equals("student", StringComparison.InvariantCultureIgnoreCase))
                        {
                            sqlConn.Open();

                            SqlCommand cmd = new SqlCommand(@"insert into tbl_Login(username, password, login_type) values ('" + userName + "','" + passWord + "', 3)", sqlConn);
                            cmd.ExecuteScalar();

                            MessageBox.Show("User Created!!");

                            frmLogin objFrmLogin = new frmLogin();
                            this.Hide();
                            objFrmLogin.Show();

                            sqlConn.Close();
                        }
                        else
                        {
                            MessageBox.Show("Invalid User Type");
                            textBox1.Clear();
                            textBox2.Clear();
                            textBox3.Clear();
                            textBox4.Clear();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Password doesn't meet requirements.");
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        textBox4.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("Passwords must match.");
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred but your application did not crash.  Please contact a system admin if this continues.");
                string filePath = @"C:\Users\Error.txt";

                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.WriteLine("-----------------------------------------------------------------------------");
                    writer.WriteLine("Date : " + DateTime.Now.ToString());
                    writer.WriteLine();

                    while (ex != null)
                    {
                        writer.WriteLine(ex.GetType().FullName);
                        writer.WriteLine("Message : " + ex.Message);
                        writer.WriteLine("StackTrace : " + ex.StackTrace);
                    }
                }
            }
        }
예제 #21
0
 internal bool IsValid() => _policy.IsValid(_password);
예제 #22
0
 /// <summary>
 /// How many passwords are valid according to their policies.
 /// </summary>
 public static int Solve(string[] passwordsAndPolicies)
 => passwordsAndPolicies.Count(p => PasswordPolicy.IsValid(p));
예제 #23
0
        public void PasswordPolicy_IsValid_fails_invalidwords()
        {
            var policy = new PasswordPolicy();

            policy.InvalidWords.Add("jim");

            Assert.IsFalse(policy.IsValid("Password123"));
            Assert.IsFalse(policy.IsValid("Pa$$word123"));
            Assert.IsFalse(policy.IsValid("Pa$$w0rd123"));
            Assert.IsFalse(policy.IsValid("Passw0rd123"));
            Assert.IsFalse(policy.IsValid("P@ssword123"));

            Assert.IsFalse(policy.IsValid("AdMin123!&*("));
            Assert.IsFalse(policy.IsValid("adMin123!&*("));
            Assert.IsFalse(policy.IsValid("@dMin123!&*("));
            Assert.IsFalse(policy.IsValid("AdM1n123!&*("));
            Assert.IsFalse(policy.IsValid("AdM!n123!&*("));
            Assert.IsFalse(policy.IsValid("AdM!n123!&*("));
            Assert.IsFalse(policy.IsValid("AdM!n123!&*("));
            Assert.IsFalse(policy.IsValid("AdM!n123!&*("));
            Assert.IsFalse(policy.IsValid("ådmin123!&*(M"));
            Assert.IsFalse(policy.IsValid("àdmin123!&*(M"));
            Assert.IsFalse(policy.IsValid("ádmin123!&*(M"));
            Assert.IsFalse(policy.IsValid("ãdmin123!&*(M"));
            Assert.IsFalse(policy.IsValid("ädmin123!&*(M"));
            Assert.IsFalse(policy.IsValid("ạdmin123!&*(M"));

            Assert.IsFalse(policy.IsValid("Jimbo456"));
            Assert.IsFalse(policy.IsValid("J!mbo456"));
            Assert.IsFalse(policy.IsValid("Jimb0456"));
            Assert.IsFalse(policy.IsValid("jimbo456"));

            Assert.IsFalse(policy.IsValid("qwerty345B;"));
            Assert.IsFalse(policy.IsValid("qw3rty345B;"));
            Assert.IsFalse(policy.IsValid("qwer7y345B;"));
            Assert.IsFalse(policy.IsValid("qwerty345B;"));

            Assert.IsFalse(policy.IsValid("princess"));
            Assert.IsFalse(policy.IsValid("qw3rty345B;"));
            Assert.IsFalse(policy.IsValid("qwer7y345B;"));
            Assert.IsFalse(policy.IsValid("qwerty345B;"));

            Assert.IsFalse(policy.IsValid("Fr@nk789!!", "frank"));
            Assert.IsFalse(policy.IsValid("G3orge789!!", "frank", "george"));
            Assert.IsFalse(policy.IsValid("acme789!!Bob", "frank", "george", "acme"));

            Assert.IsFalse(policy.IsValid("cute!!BobFrank;;", "cute"));
            Assert.IsFalse(policy.IsValid("çute!!BobFrank;;", "cute"));
            Assert.IsFalse(policy.IsValid("ćute!!BobFrank;;", "cute"));
        }
        public void IsValidPasswords()
        {
            var policy = new PasswordPolicy();

            Assert.IsTrue(policy.IsValid("FonjMonj899cc!"));
        }
예제 #25
0
        public void PasswordPolicy_IsValid_fails()
        {
            var policy = new PasswordPolicy();

            Assert.IsFalse(policy.IsValid(null));
            Assert.IsFalse(policy.IsValid(""));
            Assert.IsFalse(policy.IsValid("  "));
            Assert.IsFalse(policy.IsValid("bob"));
            Assert.IsFalse(policy.IsValid("george123"));
            Assert.IsFalse(policy.IsValid("george123bbbbb"));
            Assert.IsFalse(policy.IsValid("1234"));
            Assert.IsFalse(policy.IsValid("4567"));
            Assert.IsFalse(policy.IsValid("9876"));
            Assert.IsFalse(policy.IsValid("8765"));
            Assert.IsFalse(policy.IsValid("7654"));
            Assert.IsFalse(policy.IsValid("6543"));
            Assert.IsFalse(policy.IsValid("5432"));
            Assert.IsFalse(policy.IsValid("4321"));
            Assert.IsFalse(policy.IsValid("Frank Bob 123 !!*("));
        }
    public void CanMatchMaxNoOfAllowedCharacterRepetitions()
    {
      // Arrange
      PasswordPolicy policy = new PasswordPolicy
      {
        MaxNoOfAllowedCharacterRepetitions = 2
      };

      // Act + Assert
      Assert.IsTrue(policy.IsValid("aabb"));
      Assert.IsFalse(policy.IsValid("xaaay"));
    }