Skip to content

Instantly share code, notes, and snippets.

@tomasjurasek
Last active April 21, 2016 19:07
Show Gist options
  • Save tomasjurasek/790a67f5de7fc66d7e71 to your computer and use it in GitHub Desktop.
Save tomasjurasek/790a67f5de7fc66d7e71 to your computer and use it in GitHub Desktop.
Create hash with salt
public class Hashing
{
private readonly int SALT_SIZE = 16;
private readonly int HASH_SIZE = 32;
private byte[] salt;
private byte[] newSalt;
public string HashWithSalt(string passwordPlainText, string Username)
{
byte[] key;
CreateSalt();
salt = newSalt;
using (var derive = new Rfc2898DeriveBytes(passwordPlainText, salt))
{
key = derive.GetBytes(HASH_SIZE);
}
passHash = key;
return Convert.ToBase64String(key);
}
private void CreateSalt()
{
newSalt = new byte[SALT_SIZE];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(newSalt);
}
public string isValid(string password)
{
using (var derive = new Rfc2898DeriveBytes(password,salt))
{
byte[] newKey = derive.GetBytes(HASH_SIZE);
if (!newKey.SequenceEqual(passHash))
return "INVALID";
}
return "VALID";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment