C# 手动转换密码和Salt以与ASPNETDB密码进行比较
我正在尝试手动比较aspnetdb password字段与我端的手动哈希密码,以检查其有效性我无法使用默认成员身份实现,因为我使用的是自定义成员身份实现。无论如何,我从记录中获取Base64密码salt,并使用以下算法获取salt哈希:C# 手动转换密码和Salt以与ASPNETDB密码进行比较,c#,asp.net,.net,hash,C#,Asp.net,.net,Hash,我正在尝试手动比较aspnetdb password字段与我端的手动哈希密码,以检查其有效性我无法使用默认成员身份实现,因为我使用的是自定义成员身份实现。无论如何,我从记录中获取Base64密码salt,并使用以下算法获取salt哈希: static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt) { HashAlgorithm algorithm = new SHA256Managed(); byte[] plainTextW
static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt)
{
HashAlgorithm algorithm = new SHA256Managed();
byte[] plainTextWithSaltBytes =
new byte[plainText.Length + salt.Length];
for (int i = 0; i < plainText.Length; i++)
{
plainTextWithSaltBytes[i] = plainText[i];
}
for (int i = 0; i < salt.Length; i++)
{
plainTextWithSaltBytes[plainText.Length + i] = salt[i];
}
byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);
return hash;
}
然后取这些字节并与Convert.GetBase64BytesMembershipUser.Password进行比较。虽然从Convert方法得到的字节和计算出的散列永远不会相同,但在某个地方有一个断开连接,即使我知道密码是相同的
了解我哪里出错了吗?查看SqlMembershipProvider的源代码时,他们似乎在密码之前复制了salt:
static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt)
{
HashAlgorithm algorithm = new SHA256Managed();
byte[] plainTextWithSaltBytes = new byte[plainText.Length + salt.Length];
salt.CopyTo(plainTextWithSaltBytes, 0);
plainText.CopyTo(plainTextWithSaltBytes, salt.Length);
byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);
return hash;
}
通过使用Array.CopyTo对两个字节的数组进行concat,您可以保存一些代码和可能的错误:byte[]plainTextWithSaltBytes=newbyte[plainText.Length+salt.Length];plainText.CopyToplainTextWithSaltBytes,0;salt.CopyToplainTextWithSaltBytes,纯文本.Length;