암호 해시 저장을 시뮬레이션하고 로그인 프로세스에서 유효성을 검사합니다.소금으로 해시 된 암호 저장 및 유효화
나는 hashPassword(String password)
이라는 메서드를 사용하여 문자열 암호를 가져오고 소금 추가로 해시를 반환합니다. 이 두 값이 아닌 이유
what stores in DB: [[email protected]
Users hashed password: [[email protected]
Incorrect
:
나는 소금에게 정적 값을 선택하고이 예제에서는 암호 (hello123
)
public class T1 {
public static void main(String[] args) {
String userDefinedPassword = "hello123";
String hashedPassToStoreInDB = String.valueOf(hashPassword(userDefinedPassword));
System.out.println("what stores in DB: " + hashedPassToStoreInDB);
// store in database
//Password Verify
String inputPassword = "hello123";
String hashedInputPassword = String.valueOf(hashPassword(inputPassword));
System.out.println("Users hashed password: " + hashedInputPassword);
if (hashedPassToStoreInDB.equals(hashedInputPassword)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
private static byte[] hashPassword(String password) {
byte[] salt = new byte[16];
byte[] hash = null;
for (int i = 0; i < 16; i++) {
salt[i] = (byte) i;
}
try {
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
hash = f.generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException nsale) {
nsale.printStackTrace();
} catch (InvalidKeySpecException ikse) {
ikse.printStackTrace();
}
return hash;
}
}
에 대한 동일한 값을 선택하지만 결과는 같은?
내 코드가 잘못되었습니다.
: 당신은 문자열 대신 문자 배열로 암호를 처리해야 그런 다음, 단지 문자열을 결합하여 작업 결과에
hashCode()
를 사용하여 기본 소금을 얻을 수 있습니다 : 참조 [여기] (http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) – jHilscher