2012-04-23 127 views
0

我想手動比較aspnetdb密碼字段和手動哈希密碼在我的一端檢查有效性(我不能使用默認的成員身份執行,因爲我'使用自定義的一個)。無論如何,我走的是Base64編碼的密碼鹽從記錄中,並使用下面的算法得到的鹽漬哈希:手動轉換密碼和鹽以與ASPNETDB密碼比較

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.GetBase64Bytes(MembershipUser.Password)進行比較。儘管我知道密碼是相同的,但是我從Convert方法獲得的字節和我的計算出來的散列從來都不相同。

任何有關我要去哪裏的信息都是錯誤的?

+0

您可以保存自己的一些代碼和可能的錯誤使用'Array.CopyTo'到Concat的了兩個字節數組: byte [] plainTextWithSaltBytes = new byte [plainText.Length + salt.Length]; plainText.CopyTo(plainTextWithSaltBytes,0); salt.CopyTo(plainTextWithSaltBytes,plainText.Length); – HackedByChinese 2012-04-23 11:04:59

回答

0

中望源SqlMembershipProvider的,它似乎是複製鹽之前的密碼:

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