Skip to content

Added support for sha hash family of algorithms #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/AppserverIo/Lang/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions tests/AppserverIo/Lang/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))));
}
}