Created
September 16, 2011 01:09
-
-
Save Twipped/1220934 to your computer and use it in GitHub Desktop.
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 SaltedHash { | |
protected $salt; | |
protected $active_hash; | |
const SALT_LENGTH = 15; | |
function __construct($hash=null) { | |
$this->active_hash = $hash; | |
if ($hash) $this->salt = substr($salt, 0, self::SALT_LENGTH); | |
else $this->salt = str_pad('', self::SALT_LENGTH, sha1(uniqid(rand(), true))); | |
} | |
function hash($password) { | |
return $this->salt . sha1("{$this->salt}{$password}"); | |
} | |
function compare($password) { | |
if (!$this->hash) return false; | |
return $this->hash($password) === $this->active_hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment