From 097deb52bd59bd9e749af4e045a082c58fcab5a3 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 5 May 2014 08:41:38 +0200 Subject: [PATCH] Fixed decrypting text with trailing whitespace --- src/Helpers/TextHelper.php | 6 +++++- src/SimpleException.php | 4 +++- tests/MiscTest.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/MiscTest.php diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index ff108db9..7e9ec8a1 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -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) { diff --git a/src/SimpleException.php b/src/SimpleException.php index 8ca34ef3..99baa0ad 100644 --- a/src/SimpleException.php +++ b/src/SimpleException.php @@ -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]); } } diff --git a/tests/MiscTest.php b/tests/MiscTest.php new file mode 100644 index 00000000..47795911 --- /dev/null +++ b/tests/MiscTest.php @@ -0,0 +1,19 @@ +assert->areEqual($text, TextHelper::decrypt(TextHelper::encrypt($text))); + } + } +}