我使用散列密码和salt(用户名).
问题是c#的散列值不等于我通过Tsql脚本添加到数据库的初始值.
Tsql:
UPDATE [Users] SET Password = HASHBYTES('SHA2_256','test123'+UPPER([UserName])) GO;
C#:
var passBytes = new UnicodeEncoding().GetBytes(pass); var saltBytes = new UnicodeEncoding().GetBytes(userName.ToUpper()); var dataToHash = new byte[passBytes.Length + saltBytes.Length]; Array.Copy(passBytes,dataToHash,passBytes.Length); Array.Copy(saltBytes,saltBytes.Length); var sha = new SHA256Managed(); return sha.ComputeHash(dataToHash);
我想这与编码有关.
但我不知道如何解决这个问题.
UserName是varchar(50)
DB是现有的,因此更改varchar并不容易.
我已经尝试过:
UPDATE [Users] SET Password = HASHBYTES('SHA2_256',N'test123'+UPPER([UserName])) GO;