Skip to content

Instantly share code, notes, and snippets.

@jrgcubano
Created January 5, 2015 08:58
Show Gist options
  • Save jrgcubano/a42923c0bbb41bf2b737 to your computer and use it in GitHub Desktop.
Save jrgcubano/a42923c0bbb41bf2b737 to your computer and use it in GitHub Desktop.
PasswordHelper (create hashed password with salt and verify on access)
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