コード例 #1
0
 // TODO: Separate repository
 public static void Store(string service, PasswordRequirements req)
 {
     lock (locker)
     {
         InsertOrUpdate(service, req);
     }
 }
コード例 #2
0
        /// <summary>
        /// Verifies password matches requirement, add characters if needed
        /// </summary>
        private string VerifyPassword(string sourcePassword, PasswordRequirements requirements)
        {
            bool success  = true;
            var  password = sourcePassword;

            if ((requirements & PasswordRequirements.NumberRequired) > 0)
            {
                success &= (sourcePassword.Any(x => Numbers.Contains(x)));
                if (!success)
                {
                    password += Numbers[GetAscii(sourcePassword[0]) % Numbers.Length];
                }
            }

            if ((requirements & PasswordRequirements.SymbolRequired) > 0)
            {
                success  &= (sourcePassword.Any(x => Symbols.Contains(x)));
                password += Symbols[GetAscii(sourcePassword[0]) % Symbols.Length];
            }

            if ((requirements & PasswordRequirements.UpperCaseRequired) > 0)
            {
                success  &= (sourcePassword.Any(x => Uppercase.Contains(x)));
                password += Uppercase[GetAscii(sourcePassword[0]) % Uppercase.Length];
            }

            return(password);
        }
コード例 #3
0
        public void TestInvalidPasswordParameters_Multiple()
        {
            //Arrange
            PasswordGenerator generator = new PasswordGenerator();
            var requirements            = new PasswordRequirements()
            {
                MaxLength          = 16,
                MinLength          = 8,
                MinLowerAlphaChars = 5,
                MinNumericChars    = 4,
                MinSpecialChars    = 4,
                MinUpperAlphaChars = 4
            };
            var       expectedExcetpion = new Exception("Combination of MinLowerAlphaChars, MinNumericChars, MinSpecialChars and MinUpperAlphaChars cannot be greater than MaxLength.");
            Exception resultExcetpion   = null;

            //Act
            try
            {
                string result = generator.GeneratePassword(requirements);
            }
            catch (Exception ex)
            {
                resultExcetpion = ex;
            }

            //Assert
            Assert.AreEqual(expectedExcetpion.Message, resultExcetpion.Message);
        }
コード例 #4
0
        public void TestInvalidMinLength()
        {
            //Arrange
            PasswordGenerator generator = new PasswordGenerator();
            var requirements            = new PasswordRequirements()
            {
                MaxLength = 8,
                MinLength = 16,
            };
            var       expectedExcetpion = new Exception("MinLength cannot be greater than MaxLength.");
            Exception resultExcetpion   = null;

            //Act
            try
            {
                string result = generator.GeneratePassword(requirements);
            }
            catch (Exception ex)
            {
                resultExcetpion = ex;
            }

            //Assert
            Assert.AreEqual(expectedExcetpion.Message, resultExcetpion.Message);
        }
コード例 #5
0
        public string GeneratePassword(PasswordRequirements requirements)
        {
            string[] randomCharacters = new[]
            {
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ", // uppercase
                "abcdefghijkmnopqrstuvwxyz",  // lowercase
                "0123456789",                 // digits
                "!@#$£%^&*()<>{}-="           // non-alphanumeric
            };
            Random      rand  = new Random(Environment.TickCount);
            List <char> chars = new List <char>();

            try
            {
                if (requirements.MaxLength == 0)
                {
                    requirements.MaxLength = 8;
                }

                if (requirements.MinLength == 0)
                {
                    requirements.MinLength = 5;
                }

                if (requirements.MinUpperAlphaChars > 0)
                {
                    PwRequirements(requirements.MinUpperAlphaChars, chars, rand, randomCharacters, 0);
                }

                if (requirements.MinLowerAlphaChars > 0)
                {
                    PwRequirements(requirements.MinLowerAlphaChars, chars, rand, randomCharacters, 1);
                }

                if (requirements.MinNumericChars > 0)
                {
                    PwRequirements(requirements.MinNumericChars, chars, rand, randomCharacters, 2);
                }

                if (requirements.MinSpecialChars > 0)
                {
                    PwRequirements(requirements.MinSpecialChars, chars, rand, randomCharacters, 3);
                }

                for (int i = chars.Count; i < requirements.MaxLength; i++)
                {
                    string generatedChars = randomCharacters[rand.Next(0, randomCharacters.Length)];
                    chars.Insert(rand.Next(0, chars.Count),
                                 generatedChars[rand.Next(0, generatedChars.Length)]);
                }

                return(new string(chars.ToArray()));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public void WithValidConstructorParameters_ThenThePropertiesMapVerbatim()
        {
            PasswordRequirements subject = new PasswordRequirements(42, 41, true, true, true, true);

            Assert.AreEqual(42, subject.MaximumLength);
            Assert.AreEqual(41, subject.MinimumLength);
            Assert.IsTrue(subject.Lowercase);
            Assert.IsTrue(subject.Uppercase);
            Assert.IsTrue(subject.Number);
            Assert.IsTrue(subject.Special);
        }
コード例 #7
0
        public MainPage()
        {
            InitializeComponent();
            ViewModel      = new PasswordRequestViewModel();
            BindingContext = ViewModel;

            DefaultRequirements =
                PasswordRequirements.SymbolRequired
                | PasswordRequirements.UpperCaseRequired
                | PasswordRequirements.NumberRequired;
        }
        public void ThenThePropertiesMapVerbatim()
        {
            PasswordRequirements subject = PasswordRequirements.Default;

            Assert.AreEqual(33, subject.MaximumLength);
            Assert.AreEqual(8, subject.MinimumLength);
            Assert.IsTrue(subject.Lowercase);
            Assert.IsTrue(subject.Uppercase);
            Assert.IsTrue(subject.Number);
            Assert.IsTrue(subject.Special);
        }
コード例 #9
0
        public string GeneratePassword(PasswordRequirements requirements)
        {
            var password = GenerateUppercase(requirements.MinUpperAlphaChars);

            password = string.Concat(password, GenerateLowercase(requirements.MinLowerAlphaChars));
            password = string.Concat(password, GenerateNumeric(requirements.MinNumericChars));
            password = string.Concat(password, GenerateSpecial(requirements.MinSpecialChars));
            password = string.Concat(password, GeneratePadding(requirements.MinLength, requirements.MaxLength, password.Length));

            //randomize the password
            return(new string(password.ToCharArray().OrderBy(c => (Random.Next())).ToArray()));
        }
コード例 #10
0
        private void GeneratePassword(string password, string serviceName, PasswordRequirements requirement)
        {
            PasswordGenerator generator = new PasswordGenerator(requirement);

            ViewModel.Password = generator.GeneratePassword(password, serviceName, 10);

            // store settings
            ServicePersistor.Persist(serviceName, requirement);
            ViewModel.PasswordVisible   = true;
            ViewModel.CreateNewPassword = true;

            IoCProviders.GetProvider <ICopyToClipboard>().CopyToClipboard(ViewModel.Password);
            IoCProviders.GetProvider <IProvideNotifications>().Notify(null, "Password has been copied to clipboard");
        }
コード例 #11
0
        static void Main(string[] args)
        {
            int numberOfGenerations = 50;

            var generator    = new PasswordGenerator();
            var requirements = new PasswordRequirements(
                minLength: 8,
                maxLength: 16,
                minLowerAlpha: 2,
                minUpperAlpha: 2,
                minNumeric: 2,
                minSpecial: 2);

            for (int i = 0; i < numberOfGenerations; i++)
            {
                Console.WriteLine(generator.GeneratePassword(requirements));
            }
        }
コード例 #12
0
        private static void InsertOrUpdate(string service, PasswordRequirements req)
        {
            var requirementInt = Convert.ToInt32(req);

            var existingEntity = conn.Table <ServicesEntity>().FirstOrDefault(e => e.Service == service);

            if (existingEntity == null)
            {
                var newEntity = new ServicesEntity()
                {
                    Service      = service,
                    Requirements = requirementInt
                };

                conn.Insert(newEntity);
            }
            else
            {
                existingEntity.Requirements = requirementInt;
                conn.Update(existingEntity);
            }
        }
コード例 #13
0
        public void TestNullPasswordRequirements()
        {
            //Arrange
            PasswordGenerator    generator    = new PasswordGenerator();
            PasswordRequirements requirements = null;

            var       expectedExcetpion = new Exception("Password requirements are null.");
            Exception resultExcetpion   = null;

            //Act
            try
            {
                string result = generator.GeneratePassword(requirements);
            }
            catch (Exception ex)
            {
                resultExcetpion = ex;
            }

            //Assert
            Assert.AreEqual(expectedExcetpion.Message, resultExcetpion.Message);
        }
コード例 #14
0
 /// <summary>
 /// Store password requirement for a given service
 /// </summary>
 public static void Persist(string service, PasswordRequirements req)
 {
     SqliteStorage.Store(GetServiceName(service), req);
 }
コード例 #15
0
 public PasswordGenerator(PasswordRequirements req)
 {
     Requirements = req;
 }