Exemple #1
0
 public void IntMathPower()
 {
     Aver.AreEqual(1024, IntUtils.Pow(2, 10));
     Aver.AreEqual(2187, IntUtils.Pow(3, 7));
     Aver.AreEqual(390625, IntUtils.Pow(5, 8));
     Aver.AreEqual(0, IntUtils.Pow(0, 0));
     Aver.AreEqual(0, IntUtils.Pow(0, 1));
     Aver.AreEqual(1, IntUtils.Pow(1, 0));
     Aver.AreEqual(1, IntUtils.Pow(100, 0));
     Aver.AreEqual(100, IntUtils.Pow(100, 1));
 }
Exemple #2
0
        protected virtual IEnumerable <PasswordRepresentation> DoGeneratePassword(PasswordFamily family, PasswordRepresentationType type, PasswordStrengthLevel level)
        {
            if (family != PasswordFamily.Text && family != PasswordFamily.PIN)
            {
                yield break;
            }

            if ((type & PasswordRepresentationType.Text) != 0)
            {
                if (family == PasswordFamily.Text)
                {
                    int score = 0;
                    while (true)
                    {
                        using (var password = Platform.RandomGenerator.Instance.NextRandomWebSafeSecureBuffer(getMinLengthForLevel(family, level), getMaxLengthForLevel(family, level)))
                        {
                            score = CalculateStrenghtScore(family, password);

                            if (score >= getMinScoreForLevel(family, level))
                            {
                                var content     = password.Content;
                                var length      = content.Length;
                                var reprContent = new byte[length];
                                Array.Copy(content, reprContent, length);

                                yield return(new PasswordRepresentation(PasswordRepresentationType.Text, "plain/text", reprContent));

                                break;
                            }
                        }
                    }
                }

                if (family == PasswordFamily.PIN)
                {
                    var min = getMinLengthForLevel(family, level);
                    var max = getMaxLengthForLevel(family, level);

                    var minValue = (int)IntUtils.Pow(10, min - 1);
                    var maxValue = (int)IntUtils.Pow(10, max) - 1;
                    var value    = (uint)Platform.RandomGenerator.Instance.NextScaledRandomInteger(minValue, maxValue);

                    var content     = value.ToString();
                    var reprContent = new byte[content.Length];
                    for (int i = 0; i < content.Length; i++)
                    {
                        reprContent[i] = (byte)content[i];
                    }

                    yield return(new PasswordRepresentation(PasswordRepresentationType.Text, "plain/text", reprContent));
                }
            }
        }