3

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

    public void AddStudent(Student student)
    {

        student.StudentID = (++eCount).ToString();
        byte[] passwordHash = Hash(student.Password, GenerateSalt());

        student.TimeAdded = DateTime.Now;
        students.Add(student);
    }

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

4

2 回答 2

3

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

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);
}
于 2012-04-21T22:51:47.793 回答
1

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

[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;

    // ...
于 2012-04-21T22:55:40.817 回答