/// <summary>
 /// Checks if a password meets all criterias
 /// </summary>
 /// <param name="password"></param>
 /// <param name="length"></param>
 /// <param name="minStrength"></param>
 /// <param name="forceNumbers"></param>
 /// <param name="forceUpper"></param>
 /// <param name="forceLower"></param>
 /// <param name="minNumbers"></param>
 /// <param name="minLowers"></param>
 /// <param name="minUppers"></param>
 /// <returns></returns>
 private bool IsPasswordValid(char[] password, uint length, PasswordStrengthEnum minStrength, bool forceNumbers, bool forceUpper, bool forceLower, uint minNumbers, uint minLowers, uint minUppers)
 {
     if (password == null)
     {
         return(false);
     }
     if (password.Count() != length)
     {
         return(false);
     }
     if (forceNumbers && (uint)password.Count((c) => char.IsNumber(c)) < minNumbers)
     {
         return(false);
     }
     if (forceUpper && (uint)password.Count((c) => char.IsUpper(c)) < minUppers)
     {
         return(false);
     }
     if (forceLower && (uint)password.Count((c) => char.IsLower(c)) < minLowers)
     {
         return(false);
     }
     if (minStrength != PasswordStrengthEnum.NotDefined)
     {
         var strength = base.GetPasswordStrength(password);
         if ((PasswordStrengthEnum)strength < minStrength)
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #2
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            _value = null;
            _calculateStrengthDelegate = null;
            _strength = PasswordStrengthEnum.NotDefined;

            GC.SuppressFinalize(this);
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Core.Password"/> class.
 /// </summary>
 /// <exception cref="ArgumentException">strength has the NotDefined value.</exception>
 public Password(char[] value, PasswordStrengthEnum strength)
     : this(value)
 {
     if (strength == PasswordStrengthEnum.NotDefined)
     {
         throw new ArgumentException();
     }
     _strength = strength;
 }
Exemple #4
0
 /// <summary>
 /// Invokes the CalculateStrengthDelegate function if any.
 /// </summary>
 internal void CalculateStrength()
 {
     try
     {
         if (_calculateStrengthDelegate != null)
         {
             Strength = (PasswordStrengthEnum)_calculateStrengthDelegate(Value);
         }
     }
     catch (Exception)
     {
         Strength = PasswordStrengthEnum.None;
     }
 }
        /// <summary>
        /// Password generation method
        /// </summary>
        /// <param name="length"></param>
        /// <param name="forceNumbers"></param>
        /// <param name="forceLowerCase"></param>
        /// <param name="forceUpperCase"></param>
        /// <param name="useSymbols"></param>
        /// <param name="dictionary"></param>
        /// <param name="minNumbers"></param>
        /// <param name="minLowers"></param>
        /// <param name="minUppers"></param>
        /// <param name="minStrength"></param>
        /// <returns></returns>
        private Password GeneratePassword(uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, IList <char> dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength)
        {
            var pass = new char[length];

            for (var i = 0; i < pass.Length; i++)
            {
                if (_canceled)
                {
                    break;
                }

                pass[i] = GetRandomChar(dictionary);

                // Checks if previous char is the same, if it is, invalidates it
                if (i > 0 && pass[i] == pass[i - 1])
                {
                    i--;
                }

                if (i != (length - 1))
                {
                    continue; // loop while not at the end of the char array
                }
                // Check password validity
                if (!IsPasswordValid(pass, length, minStrength, forceNumbers, forceUpperCase, forceLowerCase, minNumbers, minLowers, minUppers))
                {
                    Thread.Sleep(TimeSpan.FromTicks(10000)); // Make a quick pause to lower cpu usage
                    i = -1;                                  // Restart main loop
                }
                else
                {
                    return(new Password(pass, base.GetPasswordStrength));
                }
            }

            return(null); // returns null if canceled
        }
        private Password GeneratePassword(uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, IList <char> dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength, Action <Password> callback)
        {
            var p = GeneratePassword(length, forceNumbers, forceLowerCase, forceUpperCase, useSymbols, dictionary,
                                     minNumbers, minLowers, minUppers, minStrength);

            if (callback != null && !_canceled)
            {
                callback(p);
            }

            return(p);
        }
        public void BeginGenerate(uint count, uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, char[] dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength, Action <GenerationEventArgs> callback, Action <Password> passwordCallback)
        {
            if (IsGenerating)
            {
                return;
            }
            IsGenerating = true;

            try
            {
                CheckParameters(length, useSymbols, forceNumbers, forceLowerCase, forceUpperCase, minNumbers, minLowers, minUppers);
            }
            catch (Exception e)
            {
                IsGenerating = false;
                if (callback != null)
                {
                    callback.Invoke(new GenerationEventArgs(null, TimeSpan.Zero, e));
                }
                return;
            }

            Exception exp = null;

            if (dictionary == null)
            {
                dictionary = useSymbols ? _complexDictionary : _simpleDictionary;
            }

            if (_cancellation != null)
            {
                _cancellation.Dispose();
            }
            _cancellation = new CancellationTokenSource();

            GeneratedPasswordsCount = 0;
            Canceled  = false;
            CanCancel = true;

            var tasks = new Task <Password> [count];
            var tf    = new TaskFactory <Password>(_cancellation.Token);

            var st = new Stopwatch();

            st.Start();

            for (uint i = 0; i < tasks.Length; i++)
            {
                tasks[i] = tf.StartNew(() => GeneratePassword(length, forceNumbers, forceLowerCase, forceUpperCase, useSymbols, dictionary, minNumbers, minLowers, minUppers, minStrength, passwordCallback), _cancellation.Token);
            }
            try
            {
                Task.WaitAll(tasks, _cancellation.Token);
            }
            catch (OperationCanceledException e)
            {
                _cancellation.Dispose();
                _cancellation = null;
                exp           = e;

                Canceled = true;
            }
            finally { st.Stop(); }

            CanCancel = false;

            var passwords = new Password[count];

            if (exp == null)
            {
                for (var i = 0; i < passwords.Length; i++)
                {
                    if (tasks[i].Result != null)
                    {
                        passwords[i] = tasks[i].Result;
                    }

                    tasks[i].Dispose();
                }
            }

            if (callback != null)
            {
                callback.Invoke(new GenerationEventArgs(passwords, st.Elapsed, exp));
            }

            IsGenerating = false;
        }
 /// <summary>
 /// Starts an asynchronous password generation
 /// </summary>
 /// <param name="count">The password(s) count to generate</param>
 /// <param name="length">The password(s) length</param>
 /// <param name="forceNumbers">Force usage of numbers</param>
 /// <param name="forceLowerCase">Force usage of lower case letters</param>
 /// <param name="forceUpperCase">Force usage of upper case letters</param>
 /// <param name="useSymbols">Allow symbols</param>
 /// <param name="dictionary">A custom dictionary to use</param>
 /// <param name="minNumbers">The minimum numbers count the password must have</param>
 /// <param name="minLowers">The minimum lower case letters the password(s) must have</param>
 /// <param name="minUppers">The minimum upper case letters the password(s) must have</param>
 /// <param name="minStrength">The minimum password strength or PasswordStrength.NotDefined</param>
 /// <param name="callback">The callback to use when generation is finished</param>
 public override void BeginGenerate(uint count, uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, char[] dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength, Action <GenerationEventArgs> callback)
 {
     BeginGenerate(count, length, forceNumbers, forceLowerCase, forceUpperCase, useSymbols, dictionary, minNumbers, minLowers, minUppers, minStrength, callback, null);
 }
        /// <summary>
        /// Generates random passwords
        /// </summary>
        /// <param name="count">The password(s) count to generate</param>
        /// <param name="length">The password(s) length</param>
        /// <param name="forceNumbers">Force usage of numbers</param>
        /// <param name="forceLowerCase">Force usage of lower case letters</param>
        /// <param name="forceUpperCase">Force usage of upper case letters</param>
        /// <param name="useSymbols">Allow symbols</param>
        /// <param name="dictionary">A custom dictionary to use</param>
        /// <param name="minNumbers">The minimum numbers count the password must have</param>
        /// <param name="minLowers">The minimum lower case letters the password(s) must have</param>
        /// <param name="minUppers">The minimum upper case letters the password(s) must have</param>
        /// <param name="minStrength">The minimum password strength or PasswordStrength.NotDefined</param>
        /// <returns>A <see cref="T:Core.Password"/> array.</returns>
        public override Password[] Generate(uint count, uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, char[] dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength)
        {
            if (IsGenerating)
            {
                return(null);
            }
            CheckParameters(length, useSymbols, forceNumbers, forceLowerCase, forceUpperCase, minNumbers, minLowers, minUppers);

            IsGenerating            = true;
            CanCancel               = false;
            Canceled                = false;
            GeneratedPasswordsCount = 0;

            if (dictionary == null)
            {
                dictionary = useSymbols ? _complexDictionary : _simpleDictionary;
            }

            var passwords = new Password[count];

            for (int i = 0; i < passwords.Length; i++)
            {
                passwords[i] = GeneratePassword(length, forceNumbers, forceLowerCase, forceUpperCase, useSymbols, dictionary, minNumbers, minLowers, minUppers, minStrength);
            }

            IsGenerating = false;

            return(passwords);
        }
 /// <summary>
 /// Starts an asynchronous password generation
 /// </summary>
 /// <param name="count">The password(s) count to generate</param>
 /// <param name="length">The password(s) length</param>
 /// <param name="forceNumbers">Force usage of numbers</param>
 /// <param name="forceLowerCase">Force usage of lower case letters</param>
 /// <param name="forceUpperCase">Force usage of upper case letters</param>
 /// <param name="useSymbols">Allow symbols</param>
 /// <param name="dictionary">A custom dictionary to use</param>
 /// <param name="minNumbers">The minimum numbers count the password must have</param>
 /// <param name="minLowers">The minimum lower case letters the password(s) must have</param>
 /// <param name="minUppers">The minimum upper case letters the password(s) must have</param>
 /// <param name="callback">The callback to use when generation is finished</param>
 public abstract void BeginGenerate(uint count, uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, char[] dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength, Action <GenerationEventArgs> callback);
 /// <summary>
 /// Generates random passwords
 /// </summary>
 /// <param name="count">The password(s) count to generate</param>
 /// <param name="length">The password(s) length</param>
 /// <param name="forceNumbers">Force usage of numbers</param>
 /// <param name="forceLowerCase">Force usage of lower case letters</param>
 /// <param name="forceUpperCase">Force usage of upper case letters</param>
 /// <param name="useSymbols">Allow symbols</param>
 /// <param name="dictionary">A custom dictionary to use</param>
 /// <param name="minNumbers">The minimum numbers count the password must have</param>
 /// <param name="minLowers">The minimum lower case letters the password(s) must have</param>
 /// <param name="minUppers">The minimum upper case letters the password(s) must have</param>
 /// <returns>A <see cref="T:Core.Password"/> array.</returns>
 public abstract Password[] Generate(uint count, uint length, bool forceNumbers, bool forceLowerCase, bool forceUpperCase, bool useSymbols, char[] dictionary, uint minNumbers, uint minLowers, uint minUppers, PasswordStrengthEnum minStrength);