Esempio n. 1
0
        public void TestConstruction()
        {
            using (PasswordHash pwd1 = new PasswordHash(TEST_PASSWORD))
            {
                //no salt provided
                using (PasswordHash pwd2 = new PasswordHash(TEST_PASSWORD))
                    Assert.AreNotEqual(pwd1, pwd2);
                using (PasswordHash pwd2 = new PasswordHash(false, Password.Encoding.GetBytes(TEST_PASSWORD)))
                    Assert.AreNotEqual(pwd1, pwd2);
                //identical salt
                using (PasswordHash pwd2 = new PasswordHash(TEST_PASSWORD, pwd1.Salt))
                    Assert.AreEqual(pwd1, pwd2);
                using (PasswordHash pwd2 = new PasswordHash(false, Password.Encoding.GetBytes(TEST_PASSWORD), pwd1.Salt))
                    Assert.AreEqual(pwd1, pwd2);
                using (PasswordHash pwd2 = new PasswordHash(new MemoryStream(Password.Encoding.GetBytes(TEST_PASSWORD)), pwd1.Salt))
                    Assert.AreEqual(pwd1, pwd2);

                using (PasswordHash pwd2 = new PasswordHash(new Password(TEST_PASSWORD), pwd1.Salt))
                    Assert.AreEqual(pwd1, pwd2);
                using (SecureString sstr = SecureStringUtils.Create(TEST_PASSWORD))
                {
                    using (PasswordHash pwd2 = new PasswordHash(sstr, pwd1.Salt))
                        Assert.AreEqual(pwd1, pwd2);
                    using (PasswordHash pwd2 = new PasswordHash(sstr))
                        Assert.AreNotEqual(pwd1, pwd2);
                }
            }
        }
        IEnumerable <SecureString> MakeStrings()
        {
            yield return(SecureStringUtils.Create(TEST_PASSWORD));

            yield return(SecureStringUtils.Create(TEST_PASSWORD.ToCharArray()));

            yield return(SecureStringUtils.Create(new MemoryStream(Encoding.Unicode.GetBytes(TEST_PASSWORD))));

            yield return(SecureStringUtils.Create(new MemoryStream(Encoding.UTF8.GetBytes(TEST_PASSWORD)), Encoding.UTF8));

            yield return(SecureStringUtils.AppendAll(new SecureString(), TEST_PASSWORD));
        }
        public void TestRecreate()
        {
            PasswordKey pk1 = new PasswordKey(TEST_PASSWORD);
            PasswordKey pk2 = new PasswordKey(SecureStringUtils.Create(TEST_PASSWORD));

            Assert.AreNotEqual(pk1.CreateKey().Key, pk2.CreateKey().Key);

            pk2.Salt = pk1.Salt;
            Assert.AreEqual(pk1.CreateKey().Key, pk2.CreateKey().Key);
            pk1.CreateKey();
            Assert.AreEqual(pk1.CreateKey().Key, pk2.CreateKey().Key);

            pk1.Salt = new Salt();
            Assert.AreNotEqual(pk1.Salt, pk2.Salt);
            Assert.AreNotEqual(pk1.CreateKey().Key, pk2.CreateKey().Key);
            Assert.AreEqual(pk1.CreateKey().Key, pk2.CreateKey(pk1.Salt).Key);
            pk2.Salt = pk1.Salt;
            Assert.AreEqual(pk1.Salt, pk2.Salt);
            Assert.AreEqual(pk1.CreateKey().Key, pk2.CreateKey().Key);
        }