internal static (double, double, string) StrengthChecker(string data)
        {
            if (_estimator == null)
            {
                _estimator = new ZxcvbnEstimator();
            }
            if (string.IsNullOrEmpty(data))
            {
                return(0, 0, string.Empty);
            }
            string strength = null;
            var    result   = _estimator.EstimateStrength(data);
            var    calc     = Math.Log(result.Guesses) / Math.Log(10);

            if (calc < AppConstants.AccStrengthVeryWeak)
            {
                strength = "VERY_WEAK";
            }
            else if (calc < AppConstants.AccStrengthWeak)
            {
                strength = "WEAK";
            }
            else if (calc < AppConstants.AccStrengthSomeWhatSecure)
            {
                strength = "SOMEWHAT_SECURE";
            }
            else if (calc >= AppConstants.AccStrengthSomeWhatSecure)
            {
                strength = "SECURE";
            }
            double percentage = Math.Round(Math.Min((calc / 16) * 100, 100));

            return(calc, percentage, strength);
        }
Ejemplo n.º 2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            string password = value as string;

            if (string.IsNullOrEmpty(password))
            {
                return(new ValidationResult("Password can't be empty."));
            }

            var estimator = new ZxcvbnEstimator();
            var result    = estimator.EstimateStrength(password);

            if (result.Score < 3)
            {
                string warning = !string.IsNullOrWhiteSpace(result.Feedback.Warning)
                    ? " " + result.Feedback.Warning.TrimEnd('.') + "."
                    : "";

                string suggestions = string.Join(" ", result.Feedback.Suggestions.Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.TrimEnd('.') + "."));

                if (!string.IsNullOrWhiteSpace(suggestions))
                {
                    suggestions = " " + suggestions;
                }

                return(new ValidationResult(string.Join("", "Password is not strong enough.", warning, suggestions)));
            }

            return(ValidationResult.Success);
        }
        /// <summary>
        /// Demo of Dot net wrapper for zxcvbn Password strength library for dot net
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.Write("Enter Password : "******"____________________\n\n");


            var estimator = new ZxcvbnEstimator();
            var result    = estimator.EstimateStrength(password);

            Console.WriteLine(result.Password);
            Console.WriteLine("____________________\n\n");
            Console.WriteLine(result.Score);
            Console.WriteLine(result.CalcTime);
            //Console.WriteLine(result.CrackTimesDisplay);
            //Console.WriteLine(result.CrackTimesSeconds);
            Console.WriteLine(result.Feedback);
            Console.WriteLine(result.Guesses);
            Console.WriteLine(result.GuessesLog10);
            Console.WriteLine(result.Sequence);


            if (result.Score < 3)
            {
                Console.WriteLine("Password is not strong enough");
                Console.WriteLine(result.Feedback.Warning);
                Console.WriteLine(string.Join(Environment.NewLine, result.Feedback.Suggestions));
            }

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        internal static StrengthIndicator StrengthChecker(string data)
        {
            if (_estimator == null)
            {
                _estimator = new ZxcvbnEstimator();
            }

            var strengthIndicator = new StrengthIndicator();

            if (string.IsNullOrEmpty(data))
            {
                throw new Exception("Can't check strength for empty string.");
            }

            var result = _estimator.EstimateStrength(data);

            strengthIndicator.Guesses = Math.Log(result.Guesses) / Math.Log(10);
            if (strengthIndicator.Guesses < Constants.StrengthScoreVeryWeak)
            {
                strengthIndicator.Strength = Constants.StrengthVeryWeak;
            }
            else if (strengthIndicator.Guesses < Constants.StrengthScoreWeak)
            {
                strengthIndicator.Strength = Constants.StrengthWeak;
            }
            else if (strengthIndicator.Guesses < Constants.StrengthScoreSomeWhatSecure)
            {
                strengthIndicator.Strength = Constants.StrengthSomewhatSecure;
            }
            else if (strengthIndicator.Guesses >= Constants.StrengthScoreSomeWhatSecure)
            {
                strengthIndicator.Strength = Constants.StrengthSecure;
            }

            strengthIndicator.Percentage = Math.Round(Math.Min((strengthIndicator.Guesses / 16) * 100, 100));
            return(strengthIndicator);
        }