I have a question regarding the validate a hashed text in the database. Right now, UserType that has been registered in the database is hashed like the image below:
I want it to when there is an Administrator text that is stored as hash in the database, the program will validate the hash and check if the validation is an Administrator text. If yes, then it will simply remove the Administrator text in the combo box list.
The reason why I stored the UserType to the database by Hash, is I am restrict the other UserType to gain full access to the system and to prevent the hacker to change the UserType to the other than Administrator (if I store it by the plain text only) in the database once they found it.
The image above shows that UserType has been hashed, and the text that is hashed is Administrator for the UserType.
Right now, I am add and store the hashed UserType to the database like this:
public static void AddDatabase(string _selectedIndex)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string query = "INSERT INTO [Member] ([UserType]) VALUES (@UserType)";
connection.Open();
_hashedType = BCrypt.CreateHash(_selectedIndex, BCrypt.GenerateSalt(12));
using (OleDbCommand command = new OleDbCommand(query, connection))
{
command.Parameters.Add("@UserType", OleDbType.VarChar);
command.Parameters["@UserType"].Value = _hashedType;
command.ExecuteNonQuery();
}
connection.Close();
}
}
void button2_Click(object sender, EventArgs e)
{
SystemManager.AddDatabase(this.comboBox1.Text);
}
void Registration_Load(object sender, EventArgs e)
{
if (SystemManager.CheckType("Administrator") != true)
{
this.comboBox1.Items.Add("Administrator");
}
this.comboBox1.Items.Add("Member");
this.comboBox1.SelectedIndex = 0;
}
To validate it, I am using it like the code below. But, once I run the program, everything become empty (the combo box drop down list not shows anything (empty text) like image below):
public static bool CheckType(string _value1)
{
using (OleDbConnection connection = new OleDbConnection(SystemManager.connectionString))
{
string query = "SELECT COUNT(*) FROM [Member] WHERE [UserType] = @UserType";
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
{
command.Parameters.Add("@UserType", OleDbType.VarChar);
command.Parameters["@UserType"].Value = BCrypt.ValidateHash(_value1, _hashedType);
_count = (int)command.ExecuteScalar();
connection.Close();
}
}
return _count > 0;
}
Thank you very much!
I really appreciate your answer