Created
July 15, 2013 20:48
-
-
Save nikodemrafalski/6003329 to your computer and use it in GitHub Desktop.
Password hashing
This file contains hidden or 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
| public static byte[] CreateRandomSalt(int size = 16) | |
| { | |
| var cryptoProvider = new RNGCryptoServiceProvider(); | |
| var salt = new byte[size]; | |
| cryptoProvider.GetBytes(salt); | |
| return salt; | |
| } | |
| public static string HashPassword(string plainText, byte[] salt) | |
| { | |
| var hashAlgo = new SHA512Managed(); | |
| byte[] plainPassword = Encoding.UTF8.GetBytes(plainText); | |
| var saltedPassword = new byte[plainPassword.Length + salt.Length]; | |
| Buffer.BlockCopy(plainPassword, 0, saltedPassword, 0, plainPassword.Length); | |
| Buffer.BlockCopy(salt, 0, saltedPassword, plainPassword.Length, salt.Length); | |
| byte[] passwordHash = hashAlgo.ComputeHash(saltedPassword); | |
| return Convert.ToBase64String(passwordHash); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment