- 論壇徽章:
- 0
|
本帖最后由 yanyangtian4502 于 2012-01-04 11:05 編輯
我給段代碼出來吧:
因為對C++與php知之甚少,所以我就用C#代碼寫出來,熱心的朋友轉(zhuǎn)為其他代碼,請發(fā)上來吧,多謝啊!
搞個圖片看看把,看著好一點:
QQ截圖20120104110641.png (67.24 KB, 下載次數(shù): 51)
下載附件
salt代碼
2012-01-04 11:03 上傳
代碼也放:(兄弟們,積極參與啊!我喝口水去,搞了一上午了)
private bool IsPasswordValid(string password, byte[] savedSalt, byte[] savedHash)
{
// 將字符串密碼轉(zhuǎn)為字節(jié)
byte[] clearTextBytes = Encoding.UTF8.GetBytes(password);
// 創(chuàng)建一個字節(jié)數(shù)組包括明文密碼和salt的值
byte[] clearTextWithSaltBytes =
new byte[clearTextBytes.Length + saltBytes.Length];
// 把將明文密碼的字節(jié)copy到新的字節(jié)數(shù)組中
for (int i = 0; i < clearTextBytes.Length; i++)
clearTextWithSaltBytes = clearTextBytes;
// 將salt值的字節(jié)數(shù)組也放在新的字節(jié)數(shù)組中
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值與數(shù)據(jù)庫中保存的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);
}
|
|