public void RegisterTest()
        {
            var alice = Protector.Register("alice", "mypass");

            Console.WriteLine($"Name: {alice.Name}");
            Console.WriteLine($"Salt: {alice.Salt}");
            Console.WriteLine($"SaltedHash: {alice.SaltedHashedPassword}");

            Assert.IsTrue(Protector.CheckPassword("alice", "mypass"));
            Assert.IsFalse(Protector.CheckPassword("alice", "notpass"));
        }
Example #2
0
        static void Main(string[] args)
        {
            WriteLine("Registering Alice with Pa$$w0rd.");
            var alice = Protector.Register("Alice", "Pa$$w0rd");

            WriteLine($"Name: {alice.Name}");
            WriteLine($"Salt: {alice.Salt}");
            WriteLine("Password (salted and hashed): {0}",
                      arg0: alice.SaltedHashedPassword);
            WriteLine();

            Write("Enter a new user to register: ");
            string username = ReadLine();

            Write($"Enter a password for {username}: ");
            string password = ReadLine();
            var    user     = Protector.Register(username, password);

            WriteLine($"Name: {user.Name}");
            WriteLine($"Salt: {user.Salt}");
            WriteLine("Password (salted and hashed): {0}",
                      arg0: user.SaltedHashedPassword);
            WriteLine();

            bool correctPassword = false;

            while (!correctPassword)
            {
                Write("Enter a username to log in: ");
                string loginUsername = ReadLine();
                Write("Enter a password to log in: ");
                string loginPassword = ReadLine();

                correctPassword = Protector.CheckPassword(
                    loginUsername, loginPassword);

                if (correctPassword)
                {
                    WriteLine($"Correct! {loginUsername} has been logged in.");
                }
                else
                {
                    WriteLine("Invalid username or password. Try again.");
                }
            }
        }
        static void Main(string[] args)
        {
            WriteLine("Registering [email protected] with Pa$$w0rd");

            var alice = Protector.Register("*****@*****.**", "Pa$$w0rd");

            WriteLine($"Name: {alice.Name}");
            WriteLine($"Salt: {alice.Salt}");
            WriteLine($"SaltedAndHashed password: {alice.SaltedHashedPassword}\n");

            WriteLine("Enter a new user to register: ");
            string username = ReadLine();

            WriteLine($"Enter password for {username}: ");
            string password = ReadLine();

            var newUser = Protector.Register(username, password);

            WriteLine($"New user: {newUser.Name}");
            WriteLine($"Salt: {newUser.Salt}");
            WriteLine($"SaltedAndHashed password: {newUser.SaltedHashedPassword}\n");


            bool correctPassword = false;

            while (!correctPassword)
            {
                WriteLine($"Enter a username to log in with: ");
                string loginUsername = ReadLine();
                WriteLine($"Enter the password for {loginUsername}: ");
                string loginPassword = ReadLine();
                correctPassword = Protector.CheckPassword(loginUsername, loginPassword);
                if (correctPassword)
                {
                    WriteLine($"{loginUsername} has successfully logged in.");
                }
                else
                {
                    WriteLine($"{loginUsername} cannot log in with details provided. Try again. \n");
                }
            }

            WriteLine("End of HashingApp :-)");
        }
Example #4
0
        static void Main(string[] args)
        {
            WriteLine("Registering Teisel UwU with Pa$$w0rd");
            var teisel = Protector.Register("Teisel", "Pa$$w0rd");

            WriteLine($"Name {teisel.Name}");
            WriteLine($"Salt : {teisel.Salt}");
            WriteLine($"Password (Salted and hashed) :  {teisel.SaltedHashedPassword}");

            Write("Enter a new user to register : ");
            string username = ReadLine();

            Write($"Enter password for {username}");
            string password = ReadLine();
            var    user     = Protector.Register(username, password);

            WriteLine($"Name {user.Name}");
            WriteLine($"Salt : {user.Salt}");
            WriteLine($"Password (Salted and hashed) :  {user.SaltedHashedPassword}");

            WriteLine();

            bool correctPassword = false;

            while (!correctPassword)
            {
                Write("Enter a username to log in");
                string loginUsername = ReadLine();
                Write("Enter a password to log in");
                string loginPassword = ReadLine();

                correctPassword = Protector.CheckPassword(loginUsername, loginPassword);

                if (correctPassword)
                {
                    WriteLine($"Welcome back! {loginUsername} has been logged in");
                }
                else
                {
                    WriteLine("Invalid username or password");
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("A user named Alice has been registered with Pas$$w0rd as her password.");
            var alice = Protector.Register("Alice", "Pa$$w0rd");

            Console.WriteLine($"Name: {alice.Name}");
            Console.WriteLine($"Salt: {alice.Salt}");
            Console.WriteLine($"Salted hashed password: {alice.SaltedHashedPassword}");
            Console.WriteLine();

            Console.WriteLine("Enter a username to register: ");
            string username = Console.ReadLine();

            Console.WriteLine("Enter a password to register: ");
            string password = Console.ReadLine();
            var    user     = Protector.Register(username, password);

            Console.WriteLine($"Name: {user.Name}");
            Console.WriteLine($"Salt: {user.Salt}");
            Console.WriteLine($"Salted hashed password: {user.SaltedHashedPassword}");
            Console.WriteLine();
            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine();

            bool correctPassword = false;

            while (!correctPassword)
            {
                Console.WriteLine("Enter a username to login: "******"Enter a password to login: "******"Correct! {loginUsername} has been logged in.");
                }
                else
                {
                    Console.WriteLine("Invalid username or password. Try again.");
                }
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("================== Registration ==================");
            Write("Name: ");
            var name = ReadLine();

            Write("Password: "******"Name: " + user.Name);
            WriteLine("Salt: " + user.Salt);
            WriteLine("SaltedHashedPassword: "******"================== Password Check ==================");
            bool isPasswordCorrect = false;

            while (!isPasswordCorrect)
            {
                Write("Enter Name: ");
                var nameInput = ReadLine();
                Write("Enter password: "******"Passsword Correct! =)");
                }
                else
                {
                    WriteLine("Wrong password. Please try again:");
                }
            }
        }
        static void Main(string[] args)
        {
            WriteLine("You must enter the correct password to decrypt the document.");
            Write("Password: "******"..", "protected-customers.xml");

            if (!File.Exists(xmlFile))
            {
                WriteLine($"{xmlFile} does not exist!");
                return;
            }

            var xmlReader = XmlReader.Create(xmlFile,
                                             new XmlReaderSettings {
                IgnoreWhitespace = true
            });

            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "customer")
                {
                    xmlReader.Read(); // move to <name>
                    string name = xmlReader.ReadElementContentAsString();
                    string creditcardEncrypted = xmlReader.ReadElementContentAsString();
                    string creditcard          = null;
                    string errorMessage        = null;
                    try
                    {
                        creditcard = Protector.Decrypt(creditcardEncrypted, password);
                    }
                    catch (CryptographicException)
                    {
                        errorMessage = $"Failed to decrypt {name}'s credit card.";
                    }
                    string passwordHashed = xmlReader.ReadElementContentAsString();
                    string salt           = xmlReader.ReadElementContentAsString();

                    customers.Add(new Customer
                    {
                        Name       = name,
                        CreditCard = creditcard ?? errorMessage,
                        Password   = passwordHashed,
                        Salt       = salt
                    });
                }
            }

            xmlReader.Close();

            WriteLine();
            int number = 0;

            WriteLine("    {0,-20} {1,-20}",
                      arg0: "Name",
                      arg1: "Credit Card");

            foreach (var customer in customers)
            {
                WriteLine("[{0}] {1,-20} {2,-20}",
                          arg0: number,
                          arg1: customer.Name,
                          arg2: customer.CreditCard);

                number++;
            }
            WriteLine();

            Write("Press the number of a customer to log in as: ");

            string customerName = null;

            try
            {
                number       = int.Parse(ReadKey().KeyChar.ToString());
                customerName = customers[number].Name;
            }
            catch
            {
                WriteLine();
                WriteLine("Not a valid customer selection.");
                return;
            }

            WriteLine();
            Write($"Enter {customerName}'s password: "******"Correct!");
            }
            else
            {
                WriteLine("Wrong!");
            }
        }