그래서 저는 bcrypt를 실험하고있었습니다. 나는 아래에 나와있는 클래스를 가지고있다.이 클래스는 3 개의 함수가있다. 첫 번째는 무작위 소금을 생성하는 것이며 두 번째는 생성 된 소금을 사용하여 해시를 생성하고 두 번째는 해시 된 암호와 비교하여 제공된 암호를 확인하는 것입니다. 내가 예를 들어 '비밀번호'와 해시를 생성하는 경우bcrypt와 무작위로 생성 된 소금
<?php
/* Bcrypt Example */
class bcrypt {
private $rounds;
public function __construct($rounds = 12) {
if(CRYPT_BLOWFISH != 1) {
throw new Exception("Bcrypt is not supported on this server, please see the following to learn more: http://php.net/crypt");
}
$this->rounds = $rounds;
}
/* Gen Salt */
public function genSalt() {
/* openssl_random_pseudo_bytes(16) Fallback */
$seed = '';
for($i = 0; $i < 16; $i++) {
$seed .= chr(mt_rand(0, 255));
}
/* GenSalt */
$salt = substr(strtr(base64_encode($seed), '+', '.'), 0, 22);
/* Return */
return $salt;
}
/* Gen Hash */
public function genHash($password) {
/* Explain '$2y$' . $this->rounds . '$' */
/* 2a selects bcrypt algorithm */
/* $this->rounds is the workload factor */
/* GenHash */
$hash = crypt($password, '$2y$' . $this->rounds . '$' . $this->genSalt());
/* Return */
return $hash;
}
/* Verify Password */
public function verify($password, $existingHash) {
/* Hash new password with old hash */
$hash = crypt($password, $existingHash);
/* Do Hashs match? */
if($hash === $existingHash) {
return true;
} else {
return false;
}
}
}
/* Next the Usage */
/* Start Instance */
$bcrypt = new bcrypt(12);
/* Two create a Hash you do */
echo 'Bcrypt Password: ' . $bcrypt->genHash('password');
/* Two verify a hash you do */
$HashFromDB = $bcrypt->genHash('password'); /* This is an example you would draw the hash from your db */
echo 'Verify Password: ' . $bcrypt->verify('password', $HashFromDB);
?>
지금, 나는 randmonly했다 해시 암호, 소금을 생성 얻을. 다음으로 'password'를 다시 입력하고 verify 함수를 사용하면 암호가 일치한다는 의미가됩니다. 잘못된 비밀번호를 입력하면 허위로 표시됩니다. 내 질문은 이것이 어떻게 가능한가? 무작위로 생성 된 소금은 어떨까요? 어떻게 그런 효과가없는거야?
PHP v5.5 (곧 출시 예정)에는이 코드의 필요성을 제거하는'password_xxx()'함수 세트가 있습니다. PHP 5.5 암호 기능을 5.3 또는 5.4로 구현하는 역 호환 라이브러리도 다운로드 할 수 있습니다. 그것을 시도해 볼 수도 있습니다. 그것은 당신에게 많은 일을 덜어 줄 것입니다. https://github.com/ircmaxell/password_compat – Spudley