I managed to get hash working, but the salt-part is still an issue.. I've been searching and testing examples without success. This is my code with hash:
[Required]
[StringLength(MAX, MinimumLength = 3, ErrorMessage = "min 3, max 50 letters")]
public string Password { get; set; }
public string Salt { get; set; }
Hash password function(without salt):
public string HashPass(string password) {
byte[] encodedPassword = new UTF8Encoding().GetBytes(password);
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword);
string encoded = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
return encoded;//returns hashed version of password
}
Register:
[HttpPost]
public ActionResult Register(User user) {
if (ModelState.IsValid) {
var u = new User {
UserName = user.UserName,
Password = HashPass(user.Password)//calling hash-method
};
db.Users.Add(u);
db.SaveChanges();
return RedirectToAction("Login");
}
}return View();
}
Login:
public ActionResult Login() {
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User u) {
if (ModelState.IsValid)
{
using (UserEntities db = new UserEntities()) {
string readHash = HashPass(u.Password);
var v = db.Users.Where(a => a.UserName.Equals(u.UserName) &&
a.Password.Equals(readHash)).FirstOrDefault();
if (v != null) {
return RedirectToAction("Index", "Home"); //after login
}
}
}return View(u);
}
So far hash work.. But how do I make salt work here?
I would prefer a demonstrate on my code as I find it very hard to understand by words.
I'm using database first.