Last active
April 21, 2016 19:07
-
-
Save tomasjurasek/790a67f5de7fc66d7e71 to your computer and use it in GitHub Desktop.
Create hash with salt
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
public class Hashing | |
{ | |
private readonly int SALT_SIZE = 16; | |
private readonly int HASH_SIZE = 32; | |
private byte[] salt; | |
private byte[] newSalt; | |
public string HashWithSalt(string passwordPlainText, string Username) | |
{ | |
byte[] key; | |
CreateSalt(); | |
salt = newSalt; | |
using (var derive = new Rfc2898DeriveBytes(passwordPlainText, salt)) | |
{ | |
key = derive.GetBytes(HASH_SIZE); | |
} | |
passHash = key; | |
return Convert.ToBase64String(key); | |
} | |
private void CreateSalt() | |
{ | |
newSalt = new byte[SALT_SIZE]; | |
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | |
rng.GetBytes(newSalt); | |
} | |
public string isValid(string password) | |
{ | |
using (var derive = new Rfc2898DeriveBytes(password,salt)) | |
{ | |
byte[] newKey = derive.GetBytes(HASH_SIZE); | |
if (!newKey.SequenceEqual(passHash)) | |
return "INVALID"; | |
} | |
return "VALID"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment