Skip to content

Instantly share code, notes, and snippets.

@coreywelch
Created May 5, 2016 20:44
Show Gist options
  • Save coreywelch/e990f94dd1276f1082c13bf8a3f26df7 to your computer and use it in GitHub Desktop.
Save coreywelch/e990f94dd1276f1082c13bf8a3f26df7 to your computer and use it in GitHub Desktop.
Password Hashing Class
<?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