Skip to content

Instantly share code, notes, and snippets.

@shashank-shekhar
Created September 1, 2021 03:45
Show Gist options
  • Save shashank-shekhar/8c1b90d39f9264a4097738afee6aa312 to your computer and use it in GitHub Desktop.
Save shashank-shekhar/8c1b90d39f9264a4097738afee6aa312 to your computer and use it in GitHub Desktop.
Hashing and Salting
private const int SALT_SIZE = 24; // size in bytes
private const int HASH_SIZE = 24; // size in bytes
private const int ITERATIONS = 100000; // number of pbkdf2 iterations
private static string CreateHash(string input, string salt)
{
// Generate a salt
var saltBytes = Convert.FromBase64String(salt);
// Generate the hash
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(input, saltBytes, ITERATIONS);
return Convert.ToBase64String(pbkdf2.GetBytes(HASH_SIZE));
}
private static string GenerateSalt()
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
byte[] salt = new byte[SALT_SIZE];
provider.GetBytes(salt);
return Convert.ToBase64String(salt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment