My question is that the salt should be random and stored in a database like this, or if I can generate a 10-byte salt and save it in my code and always use the same salt to save myself, keeping the salt in DB and just save the key?
ABSOLUTELY NOT.
You do not understand the purpose of salt, even if you ask this question.
The purpose of the salt is to make it more difficult for an attacker to use a pre-computed hashed shared password table. If the salt is always the same, then the attacker simply pre-computes a hashed shared password table with that salt.
Let me make this clearer. Suppose that an attacker has obtained your password database and sets up an attack on its leisure against all stored hashes in order to determine what is the password corresponding to the hash. If each salt is different, the attacker must establish a new attack against each record in the database. If each salt is the same, then the attacking one user will attack each user.
In addition: suppose you use one salt for each user. Suppose two users have the same password. And suppose the attacker got a password database. Now the attacker knows which two users have the same password , because they have the same salty hash and can make a reasonable assumption that this is the weakest password in the database. An attacker can concentrate his efforts (no matter what may be) on an attack on this user in particular. And as soon as she finds out the user's password, the likelihood that the user used this username and a weak password for other systems can now harm an attacker without the presence of password files.
Good thing you want to know about security; itβs bad that you are trying to write a real password system with your level of understanding. If this is for a real system that should protect real users, use a system created by experts , or hire your own expert . You are going to create a system that cannot be broken, and not a system that an attacker cannot break.
In addition: you ask strangers on the Internet for help with security . Strangers who you have no idea whether they know what they are talking about, or just make themselves think. Get a real security expert (and it's not me - I'm an expert on semantic analyzers). Creating a security system is one of the most difficult programming tasks; you need professional help.
For a brief introduction to the basic password authentication schemes, see my series of articles:
http://blogs.msdn.com/b/ericlippert/archive/tags/salt/
Eric Lippert
source share