I would like to hash passwords using PBKDF2 with a pepper and a salt in C#. I am a bit new to Cryptography, so feel free to correct me if I am wrong.
I use the Rfc2898DeriveBytes Class because (according to other Stackoverflow users) bcrypt and other hash algorithms aren't natively supported and verified in C#, so it could pose a security threat. The purpose of this post isn't to start a discussing about which hashing algorithm is the best. > Bcrypt in C# Stackoverflow
My goal: Every password will get a random salt and pepper, the password will be hashed with a certain amount of iterations.
My question: Is it bad to have a bigger input size compared to the desired hash size and is my implementation correct?
- Example: (PasswordInput (?) + Pepper (16 Bytes) + Salt (16 Bytes) > HashOutput (20 Bytes)
My code
public class GenerateHash
{
//Fields
private const int saltSize = 16;
private const int hashSize = 16;
private const int iterations = 10000;
private const string secretPepper = "Secret 16 Byte pepper.";
//Properties
private string inputId { get; set; }
//Methods
public byte[] GeneratePBKDF2String(string inputId, string secretPepper, int saltSize, int
hashSize, int iterations)
{
// Generate a random salt.
RNGCryptoServiceProvider cryptographicServiceProvider = new RNGCryptoServiceProvider();
byte[] salt = new byte[saltSize];
provider.GetBytes(salt);
// Generate a salted hash with pepper.
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(inputId + secretPepper, salt, iterations);
return pbkdf2.GetBytes(hashSize);
}
}
I understand that:
- A hash isn't reversible.
- A salt and pepper are added to increase security and prevent rainbow table attacks.
- A salt is a unique and random string, it doesn't have to be secret and can be stored alongside the hash in a database.
- A pepper is not unique and it is used for every hash. It is a secret and it isn't stored in the database.
- At least a 128-bit (16 bytes > 16 characters) should be used for the salt and pepper.
- At least 10.000 iterations should be used for the algorithm.
Research: Microsoft Rfc2898DeriveBytes, Example Code