I managed to process the hash, but the salt part is still the problem .. I searched and tested the examples without success. This is my hash code:
[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;
}
Registration:
[HttpPost]
public ActionResult Register(User user) {
if (ModelState.IsValid) {
var u = new User {
UserName = user.UserName,
Password = HashPass(user.Password)
};
db.Users.Add(u);
db.SaveChanges();
return RedirectToAction("Login");
}
}return View();
}
Input:
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");
}
}
}return View(u);
}
While the hash is working ... But how do I salt the work here?
I would rather demonstrate my code as it is very difficult for me to understand the words.
First I use a database.
source
share