数据成员密码未散列

本文关键字:密码 数据成员 | 更新日期: 2023-09-27 18:31:49

我正在尝试在我的数据合同中对我的数据成员密码进行哈希/加盐,但是当我添加一个新学生然后获取该学生集合时,密码字段尚未散列/加盐,它返回为我键入的内容?

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

任何人都可以帮助解决这个问题吗?

数据成员密码未散列

您应该将散列的pasword分配给学生密码,然后添加学生。

public void AddStudent(Student student)
{
    student.StudentID = (++eCount).ToString();
    byte[] passwordHash = Hash(student.Password, GenerateSalt());
    StringBuilder stringBuilder = new StringBuilder();
    foreach(byte b in passwordHash){
        stringBuilder.AppendFormat("{0:X2}", b);
    }
    student.TimeAdded = DateTime.Now;
    student.Password= stringBuilder.ToString();;
    students.Add(student);
}

您可以简单地将其添加到您的原始问题中,但这里有更多代码:

[DataContract(Name="Student")]
public class Student
{
    [DataMember(Name = "StudentID")]
    public string StudentID { get; set; }
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }
    [DataMember(Name = "LastName")]
    public string LastName { get; set; }
    // local non public cache
    private byte[] _password;
    [DataMember(Name = "Password")]
    public byte[] Password {
        get { return _password; }
        set {
            this.Salt = GenerateSalt();
            this._password = Hash(value, this.Salt);
        }
    };
    [DataMember(Name = "Salt")]
    public byte[] Salt;
    // ...