왜 SHA1으로 데이터베이스에 암호를 저장하지, 다음 클라이언트 지정된 키에 HMAC를 사용 통신을 위해서?
서버가 임의의 키를 생성하여 로그인 요청과 함께 보내도록하십시오. 클라이언트는 암호의 SHA1 해시를 계산 한 다음 서버 지정 키를 사용하여 해시의 HMAC SHA1 해시를 계산합니다. 그런 다음 서버는 결과가 올바른지 검증합니다. 클라이언트 측에
: 서버 측에
// password is the plaintext password
// keyb64 is a random key specified by the server, encoded in base64.
string ComputeSecureHash(string password, string keyb64)
{
byte[] data = Encoding.UTF8.GetBytes(password);
byte[] key = Convert.FromBase64String(keyb64);
byte[] hash;
byte[] machash;
// compute a plain SHA1 hash of the specified data
using (SHA1Managed sha1 = new SHA1Managed())
{
hash = sha1.ComputeHash(data);
}
// now compute a HMAC hash of that hash, using the random key.
using (HMACSHA1 sha1mac = new HMACSHA1(key))
{
machash = sha1mac.ComputeHash(hash);
}
return Convert.ToBase64String(machash);
}
는 :
// hash is the string produced by the function above
// realHash is the SHA1 hash of the real password, which you've fetched from the db
// key is the key you generated for this login session
bool VerifyHash(string hash, byte[] realHash, byte[] key)
{
byte[] machash;
using (HMACSHA1 sha1mac = new HMACSHA1(key))
{
machash = sha1mac.ComputeHash(realHash);
}
return (Convert.ToBase64String(machash) == hash);
}
이것은 당신이 금이 암호를하지 않고 일반 텍스트 매체를 통해 인증 할 수 있습니다.
감사합니다. 이러한 문제를 해결하는 방법에 대한 제안 사항이 있으십니까? – Aaron
처음에는 간단한 테스트 데이터를 선택 했으므로이를 얻고보다 복잡한 상황으로 이동하십시오. 예 : 처음에는 모든 0, CBC, 패딩없이 정확히 한 블록 크기의 데이터를 iv로 선택합니다. 그것이 작동 할 때 더 많은 복잡성이 추가됩니다. 어쨌든, 당신이 진짜 보안을 원하는 경우 노련한 전문가가 코드를 검토하도록하십시오. 보안이 쉽지 않습니다. 암호화 부분은 쉬운 부분입니다. 또는 SSL을 사용하십시오. 오, 도움이된다면 대답을 받아들입니다. – zaph