Created
May 5, 2016 20:44
-
-
Save coreywelch/e990f94dd1276f1082c13bf8a3f26df7 to your computer and use it in GitHub Desktop.
Password Hashing Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Password | |
{ | |
private $options = ['cost' => 10]; | |
public function make($password) | |
{ | |
$hash = password_hash($password, PASSWORD_BCRYPT, $options); | |
if ($hash === false) { | |
throw new Exception('Bcrypt hashing not supported.'); | |
} | |
return $hash; | |
} | |
public function check($password, $hash) | |
{ | |
if (strlen($hash) === 0) { | |
return false; | |
} | |
if (password_verify($password, $hash)) { | |
if (password_needs_rehash($hash, PASSWORD_BCRYPT, $options)) { | |
return $this->make($password); | |
} | |
return true; | |
} | |
} | |
public function set_option($options) { | |
$this->options = $options; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment