简体   繁体   中英

Store hash value

Is there a way to store the salt for this hash method? I dont know how to do it?

Thanks.

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

This should be along the lines of what you want. Not sure where these Students are being stored, but it will likely need to be changed, too.

[DataMember(Name = "StudentID")]
public string StudentID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "Password")]
public string Password;
[DataMember(Name = "Salt")]
public byte[] Salt;

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)
{
    byte[] salt = GenerateSalt();

    student.StudentID = (++eCount).ToString();
    byte[] passwordHash = Hash(student.Password, salt);
    student.Salt = salt;
    student.Password = Convert.ToBase64String(passwordHash);
    student.TimeAdded = DateTime.Now;
    students.Add(student);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM