Moved hash generation into the Security class so it can be accessed outside of the input generation.

This commit is contained in:
Josh Sherman 2010-11-26 02:28:07 -05:00
parent b694ff55c5
commit e120150bca
2 changed files with 43 additions and 27 deletions

View file

@ -84,34 +84,8 @@ class Form extends Object
*/
public function securityInput($value, $salts = null)
{
// Determines which salt(s) to use
if ($salts == null)
{
if (!isset($this->config->security['salt']) || $this->config->security['salt'] == null)
{
$salts = array('P1ck73', 'Ju1C3');
}
else
{
$salts = $this->config->security['salt'];
}
}
// Forces the variable to be an array
if (!is_array($salts))
{
$salts = array($salts);
}
// Loops through the salts, applies them and calculates the hash
$hash = $value;
foreach ($salts as $salt)
{
$hash = sha1($salt . $hash);
}
// Returns the hidden input
return $this->hiddenInput('security_hash', $hash);
return $this->hiddenInput('security_hash', Security::generateHash($value, $salts));
}
/**

View file

@ -36,6 +36,48 @@ class Security
* @var array
*/
private static $cache = array();
/**
* Generate Hash
*
* Generates an SHA1 hash from the provided string. Optionally can be salted.
*
* @param string $value value to hash
* @param mixed $salts optional salt or salts
* @return string SHA1 has
*/
public static function generateHash($value, $salts = null)
{
// Determines which salt(s) to use
if ($salts == null)
{
$config = Config::getInstance();
if (isset($config->security['salt']) && $config->security['salt'] != null)
{
$salts = $config->security['salt'];
}
else
{
$salts = array('P1ck73', 'Ju1C3');
}
}
// Forces the variable to be an array
if (!is_array($salts))
{
$salts = array($salts);
}
// Loops through the salts, applies them and calculates the hash
$hash = $value;
foreach ($salts as $salt)
{
$hash = sha1($salt . $hash);
}
return $hash;
}
/**
* Check Session