Fixed decrypting text with trailing whitespace

This commit is contained in:
Marcin Kurczewski 2014-05-05 08:41:38 +02:00
parent 7784be1838
commit 097deb52bd
3 changed files with 27 additions and 2 deletions

View file

@ -195,7 +195,11 @@ class TextHelper
$hash = base64_decode($hash);
$alg = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
return trim(mcrypt_decrypt($alg, $salt, $hash, $mode, $iv));
$ret = mcrypt_decrypt($alg, $salt, $hash, $mode, $iv);
$pos = strpos($ret, "\0");
if ($pos !== false)
$ret = substr($ret, 0, $pos);
return $ret;
}
catch (Exception $e)
{

View file

@ -3,6 +3,8 @@ class SimpleException extends Exception
{
public function __construct()
{
parent::__construct(call_user_func_array('sprintf', func_get_args()));
parent::__construct(func_num_args() > 1
? call_user_func_array('sprintf', func_get_args())
: func_get_args()[0]);
}
}

19
tests/MiscTest.php Normal file
View file

@ -0,0 +1,19 @@
<?php
class MiscTest extends AbstractTest
{
public function testEncryption()
{
$lengths = [0];
for ($i = 0; $i < 20; $i ++)
$lengths []= mt_rand(0, 10000);
foreach ($lengths as $length)
{
$text = '';
foreach (range(0, $length) as $j)
$text .= chr(mt_rand(1, 255));
$this->assert->areEqual($text, TextHelper::decrypt(TextHelper::encrypt($text)));
}
}
}