Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (33 sloc) 0.834 kB
<?php
/**
* Primal SaltedHash
*
* Data model for hashing passwords with a blowfish salt.
*
* @package Primal
* @author Jarvis Badgley
* @copyright 2008 - 2012 Jarvis Badgley
*/
namespace Primal;
class SaltedHash {
protected $salt;
protected $active_hash;
function __construct($hash=null, $cost='10') {
$this->active_hash = $hash;
if ($hash) $this->salt = substr($hash, 0, 29);
else $this->salt = '$2a$'.$cost.'$'.substr(hash('sha256', uniqid(mt_rand(), true)), 0, 22);
}
function hash($password) {
return crypt($password, $this->salt);
}
function compare($password) {
if (!$this->active_hash) return false;
return $this->hash($password) === $this->active_hash;
}
}
/*
$sh = new SaltedHash();
$hash = $sh->hash('admin');
$sh = new SaltedHash($hash);
var_dump($hash, $sh->compare('admin'));
*/
Jump to Line
Something went wrong with that request. Please try again.