3 * @author Jan Altensen (Stricted)
4 * @copyright 2013-2014 Jan Altensen (Stricted)
5 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 * compare password with given hash
11 * @param string $password
15 public function compare ($password, $hash) {
16 $salt = $this->getSalt($hash);
18 $new = crypt($password, $salt);
19 $doubleSalted = crypt(crypt($password, $salt), $salt);
24 else if ($doubleSalted == $hash) {
34 * @param string $password
35 * @param string $double <optional>
38 public function crypt ($password, $double = false) {
39 $salt = $this->generateSalt();
41 return crypt(crypt($password, $salt), $salt);
44 return crypt($password, $salt);
53 public function generateSalt() {
54 $blowfishCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
55 $maxIndex = strlen($blowfishCharacters) - 1;
58 for ($i = 0; $i < 22; $i++
) {
59 $rand = mt_rand(0, $maxIndex);
60 $salt .= $blowfishCharacters[$rand];
63 return '$2a$08$' . $salt;
67 * get salt from password hash
72 private function getSalt($hash) {
74 if (mb_substr($hash, 0, 7) == '$2a$08$') {
75 $salt = mb_substr($hash, 7, 22);
78 $salt = mb_substr($hash, 0, 22);
81 return '$2a$08$' . $salt;