Created
September 1, 2021 03:45
-
-
Save shashank-shekhar/8c1b90d39f9264a4097738afee6aa312 to your computer and use it in GitHub Desktop.
Hashing and Salting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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