Exemplo n.º 1
0
        private bool constraintRegister()
        {
            string reason = "";

            if (txtPassword1.Text.Trim().Equals("") || txtPassword2.Text.Trim().Equals("") || txtRepassword.Text.Trim().Equals(""))
            {
                lblReason.Text = "Không được bỏ trốn ô này";
                txtPassword1.Focus();
                return(false);
            }
            else
            {
                if (!txtPassword2.Text.Trim().Equals(txtRepassword.Text.Trim()))
                {
                    lblReason.Text = "Không trùng khớp! vui lòng gõ lại";
                    txtRepassword.Focus();
                    return(false);
                }
                else
                {
                    bool truePassword = new PasswordRule().IsPassword(txtPassword2.Text.Trim(), ref reason);
                    if (!truePassword)
                    {
                        lblReason.Text = reason;
                        txtPassword2.ResetText();
                        txtRepassword.ResetText();
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public void Solve()
        {
            var answer = 0;

            foreach (var input in RawInput.Split("\n"))
            {
                var components = input.Split(" ");
                var counts     = components[0].Split("-");

                var rule = new PasswordRule()
                {
                    Lowest            = int.Parse(counts[0]),
                    Highest           = int.Parse(counts[1]),
                    RequiredCharacter = components[1][0],
                    Password          = components[2]
                };

                if (IsValid(rule))
                {
                    answer++;
                }
            }

            Console.WriteLine("Day 2, Part 1");
            Console.WriteLine($"Answer: {answer}");
        }
Exemplo n.º 3
0
        public PasswordGenerator(PasswordRule passwordRule, string customRuleExpression, string masterPassword)
        {
            _masterPassword = masterPassword;

            switch (passwordRule)
            {
            case PasswordRule.FileNameNumbersPlusPassword:
                _expression = "Add(Digits(RemoveFileExtension(FileName)), Password)";
                break;

            case PasswordRule.Password:
                _expression = "Password";
                break;

            case PasswordRule.PasswordPlusFileNameNumbers:
                _expression = "Add(Password, Digits(RemoveFileExtension(FileName)))";
                break;

            default:
                _expression = customRuleExpression;
                break;
            }

            // test expression syntax
            GetPassword("test123.jpg", true);
        }
Exemplo n.º 4
0
        public void ParseRule_GivenValidInput_ReturnsParsedRule(string input, char expectedChar, int expectedMin, int expectedMax)
        {
            var expectedResult = new PasswordRule(expectedChar, expectedMin, expectedMax);

            var result = InputParser.ParseRule(input);

            Assert.AreEqual(expectedResult, result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Checks whether a password is valid under rule 1.
        /// The password must contain the reuired character
        /// between the min and max number of times.
        /// </summary>
        /// <param name="passwordRule">The password to check.</param>
        /// <returns>True if the password matches.</returns>
        private bool IsPasswordValidRule1(PasswordRule passwordRule)
        {
            char   letter   = passwordRule.Letter;
            String password = passwordRule.Password;

            int letterCount = password.Count(x => x == letter);

            return(letterCount >= passwordRule.MinLetter &&
                   letterCount <= passwordRule.MaxLetter);
        }
        public void SetupTests()
        {
            var inputs = new string[] { "1-3", "a:", "1-3", "b:", "2-9", "c:", "1-4", "z:", "10-11", "i:" };

            for (int i = 0; i < inputs.Length; i += 2)
            {
                var rule = new PasswordRule(inputs[i], inputs[i + 1]);

                rules.Add(rule);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Determines whether this instance can login.
        /// </summary>
        /// <returns><c>true</c> if this instance can login; otherwise, <c>false</c>.</returns>
        public bool CanLogin()
        {
            var canLogin = !String.IsNullOrWhiteSpace(Request.UserName);
            var rule     = new PasswordRule();
            var res      = rule.Validate(Password, null);

            if (!res.IsValid)
            {
                canLogin = false;
            }
            return(canLogin);
        }
Exemplo n.º 8
0
        private (PasswordRule Rule, string Password) Parse(Match match)
        {
            // group 0 is the full match
            var min       = int.Parse(match.Groups[1].Value);
            var max       = int.Parse(match.Groups[2].Value);
            var character = match.Groups[3].Value.Single();
            var password  = match.Groups[4].Value;

            var rule = new PasswordRule(min, max, character);

            return(rule, password);
        }
Exemplo n.º 9
0
        public void PasswordRules()
        {
            var    rule = new PasswordRule(8, 1);
            string errorMessage;

            Assert.IsFalse(rule.ValidatePassword("pass", out errorMessage));
            Assert.IsFalse(rule.ValidatePassword("password", out errorMessage));
            Assert.IsTrue(rule.ValidatePassword("Password", out errorMessage));
            rule.SpecialChars = true;
            Assert.IsFalse(rule.ValidatePassword("Password", out errorMessage));
            Assert.IsTrue(rule.ValidatePassword("Passwor#", out errorMessage));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Checks whether a password is valid under rule 2.
        /// The password must have the character at either
        /// the min and max positions.
        /// </summary>
        /// <param name="passwordRule">The password rule.</param>
        /// <returns>True if the password matches.</returns>
        private bool IsPasswordValidRule2(PasswordRule passwordRule)
        {
            char   letter   = passwordRule.Letter;
            String password = passwordRule.Password;

            int minLetterPosition = passwordRule.MinLetter;
            int maxLetterPosition = passwordRule.MaxLetter;

            bool matches1 = password[minLetterPosition - 1] == letter;
            bool matches2 = password[maxLetterPosition - 1] == letter;

            return((matches1 && !matches2) ||
                   (matches2 && !matches1));
        }
Exemplo n.º 11
0
        private bool IsValid(PasswordRule rule)
        {
            var groups = rule.Password.GroupBy(_ => _);

            foreach (var group in groups)
            {
                var count = group.Count();
                if (group.Key == rule.RequiredCharacter && rule.Lowest <= count && count <= rule.Highest)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 12
0
        public async Task <string> ExecuteAsync(string[] data)
        {
            var totalValid = data.Length;

            for (var i = totalValid - 1; i >= 0; i--)
            {
                var passwordRule = new PasswordRule(data[i]);
                if (!passwordRule.IsValid())
                {
                    --totalValid;
                }
            }

            return(totalValid.ToString());
        }
Exemplo n.º 13
0
        private bool IsValid(PasswordRule rule)
        {
            if (rule.Password[rule.Lowest - 1] == rule.RequiredCharacter && rule.Password[rule.Highest - 1] == rule.RequiredCharacter)
            {
                return(false);
            }

            if (rule.Password[rule.Lowest - 1] != rule.RequiredCharacter && rule.Password[rule.Highest - 1] != rule.RequiredCharacter)
            {
                return(false);
            }

            if (rule.Password[rule.Lowest - 1] == rule.RequiredCharacter || rule.Password[rule.Highest - 1] == rule.RequiredCharacter)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 14
0
        public void PredefinedRules(PasswordRule passwordRule, string fileName, string password, string result)
        {
            var parser = new PasswordGenerator(passwordRule, null, password);

            Assert.AreEqual(result, parser.GetPassword(fileName));
        }