0

aspnetdbパスワードフィールドを手動でハッシュされたパスワードと手動で比較して、有効性を確認しようとしています(カスタムのメンバーシップ実装を使用しているため、デフォルトのメンバーシップ実装を使用できません)。とにかく、レコードからBase64パスワードソルトを取得し、次のアルゴリズムを使用してソルトハッシュを取得しています。

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メソッドから取得したバイトと計算されたハッシュは、パスワードが同じであることがわかっていても同じになることはありません。

私がどこで間違っているのかについての洞察はありますか?

4

1 に答える 1

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;
}
于 2012-04-23T11:15:47.020 に答える