Skip to content

Commit

Permalink
Modified Bonafide configuration to specify the prefix as the mechanis…
Browse files Browse the repository at this point in the history
…m key, removed Bonafide_Mechanism::$prefix
  • Loading branch information
Woody Gilk committed Jan 21, 2011
1 parent c1769e6 commit 34eb135
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
11 changes: 7 additions & 4 deletions classes/bonafide/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __construct(array $config = array())

if (isset($this->config['mechanisms']))
{
foreach ($this->config['mechanisms'] as $data)
foreach ($this->config['mechanisms'] as $prefix => $data)
{
if (isset($data[1]))
{
Expand Down Expand Up @@ -132,7 +132,7 @@ public function __construct(array $config = array())
}

// Register the mechanism by its prefix
$this->mechanisms[$mechanism->prefix] = $mechanism;
$this->mechanisms[$prefix] = $mechanism;
}
}
}
Expand All @@ -147,9 +147,9 @@ public function __construct(array $config = array())
*/
public function hash($password, $salt = NULL, $iterations = NULL)
{
$mechanism = current($this->mechanisms);
$prefix = key($this->mechanisms);

return $mechanism->hash($password, $salt, $iterations);
return $prefix.$this->mechanisms[$prefix]->hash($password, $salt, $iterations);
}

/**
Expand Down Expand Up @@ -187,6 +187,9 @@ public function check($password, $hash, $salt = NULL, $iterations = NULL)
));
}

// Remove the prefix from the hash
$hash = substr($hash, strlen($prefix));

// Check the password using this password hash mechanism
return $this->mechanisms[$prefix]->check($password, $hash, $salt, $iterations);
}
Expand Down
7 changes: 1 addition & 6 deletions classes/bonafide/mechanism.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
*/
abstract class Bonafide_Mechanism {

/**
* @param string unique hash prefix
*/
public $prefix;

/**
* Applies configuration variables to the current mechanism.
*
Expand Down Expand Up @@ -76,7 +71,7 @@ public function hash($password, $salt = NULL, $iterations = NULL)
}
while(--$iterations > 0);

return $this->prefix.$password;
return $password;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions classes/bonafide/mechanism/bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function hash($password, $salt = NULL, $iterations = NULL)
$salt = '$2a$'.$cost.'$'.$salt.'$';
}

return $this->prefix.$this->_hash($password, $salt);
return $this->_hash($password, $salt);
}

protected function _hash($input, $salt = NULL)
Expand All @@ -56,13 +56,10 @@ protected function _hash($input, $salt = NULL)

public function check($password, $hash, $salt = NULL, $iterations = NULL)
{
// Prefix may be adding length to the hash
$offset = strlen($this->prefix);

// $2a$ (4) $ (1) 00 (2) $ (1) <salt> (22)
$salt = substr($hash, $offset, 4 + 1 + 2 + 1 + 22 + $offset);
$salt = substr($hash, 0, 4 + 1 + 2 + 1 + 22);

return $hash === $this->hash($password, $salt.'$');
return parent::check($password, $hash, $salt, $iterations);
}

} // End Bonafide_Mechansim_Bcrypt
Expand Down
2 changes: 1 addition & 1 deletion config/bonafide.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'mechanisms' => array(

// Put your mechanisms here! The format is:
// string $id => array(string $mechanism, array $config)
// string $prefix => array(string $mechanism, array $config)

),
),
Expand Down

0 comments on commit 34eb135

Please sign in to comment.