- 论坛徽章:
- 0
|
本帖最后由 yanyangtian4502 于 2012-01-04 11:05 编辑
我给段代码出来吧:
因为对C++与php知之甚少,所以我就用C#代码写出来,热心的朋友转为其他代码,请发上来吧,多谢啊!
搞个图片看看把,看着好一点:
代码也放:(兄弟们,积极参与啊!我喝口水去,搞了一上午了)
private bool IsPasswordValid(string password, byte[] savedSalt, byte[] savedHash)
{
// 将字符串密码转为字节
byte[] clearTextBytes = Encoding.UTF8.GetBytes(password);
// 创建一个字节数组包括明文密码和salt的值
byte[] clearTextWithSaltBytes =
new byte[clearTextBytes.Length + saltBytes.Length];
// 把将明文密码的字节copy到新的字节数组中
for (int i = 0; i < clearTextBytes.Length; i++)
clearTextWithSaltBytes = clearTextBytes;
// 将salt值的字节数组也放在新的字节数组中
for (int i = 0; i < saltBytes.Length; i++)
clearTextWithSaltBytes[clearTextBytes.Length + i] = saltBytes;
// 选择一个hash算法
HashAlgorithm hash = new SHA256Managed();
//生成hash值
byte[] currentHash = hash.ComputeHash(clearTextWithSaltBytes);
// 将生成的hash值与数据库中保存的hash值比较
bool matched = false;
if (currentHash.Length == savedHash.Length)
{
int i = 0;
while ((i < currentHash.Length) & &(currentHash == savedHash))
{
i += 1;
}
if (i == currentHash.Length)
{
matched = true;
}
}
return (matched);
}
|
|