diff --git a/src/AppserverIo/Lang/String.php b/src/AppserverIo/Lang/String.php index 4d2e4ef..897f989 100644 --- a/src/AppserverIo/Lang/String.php +++ b/src/AppserverIo/Lang/String.php @@ -413,16 +413,58 @@ public function trim() } /** - * md5 encryptes the string and returns the + * 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($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) + { + return $this->init(hash('sha1', $salt . $this->stringValue())); + } + + /** + * 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) { - return $this->init(md5($this->stringValue())); + return $this->init(hash('sha256', $salt . $this->stringValue())); } + /** + * 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) + { + return $this->init(hash('sha512', $salt . $this->stringValue())); + } + + /** * Converts the string value to upper case * and returns the instance. 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')))); + } }