user1782698
user1782698

Reputation: 33

How to store hashed password in database

I am trying to store hashed password in sql express database. but I am not able to do that one. Here is my code:

SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();

byte[] encode = sha.ComputeHash(Encoding.ASCII.GetBytes(pass));

string cmd = "insert into tblLogin (username,password,email,state,active) values ('"+name+"',"+encode+",'"+email+"','"+state +"',"+ active + ")" ;

And in database I kept password as varbinary.

Here my problem is I am getting value of encode as System.Byte[] but not hashed value. How can I do this, I tried to find and I am getting how to hash password but not how to store password.

Here my main problem is How can I construct Insert query and store Byte[] into database?

Upvotes: 1

Views: 6980

Answers (2)

abatishchev
abatishchev

Reputation: 100238

var provider = new SHA1CryptoServiceProvider(salt);
byte[] bytes = Encoding.UTF8.GetBytes(input);
string result = Convert.ToBase64String(provider.ComputeHash(bytes)); // store it

Upvotes: 3

Steven Moseley
Steven Moseley

Reputation: 16325

Save it as a varchar, not varbinary.

Upvotes: 3

Related Questions