From 881fbd78eec87f1394ffb76aab5d35a7cc0573a5 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 18 Apr 2020 21:36:42 -0500 Subject: [PATCH] Hash: add __toString() method --- phpseclib/Crypt/Common/AsymmetricKey.php | 2 +- phpseclib/Crypt/Hash.php | 8 ++++++++ phpseclib/Crypt/RSA.php | 2 +- tests/Unit/Crypt/RSA/ModeTest.php | 8 ++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/phpseclib/Crypt/Common/AsymmetricKey.php b/phpseclib/Crypt/Common/AsymmetricKey.php index f3efb733..c4576099 100644 --- a/phpseclib/Crypt/Common/AsymmetricKey.php +++ b/phpseclib/Crypt/Common/AsymmetricKey.php @@ -388,7 +388,7 @@ abstract class AsymmetricKey */ public function getHash() { - return $this->hash->getHash(); + return clone $this->hash; } /** diff --git a/phpseclib/Crypt/Hash.php b/phpseclib/Crypt/Hash.php index e0b9e464..970560e2 100644 --- a/phpseclib/Crypt/Hash.php +++ b/phpseclib/Crypt/Hash.php @@ -1462,4 +1462,12 @@ class Hash return $temp; } + + /** + * __toString() magic method + */ + public function __toString() + { + return $this->getHash(); + } } diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index b1952028..a149711a 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -669,7 +669,7 @@ abstract class RSA extends AsymmetricKey */ public function getMGFHash() { - return $this->mgfHash->getHash(); + return clone $this->mgfHash; } /** diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index 29ea8b83..bc4e12e4 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -170,17 +170,17 @@ HERE; ->withSaltLength(5) ->withMGFHash('sha512'); - $this->assertSame('sha1', $rsa->getHash()); + $this->assertEquals('sha1', $rsa->getHash()); $this->assertSame(5, $rsa->getSaltLength()); - $this->assertSame('sha512', $rsa->getMGFHash()); + $this->assertEquals('sha512', $rsa->getMGFHash()); $rsa = $rsa ->withHash('sha512') ->withSaltLength(6) ->withMGFHash('sha1'); - $this->assertSame('sha512', $rsa->getHash()); + $this->assertEquals('sha512', $rsa->getHash()); $this->assertSame(6, $rsa->getSaltLength()); - $this->assertSame('sha1', $rsa->getMGFHash()); + $this->assertEquals('sha1', $rsa->getMGFHash()); } }