Click here to Skip to main content
15,886,832 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to insert my hashed password into my database. I am using a BCrypt function to hash the password:

C#
#region Password Hash w/ BCrypt
        private static string GetRandomSalt()
        {
            return BCryptHelper.GenerateSalt(12);
        }

        public static string HashPassword(string password)
        {
            return BCryptHelper.HashPassword(password, GetRandomSalt());
        }

        public static bool ValidatePassword(string password, string correctHash)
        {
            return BCryptHelper.CheckPassword(password, correctHash);
        }
        #endregion



Here is the building of the query parameters:

C#
using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand(insert, conn);

                    // build params
                    cmd.Parameters.AddWithValue("@FirstName", tbxFname.Text.Trim());
                    cmd.Parameters.AddWithValue("@LastName", tbxLname.Text.Trim());
                    cmd.Parameters.AddWithValue("@Email", tbxEmail.Text.Trim());
                    string password = HashPassword(tbxPassword.Text);
                    char[] hashedPassword = password.ToCharArray();
                    cmd.Parameters.AddWithValue("@HashedPassword", hashedPassword);
                    cmd.Parameters.AddWithValue("@Gender", rbnGender.SelectedValue.Trim());
                    cmd.Parameters.AddWithValue("@DateOfBirth", tbxDob.Text.Trim());
                    cmd.Parameters.AddWithValue("@DateCreated", DateTime.Now);

                    // execute
                    cmd.ExecuteNonQuery();
                    //lbl1.Text = "Success";
                }


The datatype I am using in my table is CHAR(60), and the error I am currently returning is:

Insert error: Implicit conversion from data type nvarchar to binary is not allowed. Use the CONVERT function to run this query.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-14 13:31pm    
You should no use nvarchar at all. Essentially, any hashed (encrypted, compressed, etc.) data is binary, the array of bytes.
—SA

1 solution

Try not converting it to a char array - send teh string directly:
C#
string password = HashPassword(tbxPassword.Text);
cmd.Parameters.AddWithValue("@HashedPassword", password);

Normally, I work with binary data for hashes, but since your BCryptHelper.HashPassword method is returning a string, it's probably best to stick with that.

The error message is probably because a .NET char array is treated as a byte array for transfer to SQL - so it assumes you are sending binary data instead of characters. Sending it as a string should fix that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900