Пример #1
0
        public bool addNewUser(string user, string password, string type, String filePath)
        {
            UserLogin = true;
            String password_encrypt = "";

            userName = user;
            Cryptography cryptography = new Cryptography();

            string byte_password = "";

            if (type == "HMAC")
            {
                byte_password = cryptography.GenerateHMACString(password);
                Console.WriteLine("HMAc" + byte_password);
            }
            if (type == "SHA512")
            {
                byte_password = cryptography.EncryptSHA512(password);
            }

            try {
                using (SQLiteConnection connection = new SQLiteConnection(@"DataSource=" + filePath))
                {
                    Console.WriteLine(connection.ConnectionString);
                    SQLiteCommand command = new SQLiteCommand("insert into user (login, password_hash,salt,isPasswordKeptHash) values (@login, @engWord, @spaWord, @frequency)", connection);
                    connection.Open();
                    command.Parameters.AddWithValue("@login", user);
                    command.Parameters.AddWithValue("@engWord", byte_password);
                    command.Parameters.AddWithValue("@spaWord", cryptography.GenerateSalt());
                    command.Parameters.AddWithValue("@frequency", false);
                    command.ExecuteScalar();
                    connection.Close();
                    return(true);

                    /*  Forms forms= new Forms();
                     * forms.Form4Close();*/
                }
            }catch (SqliteException sqlite)
            {
                return(false);
            }
            catch (Exception exc)
            {
                return(false);
            }
        }
Пример #2
0
        public bool ChangeMainPassword(string oldpassword, string newpassword, string type, String filePath = "")
        {
            Cryptography c            = new Cryptography();
            String       hashPassword = "";

            if (type == "HMAC")
            {
                hashPassword = c.GenerateHMACString(newpassword);
            }
            if (type == "SHA512")
            {
                hashPassword = c.EncryptSHA512(newpassword);
            }
            Console.WriteLine("Zmienianie hasło");
            try
            {
                SQLiteConnection connection = new SQLiteConnection(@"DataSource=" + filePath);
                connection.Open();
                Console.WriteLine("Aktualny user" + userName);
                string sql = "UPDATE user SET password_hash = '" + hashPassword + "' WHERE login = '******'";


                SQLiteCommand command = new SQLiteCommand(sql, connection);

                command.ExecuteNonQuery();
                Console.WriteLine(command.CommandText.ToString());
                /* connection.Close();*/
                Console.WriteLine("Zmieniono hasło");
                return(true);
            }catch (SqliteException sqlite)
            {
                return(false);
            }
            catch (Exception exc)
            {
                return(false);
            }
        }
Пример #3
0
        public bool LoginUser(string user, string password, string type, String filePath)
        {
            Cryptography cryptography = new Cryptography();

            using (var sqlite2 = new SQLiteConnection(@"DataSource=" + filePath))
            {
                sqlite2.Open();

                string[] result = new string[5];
                string   sql    = "select * from user where login='******'";
                userName = user;
                SQLiteCommand    command = new SQLiteCommand(sql, sqlite2);
                SQLiteDataReader reader  = command.ExecuteReader();
                String           DBLogin = "";
                while (reader.Read())
                {
                    result[0] = reader[0].ToString();
                    result[1] = reader[1].ToString();
                    result[2] = reader[2].ToString();
                }
                if (type == "HMAC")
                {
                    Console.WriteLine("HMAC");


                    String hashPassword = result[2].ToString();
                    String passHMAC     = cryptography.GenerateHMACString(password);
                    String loginDbUSer  = result[1].ToString();
                    loginCurrentUser = loginDbUSer;
                    Console.WriteLine("Passwords: form: " + passHMAC + "and database:  " + hashPassword);

                    if (String.Equals(hashPassword, passHMAC) && String.Equals(loginDbUSer, user) && user != null && password != null)
                    {
                        UserLogin = true;

                        Console.WriteLine("Logowanie pomyślne HMAC");
                    }
                    else
                    {
                        Console.WriteLine("Logowanie nieudane HMAC");
                        UserLogin = false;
                    }
                }
                else if (type == "SHA512")
                {
                    Console.WriteLine("SHA512");
                    String hashPassword = result[2].ToString();
                    String passSHA      = cryptography.EncryptSHA512(password);
                    String loginDbUSer  = result[1].ToString();
                    loginCurrentUser = loginDbUSer;
                    if (String.Equals(hashPassword, passSHA) && String.Equals(loginDbUSer, user) && user != null && password != null)
                    {
                        UserLogin = true;
                        Console.WriteLine("Logowanie pomyślne SHA512");
                    }
                }
                else
                {
                    Console.WriteLine("Logowanie nie udało się SHA512");
                    UserLogin = false;
                }

                reader.Close();
                command.Dispose();
            }

            return(UserLogin);
        }