Return a rating for how strong the string is as a password - CSharp Security

CSharp examples for Security:Password

Description

Return a rating for how strong the string is as a password

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;/*from ww w . j  ava2s .  co  m*/

public class Main{
        //Return a rating for how strong the string is as a password
        //Max rating is 100
        //Credits for original function to D. Rijmenants
        //If there are problems with copyright contact us and we will delete it
        public static int PasswordStrength(string input)
        {
            double total = 0;
            bool hasUpperCase = false;
            bool hasLowerCase = false;

            total = input.Length * 3;

            char currentLetter;
            foreach (char t in input)
            {
                currentLetter = t;
                if (Convert.ToInt32(currentLetter) >= 65 && Convert.ToInt32(currentLetter) <= 92)
                    hasUpperCase = true;

                if (Convert.ToInt32(currentLetter) >= 97 && Convert.ToInt32(currentLetter) <= 122)
                    hasLowerCase = true;
            }

            if (hasUpperCase && hasLowerCase) total *= 1.2;

            foreach (char t in input)
            {
                currentLetter = t;
                if (Convert.ToInt32(currentLetter) >= 48 && Convert.ToInt32(currentLetter) <= 57) //Numbers
                    if (hasUpperCase && hasLowerCase) total *= 1.4;
            }

            foreach (char t in input)
            {
                currentLetter = t;
                if ((Convert.ToInt32(currentLetter) > 47 || Convert.ToInt32(currentLetter) < 123) &&
                    (Convert.ToInt32(currentLetter) < 58 || Convert.ToInt32(currentLetter) > 64)) continue;
                total *= 1.5;
                break;
            }

            if (total > 100.0) total = 100.0;

            return (int)total;
        }
}

Related Tutorials