refactoring Security::hash(), fixing two broken tests in Security

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7420 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-08-02 14:44:42 +00:00
parent 27125a247b
commit f131b2e441
2 changed files with 10 additions and 19 deletions

View file

@ -119,11 +119,10 @@ class Security extends Object {
*/
function hash($string, $type = null, $salt = false) {
$_this =& Security::getInstance();
$return = null;
if ($salt) {
$string = Configure::read('Security.salt') . $string;
}
if (empty($type)) {
$type = $_this->hashType;
}
@ -133,25 +132,18 @@ class Security extends Object {
if (function_exists('sha1')) {
$return = sha1($string);
return $return;
} else {
$type = 'sha256';
}
$type = 'sha256';
}
if ($type == 'sha256') {
if (function_exists('mhash')) {
$return = bin2hex(mhash(MHASH_SHA256, $string));
return $return;
}
if ($type == 'sha256' && function_exists('mhash')) {
return bin2hex(mhash(MHASH_SHA256, $string));
}
if (function_exists('hash')) {
$return = hash($type, $string);
} else {
$return = md5($string);
return hash($type, $string);
}
return $return;
return md5($string);
}
/**
* Sets the default hash method for the Security object. This affects all objects using

View file

@ -113,13 +113,12 @@ class SecurityTest extends CakeTestCase {
$this->assertIdentical(strlen(Security::hash($key, null, false)), 32);
$this->assertIdentical(strlen(Security::hash($key, null, true)), 32);
if (function_exists('mhash')) {
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
} else {
if (!function_exists('hash') && !function_exists('mhash')) {
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32);
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32);
} else {
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
}
}
/**