Skip to content

Instantly share code, notes, and snippets.

@Twipped
Created September 16, 2011 01:09
Show Gist options
  • Save Twipped/1220934 to your computer and use it in GitHub Desktop.
Save Twipped/1220934 to your computer and use it in GitHub Desktop.
<?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