Skip to content
Permalink
Browse files

Removing the usage of Cake\Core\Exception in Security class

Replaced it with InvalidArgumentException
  • Loading branch information...
lorenzo committed Sep 7, 2014
1 parent 9869e50 commit cce1c891d70c24c20c09d7099019f28f52e3e0d5
Showing with 16 additions and 14 deletions.
  1. +11 −9 src/Utility/Security.php
  2. +5 −5 tests/TestCase/Utility/SecurityTest.php
@@ -15,7 +15,7 @@
namespace Cake\Utility;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use InvalidArgumentException;
/**
* Security Library contains utility methods related to security
@@ -86,18 +86,18 @@ public static function setHash($hash) {
* @param string $text Encrypted string to decrypt, normal string to encrypt
* @param string $key Key to use as the encryption key for encrypted data.
* @param string $operation Operation to perform, encrypt or decrypt
* @throws \Cake\Core\Exception\Exception When there are errors.
* @throws \InvalidArgumentException When there are errors.
* @return string Encrypted/Decrypted string
*/
public static function rijndael($text, $key, $operation) {
if (empty($key)) {
throw new Exception('You cannot use an empty key for Security::rijndael()');
throw new InvalidArgumentException('You cannot use an empty key for Security::rijndael()');
}
if (empty($operation) || !in_array($operation, array('encrypt', 'decrypt'))) {
throw new Exception('You must specify the operation for Security::rijndael(), either encrypt or decrypt');
throw new InvalidArgumentException('You must specify the operation for Security::rijndael(), either encrypt or decrypt');
}
if (strlen($key) < 32) {
throw new Exception('You must use a key larger than 32 bytes for Security::rijndael()');
throw new InvalidArgumentException('You must use a key larger than 32 bytes for Security::rijndael()');
}
$algorithm = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
@@ -125,7 +125,7 @@ public static function rijndael($text, $key, $operation) {
* @param string $key The 256 bit/32 byte key to use as a cipher key.
* @param string $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
* @return string Encrypted data.
* @throws \Cake\Core\Exception\Exception On invalid data or key.
* @throws \InvalidArgumentException On invalid data or key.
*/
public static function encrypt($plain, $key, $hmacSalt = null) {
self::_checkKey($key, 'encrypt()');
@@ -153,11 +153,13 @@ public static function encrypt($plain, $key, $hmacSalt = null) {
* @param string $key Key to check.
* @param string $method The method the key is being checked for.
* @return void
* @throws \Cake\Core\Exception\Exception When key length is not 256 bit/32 bytes
* @throws \InvalidArgumentException When key length is not 256 bit/32 bytes
*/
protected static function _checkKey($key, $method) {
if (strlen($key) < 32) {
throw new Exception(sprintf('Invalid key for %s, key must be at least 256 bits (32 bytes) long.', $method));
throw new InvalidArgumentException(
sprintf('Invalid key for %s, key must be at least 256 bits (32 bytes) long.', $method)
);
}
}
@@ -173,7 +175,7 @@ protected static function _checkKey($key, $method) {
public static function decrypt($cipher, $key, $hmacSalt = null) {
self::_checkKey($key, 'decrypt()');
if (empty($cipher)) {
throw new Exception('The data to decrypt cannot be empty.');
throw new InvalidArgumentException('The data to decrypt cannot be empty.');
}
if ($hmacSalt === null) {
$hmacSalt = Configure::read('Security.salt');
@@ -103,7 +103,7 @@ public function testRijndael() {
/**
* testRijndaelInvalidOperation method
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @return void
*/
public function testRijndaelInvalidOperation() {
@@ -115,7 +115,7 @@ public function testRijndaelInvalidOperation() {
/**
* testRijndaelInvalidKey method
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @return void
*/
public function testRijndaelInvalidKey() {
@@ -186,7 +186,7 @@ public function testDecryptHmacSaltFailure() {
/**
* Test that short keys cause errors
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
* @return void
*/
@@ -223,7 +223,7 @@ public function testEncryptDecryptFalseyData() {
/**
* Test that short keys cause errors
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
* @return void
*/
@@ -236,7 +236,7 @@ public function testDecryptInvalidKey() {
/**
* Test that empty data cause errors
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The data to decrypt cannot be empty.
* @return void
*/

0 comments on commit cce1c89

Please sign in to comment.
You can’t perform that action at this time.