Created
January 5, 2015 08:58
-
-
Save jrgcubano/a42923c0bbb41bf2b737 to your computer and use it in GitHub Desktop.
PasswordHelper (create hashed password with salt and verify on access)
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 class PasswordHelper | |
{ | |
public static Byte[] GerenateSalt() | |
{ | |
byte[] saltBytes = new byte[128]; | |
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | |
rng.GetNonZeroBytes(saltBytes); | |
return saltBytes; | |
} | |
public static Byte[] CalculaHashedPassword(String password, Byte[] saltByteArray) | |
{ | |
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider(); | |
byte[] passwordByteArray = System.Text.Encoding.UTF8.GetBytes(password); | |
byte[] bs = new byte[passwordByteArray.Length + saltByteArray.Length]; | |
System.Buffer.BlockCopy(passwordByteArray, 0, bs, 0, passwordByteArray.Length); | |
System.Buffer.BlockCopy(saltByteArray, 0, bs, passwordByteArray.Length, saltByteArray.Length); | |
bs = x.ComputeHash(bs); | |
return bs; | |
} | |
public static Boolean CheckPassword(String password, Byte[] hashedPassword, Byte[] salt) | |
{ | |
return hashedPassword.SequenceEqual(CalculaHashedPassword(password, salt)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment