Hash: fix issues with the mode

This commit is contained in:
terrafrost 2019-03-09 17:08:59 -06:00
parent ca76d3913f
commit 84e2329249

View File

@ -153,6 +153,15 @@ class Crypt_Hash
*/ */
var $ipad; var $ipad;
/**
* Engine
*
* @see self::setHash()
* @var string
* @access private
*/
var $engine;
/** /**
* Default Constructor. * Default Constructor.
* *
@ -228,7 +237,7 @@ class Crypt_Hash
return; return;
} }
switch ($mode) { switch ($this->engine) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
$this->computedKey = mhash($this->hash, $this->key); $this->computedKey = mhash($this->hash, $this->key);
break; break;
@ -308,18 +317,18 @@ class Crypt_Hash
switch ($hash) { switch ($hash) {
case 'md2': case 'md2':
$mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_HASH && in_array('md2', hash_algos()) ? $this->engine = CRYPT_HASH_MODE == CRYPT_HASH_MODE_HASH && in_array('md2', hash_algos()) ?
CRYPT_HASH_MODE_HASH : CRYPT_HASH_MODE_INTERNAL; CRYPT_HASH_MODE_HASH : CRYPT_HASH_MODE_INTERNAL;
break; break;
case 'sha384': case 'sha384':
case 'sha512': case 'sha512':
$mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; $this->engine = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
break; break;
default: default:
$mode = CRYPT_HASH_MODE; $this->engine = CRYPT_HASH_MODE;
} }
switch ($mode) { switch ($this->engine) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
switch ($hash) { switch ($hash) {
case 'md5': case 'md5':
@ -387,10 +396,8 @@ class Crypt_Hash
*/ */
function hash($text) function hash($text)
{ {
$mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
if (!empty($this->key) || is_string($this->key)) { if (!empty($this->key) || is_string($this->key)) {
switch ($mode) { switch ($this->engine) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text, $this->computedKey); $output = mhash($this->hash, $text, $this->computedKey);
break; break;
@ -407,7 +414,7 @@ class Crypt_Hash
$output = call_user_func($this->hash, $output); // step 7 $output = call_user_func($this->hash, $output); // step 7
} }
} else { } else {
switch ($mode) { switch ($this->engine) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text); $output = mhash($this->hash, $text);
break; break;