Don't throw exception when trying to encrypt falsey value.

This commit is contained in:
ADmad 2013-12-15 19:26:21 +05:30
parent bf96ea36d9
commit 8a666fb37e
2 changed files with 18 additions and 9 deletions

View file

@ -375,16 +375,27 @@ class SecurityTest extends CakeTestCase {
} }
/** /**
* Test that empty data cause errors * Test encrypting falsey data
* *
* @expectedException CakeException
* @expectedExceptionMessage The data to encrypt cannot be empty.
* @return void * @return void
*/ */
public function testEncryptInvalidData() { public function testEncryptDecryptFalseyData() {
$txt = '';
$key = 'This is a key that is long enough to be ok.'; $key = 'This is a key that is long enough to be ok.';
Security::encrypt($txt, $key);
$result = Security::encrypt('', $key);
$this->assertSame('', Security::decrypt($result, $key));
$result = Security::encrypt(false, $key);
$this->assertSame('', Security::decrypt($result, $key));
$result = Security::encrypt(null, $key);
$this->assertSame('', Security::decrypt($result, $key));
$result = Security::encrypt(0, $key);
$this->assertSame('0', Security::decrypt($result, $key));
$result = Security::encrypt('0', $key);
$this->assertSame('0', Security::decrypt($result, $key));
} }
/** /**

View file

@ -303,9 +303,7 @@ class Security {
*/ */
public static function encrypt($plain, $key, $hmacSalt = null) { public static function encrypt($plain, $key, $hmacSalt = null) {
self::_checkKey($key, 'encrypt()'); self::_checkKey($key, 'encrypt()');
if (empty($plain)) {
throw new CakeException(__d('cake_dev', 'The data to encrypt cannot be empty.'));
}
if ($hmacSalt === null) { if ($hmacSalt === null) {
$hmacSalt = Configure::read('Security.salt'); $hmacSalt = Configure::read('Security.salt');
} }