Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 42 lines (33 sloc) 0.834 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?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'));
*/
Something went wrong with that request. Please try again.