From 84e232924988f27caa7611321a1d2401d6d0c142 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 9 Mar 2019 17:08:59 -0600 Subject: [PATCH] Hash: fix issues with the mode --- phpseclib/Crypt/Hash.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/phpseclib/Crypt/Hash.php b/phpseclib/Crypt/Hash.php index 1042463f..a998b5b2 100644 --- a/phpseclib/Crypt/Hash.php +++ b/phpseclib/Crypt/Hash.php @@ -153,6 +153,15 @@ class Crypt_Hash */ var $ipad; + /** + * Engine + * + * @see self::setHash() + * @var string + * @access private + */ + var $engine; + /** * Default Constructor. * @@ -228,7 +237,7 @@ class Crypt_Hash return; } - switch ($mode) { + switch ($this->engine) { case CRYPT_HASH_MODE_MHASH: $this->computedKey = mhash($this->hash, $this->key); break; @@ -308,18 +317,18 @@ class Crypt_Hash switch ($hash) { 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; break; case 'sha384': 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; default: - $mode = CRYPT_HASH_MODE; + $this->engine = CRYPT_HASH_MODE; } - switch ($mode) { + switch ($this->engine) { case CRYPT_HASH_MODE_MHASH: switch ($hash) { case 'md5': @@ -387,10 +396,8 @@ class Crypt_Hash */ function hash($text) { - $mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; - if (!empty($this->key) || is_string($this->key)) { - switch ($mode) { + switch ($this->engine) { case CRYPT_HASH_MODE_MHASH: $output = mhash($this->hash, $text, $this->computedKey); break; @@ -407,7 +414,7 @@ class Crypt_Hash $output = call_user_func($this->hash, $output); // step 7 } } else { - switch ($mode) { + switch ($this->engine) { case CRYPT_HASH_MODE_MHASH: $output = mhash($this->hash, $text); break;