Tuesday, April 24, 2012

Byte to string for hash function?

How can I convert passwordHash to string?



    protected RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();

public byte[] GenerateSalt()
{
byte[] salt = new byte[10];
random.GetNonZeroBytes(salt);
return salt;
}

public static byte[] Hash(string value, byte[] salt)
{
return Hash(Encoding.UTF8.GetBytes(value), salt);
}

public static byte[] Hash(byte[] value, byte[] salt)
{
byte[] saltedValue = value.Concat(salt).ToArray();

return new SHA256Managed().ComputeHash(saltedValue);
}

public void AddStudent(Student student)
{
student.StudentID = (++eCount).ToString();
byte[] passwordHash = Hash(student.Password, GenerateSalt());
student.Password = passwordHash; //this line?
student.TimeAdded = DateTime.Now;
students.Add(student);
}


If I try:



    public void AddStudent(Student student)
{
student.StudentID = (++eCount).ToString();
byte[] passwordHash = Hash(student.Password, GenerateSalt());
student.Password = Convert.ToString(passwordHash); //this line?
student.TimeAdded = DateTime.Now;
students.Add(student);
}


When I GET my Student collection the password field will say System.Byte[] where as I want to get the hashed/salted password back?





No comments:

Post a Comment