私はどちらが良い答えと一緒に大きなフィードバックを得たのかという質問をしました。私は、2つのハッシュの検証チェックで何か間違っていると仮定します。おそらくループ内のコードは問題ありませんが、バイトとbase64などを理解している私のコードは問題ですか?ハッシュパスワードの確認が機能していません
ここに元の質問があります。 Password Hashing - Why salt 60,000 times
問題は、これらのハッシュが一致していないですif (resultHash.Equals(hashPassword))
コード
public string BuildVerify()
{
string password = "";
string salt = "";
byte[] result;
using (var sha256 = SHA256.Create())
{
password = "hovercraft";
// step 1: you can use RNGCryptoServiceProvider for something worth using
var passwordHashing = new PasswordHashing();
salt = passwordHashing.CreateRandomSalt();
// step 2
string hash =
Convert.ToBase64String(sha256.ComputeHash(Encoding.UTF8.GetBytes(salt + password)));
// step 3
result = sha256.ComputeHash(Encoding.UTF8.GetBytes(salt + hash));
// step 4
for (int i = 0; i < 60000; i++)
{
result =
sha256.ComputeHash(Encoding.UTF8.GetBytes(salt + Convert.ToBase64String(result)));
}
}
// TESTING VERIFY this works ..
string SaltAndPwd = string.Concat(password, salt);
SHA256 sha2 = SHA256Managed.Create();
byte[] buff = sha2.ComputeHash(Encoding.Unicode.GetBytes(SaltAndPwd));
string resultHash = Convert.ToBase64String(buff);
string hashPassword = Convert.ToBase64String(result);
if (resultHash.Equals(hashPassword))
{
// perfect
}
return "";
}
public class PasswordHashing
{
public string CreateRandomSalt()
{
string password = "";
password = HashPassword.CreateSalt(8) + "=";
password = password.Replace("/", "c");
return password;
}
}
public static string CreateSalt(int size)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
アップデート - 問題
[OK]を、私は、答えからコードを使用していますが、私のコードを使って検証することができないので、明らかに私の前提は正しくありません。
// This should be stored in your DB for example along with the hash result
var newsalt = SOPasswordHasher.GetSalt();
// We calculate the hash then store the result. Next time you want to authenticate someone
// You'll have to reuse the same salt and recalculate the hash then compare
// the stored hash with the new one
var result = Convert.ToBase64String(SOPasswordHasher.Hash("hovercraft", newsalt));
string SaltAndPwd = string.Concat("hovercraft", newsalt);
SHA256 sha2 = SHA256Managed.Create();
byte[] buff = sha2.ComputeHash(Encoding.Unicode.GetBytes(SaltAndPwd));
string resultHash = Convert.ToBase64String(buff);
if (result.Equals(resultHash))
{
// perfect
}
ループ内のこの行があなたの検証と一致するかどうかは、あなたが言っていることからではありません。結果= sha256.ComputeHash(Encoding.UTF8.GetBytes(salt + Convert.ToBase64String(result))); –
bcryptまたはPBKDFv2を使用してください。これはすべて正しいことです。 – SLaks
@SLaks他の関連する質問から、彼自身の方法を使用することは彼の仕事からの必要条件です。 NIH症候群と思われる。 – Nasreddine