I am trying to learn cryptography with storing passwords in a database using hashing and salting, so I decided to make a login system trying to implement this system.
My database consists of
- UserID int PK
- Varchar Username (250)
- Salt varbinary (64)
- Password varbinary (64)
- Regdate datetime
- Email varchar (250)
I use PBKDF2 , but it looks like this is not a hash / salting method, what if it is not?
If so, am I doing it right?
My keys
private const int SALT_SIZE = 64; private const int KEY_SIZE = 64;
Insert data into a database
public static void RegisterMe(string _username, string _password, string _email) { using (var cn = new SqlConnection(User.strcon)) { string _sqlins = @" INSERT INTO [User] ([Username],[Salt],[Password],[RegDate], [Email]) VALUES (@Username, @Salt, @Password, CURRENT_TIMESTAMP, @Email)"; var cmd = new SqlCommand(_sqlins, cn); cn.Open(); using (var deriveBytes = new Rfc2898DeriveBytes(_password, SALT_SIZE)) { byte[] salt = deriveBytes.Salt; byte[] key = deriveBytes.GetBytes(KEY_SIZE);
User Validation
public bool IsValid(string _email, string _password) { using (var cn = new SqlConnection(strcon)) { byte[] salt = { }, key = { }; string _sql = @" SELECT SALT, [Password], UserID FROM [User] WHERE [Email] = @email"; SqlCommand cmd = new SqlCommand(_sql, cn); cmd.Parameters.AddWithValue("@email", _email); cn.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { salt = reader.GetSqlBytes(0).Value; key = reader.GetSqlBytes(1).Value; reader.Dispose(); cmd.Dispose(); using (var deriveBytes = new Rfc2898DeriveBytes(_password, salt)) { byte[] newKey = deriveBytes.GetBytes(KEY_SIZE);
My system works, it sets the data to the database in bytes, and if the user enters the correct password, it returns true. But is that right? Is it even hashing / salting?