From b63bd5d09c2759c01da1bfa9c6ea90bd43b6dd3b Mon Sep 17 00:00:00 2001 From: Alexandros Weigl Date: Tue, 18 Oct 2016 17:50:54 +0200 Subject: [PATCH 1/2] Added support for sha hash family of algorithms --- src/AppserverIo/Lang/String.php | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/AppserverIo/Lang/String.php b/src/AppserverIo/Lang/String.php index 4d2e4ef..03022f2 100644 --- a/src/AppserverIo/Lang/String.php +++ b/src/AppserverIo/Lang/String.php @@ -413,7 +413,7 @@ public function trim() } /** - * md5 encryptes the string and returns the + * md5 encrypts the string and returns the * instance. * * @return \AppserverIo\Lang\String The instance md5 encrypted @@ -423,6 +423,40 @@ public function md5() return $this->init(md5($this->stringValue())); } + /** + * sha1 encrypts the string and returns the + * instance. + * + * @return \AppserverIo\Lang\String The instance sha1 encrypted + */ + public function sha1($salt = null) + { + return $this->init(hash('sha1', $salt . $this->stringValue())); + } + + /** + * sha256 encrypts the string and returns the + * instance. + * + * @return \AppserverIo\Lang\String The instance sha256 encrypted + */ + public function sha256($salt = null) + { + return $this->init(hash('sha256', $salt . $this->stringValue())); + } + + /** + * sha512 encrypts the string and returns the + * instance. + * + * @return \AppserverIo\Lang\String The instance sha512 encrypted + */ + public function sha512($salt = null) + { + return $this->init(hash('sha512', $salt . $this->stringValue())); + } + + /** * Converts the string value to upper case * and returns the instance. From feed5bb6955e3e330bf029ef9eba42da815c55e5 Mon Sep 17 00:00:00 2001 From: Alexandros Weigl Date: Wed, 19 Oct 2016 13:38:49 +0200 Subject: [PATCH 2/2] Added tests for new hash algorithms - Also added salt parameter to md5 --- src/AppserverIo/Lang/String.php | 12 +++++++-- tests/AppserverIo/Lang/StringTest.php | 39 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/AppserverIo/Lang/String.php b/src/AppserverIo/Lang/String.php index 03022f2..897f989 100644 --- a/src/AppserverIo/Lang/String.php +++ b/src/AppserverIo/Lang/String.php @@ -416,17 +416,21 @@ public function trim() * md5 encrypts the string and returns the * instance. * + * @param string $salt the salt to prepend to the stringValue + * * @return \AppserverIo\Lang\String The instance md5 encrypted */ - public function md5() + public function md5($salt = null) { - return $this->init(md5($this->stringValue())); + return $this->init(md5($salt . $this->stringValue())); } /** * sha1 encrypts the string and returns the * instance. * + * @param string $salt the salt to prepend to the stringValue + * * @return \AppserverIo\Lang\String The instance sha1 encrypted */ public function sha1($salt = null) @@ -438,6 +442,8 @@ public function sha1($salt = null) * sha256 encrypts the string and returns the * instance. * + * @param string $salt the salt to prepend to the stringValue + * * @return \AppserverIo\Lang\String The instance sha256 encrypted */ public function sha256($salt = null) @@ -449,6 +455,8 @@ public function sha256($salt = null) * sha512 encrypts the string and returns the * instance. * + * @param string $salt the salt to prepend to the stringValue + * * @return \AppserverIo\Lang\String The instance sha512 encrypted */ public function sha512($salt = null) diff --git a/tests/AppserverIo/Lang/StringTest.php b/tests/AppserverIo/Lang/StringTest.php index 501cd71..fecb426 100644 --- a/tests/AppserverIo/Lang/StringTest.php +++ b/tests/AppserverIo/Lang/StringTest.php @@ -162,4 +162,43 @@ public function testValueOf() // check that String was successfully concatenated $this->assertTrue($string->equals(new String($string))); } + + /** + * This test checks the String's sha1() method. + * + * @return void + */ + public function testSha1() + { + // initialize a new String instance + $string = new String('Mustermann'); + // check that String's sha1 summs equals + $this->assertTrue($string->sha1()->equals(new String(hash('sha1' , 'Mustermann')))); + } + + /** + * This test checks the String's sha256() method. + * + * @return void + */ + public function testSha256() + { + // initialize a new String instance + $string = new String('Mustermann'); + // check that String's sha256 summs equals + $this->assertTrue($string->sha256()->equals(new String(hash('sha256' , 'Mustermann')))); + } + + /** + * This test checks the String's sha512() method. + * + * @return void + */ + public function testSha512() + { + // initialize a new String instance + $string = new String('Mustermann'); + // check that String's sha512 summs equals + $this->assertTrue($string->sha512()->equals(new String(hash('sha512' , 'Mustermann')))); + } }