N
N
neitoo2022-02-13 09:53:14
WPF
neitoo, 2022-02-13 09:53:14

What is the error checking for the password complexity level?

Password Requirements
Level 1
Number of characters less than 8.
Consists of lowercase letters.
Level 2
Number of characters >= 8
Lowercase and uppercase letters are used.
Level 3
Previous conditions + numbers are used.
Level 4
Preceding conditions + special characters are used (example: !#;%:?*()_+<>?|)

My code

var password = ValuePass.Text;
            var regex = new Regex(@"([a - z])");
            if (password.Length < 8 && regex.IsMatch(password))
            {
                resultValue.Text = "1 уровень";
                return;
            }

            var regex2 = new Regex(@"([a-zA-Z])");
            if (password.Length >= 8 && regex2.IsMatch(password))
            {
                resultValue.Text = "2 уровень";
                return;
            }

            var regex1 = new Regex(@"([0 - 9])");
            if (password.Length >= 8 &&  regex1.IsMatch(password) && regex2.IsMatch(password))
            {
                resultValue.Text = "3 уровень";
                return;
            }

            var regex3 = new Regex(@"([!,@,#,$,%,^,&,*,?,_,~])");
            if (password.Length >= 8 && regex1.IsMatch(password) && regex2.IsMatch(password) && regex3.IsMatch(password))
            {
                resultValue.Text = "4 уровень";
                return;
            }

But it only works up to level 2, and then whatever you write, there will be no higher level.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Planet_93, 2022-02-13
@neitoo

You have incorrect logic of checks. In this case, when the password matches level 4, it will still show level 2, since you have a return. That is, return, in any case, will end the execution of the method after level 2, although the password corresponds to level 3 or 4.
You have two options, either swap the levels, that is, first check for compliance with level 4, then level 3, and so on until the first, with return when checking each level.
Or remove return from under each if and leave only one at the end of the method.
Second option:

var password = ValuePass.Text;
            var regex = new Regex(@"([a - z])");
            var regex2 = new Regex(@"([a-zA-Z])");
            var regex1 = new Regex(@"([0 - 9])");
            var regex3 = new Regex(@"([!,@,#,$,%,^,&,*,?,_,~])");
            if (password.Length >= 8 && regex1.IsMatch(password) && regex2.IsMatch(password) && regex3.IsMatch(password))
            {
                resultValue.Text = "4 уровень";
                return;
            }
            if (password.Length >= 8 &&  regex1.IsMatch(password) && regex2.IsMatch(password))
            {
                resultValue.Text = "3 уровень";
                return;
            }
            if (password.Length >= 8 && regex2.IsMatch(password))
            {
                resultValue.Text = "2 уровень";
                return;
            }
            if (password.Length < 8 && regex.IsMatch(password))
            {
                resultValue.Text = "1 уровень";
                return;
            }

First option:
var password = ValuePass.Text;
            var regex = new Regex(@"([a - z])");
            if (password.Length < 8 && regex.IsMatch(password))
            {
                resultValue.Text = "1 уровень";
            }

            var regex2 = new Regex(@"([a-zA-Z])");
            if (password.Length >= 8 && regex2.IsMatch(password))
            {
                resultValue.Text = "2 уровень";
            }

            var regex1 = new Regex(@"([0 - 9])");
            if (password.Length >= 8 &&  regex1.IsMatch(password) && regex2.IsMatch(password))
            {
                resultValue.Text = "3 уровень";
            }

            var regex3 = new Regex(@"([!,@,#,$,%,^,&,*,?,_,~])");
            if (password.Length >= 8 && regex1.IsMatch(password) && regex2.IsMatch(password) && regex3.IsMatch(password))
            {
                resultValue.Text = "4 уровень";
            }
            return;

V
Vladimir Korotenko, 2022-02-13
@firedragon

I don't like regexps, can it be easier to do?

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var pwd = "Test1236";
            var r = new PasswordChecker(pwd);
            ;

        }
    }

    public class PasswordChecker
    {
        private readonly char[] _upper = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', };
        private readonly char[] _digit = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private readonly char[] _special = new char[] { '!', '@', '#', '$', '%', '^', '&', '*', '?', '_', '~' };
        private readonly int _strongPasswordLength;
        private readonly string _password;
        public bool IsLengthOk => _password.Length > _strongPasswordLength;
        public bool HasUpper { get; private set; }
        public bool HasDigit { get; private set; }
        public bool HasSpecial { get; private set; }

        public PasswordChecker(string password, int passwordLength=8)
        {
            _password = password;
            _strongPasswordLength = passwordLength;
            foreach (var ch in _password)
            {
                HasUpper = _upper.Any(x => x == ch);
                HasDigit = _digit.Any(x => x == ch);
                HasSpecial = _special.Any(x => x == ch);
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question