2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('Security', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class SecurityTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* sut property
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $sut = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testInactiveMins method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInactiveMins() {
|
2008-05-30 11:40:08 +00:00
|
|
|
Configure::write('Security.level', 'high');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(10, Security::inactiveMins());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
Configure::write('Security.level', 'medium');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(100, Security::inactiveMins());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
Configure::write('Security.level', 'low');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(300, Security::inactiveMins());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testGenerateAuthkey method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGenerateAuthkey() {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(strlen(Security::generateAuthKey()), 40);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testValidateAuthKey method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateAuthKey() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$authKey = Security::generateAuthKey();
|
|
|
|
$this->assertTrue(Security::validateAuthKey($authKey));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* testHashInvalidSalt method
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashInvalidSalt() {
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::hash('someKey', 'blowfish', true);
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testHashAnotherInvalidSalt
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashAnotherInvalidSalt() {
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::hash('someKey', 'blowfish', '$1$lksdjoijfaoijs');
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testHashYetAnotherInvalidSalt
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashYetAnotherInvalidSalt() {
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::hash('someKey', 'blowfish', '$2a$10$123');
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testHashInvalidCost method
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashInvalidCost() {
|
|
|
|
Security::setCost(1000);
|
|
|
|
}
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testHash method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHash() {
|
2010-04-24 01:28:54 +00:00
|
|
|
$_hashType = Security::$hashType;
|
2009-03-21 23:55:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = 'someKey';
|
2008-10-02 00:03:52 +00:00
|
|
|
$hash = 'someHash';
|
|
|
|
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame(40, strlen(Security::hash($key, null, false)));
|
|
|
|
$this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
|
|
|
|
$this->assertSame(40, strlen(Security::hash($key, null, true)));
|
|
|
|
$this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-02 00:03:52 +00:00
|
|
|
$result = Security::hash($key, null, $hash);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
|
2008-10-02 00:03:52 +00:00
|
|
|
|
|
|
|
$result = Security::hash($key, 'sha1', $hash);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
|
2008-10-02 00:03:52 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$hashType = 'sha1';
|
|
|
|
Security::setHash($hashType);
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame($hashType, Security::$hashType);
|
|
|
|
$this->assertSame(40, strlen(Security::hash($key, null, true)));
|
|
|
|
$this->assertSame(40, strlen(Security::hash($key, null, false)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
|
|
|
|
$this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$hashType = 'md5';
|
|
|
|
Security::setHash($hashType);
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame($hashType, Security::$hashType);
|
|
|
|
$this->assertSame(32, strlen(Security::hash($key, null, false)));
|
|
|
|
$this->assertSame(32, strlen(Security::hash($key, null, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-02 14:44:42 +00:00
|
|
|
if (!function_exists('hash') && !function_exists('mhash')) {
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame(32, strlen(Security::hash($key, 'sha256', false)));
|
|
|
|
$this->assertSame(32, strlen(Security::hash($key, 'sha256', true)));
|
2008-08-02 14:44:42 +00:00
|
|
|
} else {
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
|
|
|
|
$this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-21 23:55:39 +00:00
|
|
|
|
2012-09-27 02:23:01 +00:00
|
|
|
Security::setHash($_hashType);
|
|
|
|
}
|
|
|
|
|
2016-04-16 01:49:17 +00:00
|
|
|
/**
|
|
|
|
* Test that blowfish doesn't return '' when the salt is ''
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashBlowfishEmptySalt() {
|
|
|
|
$test = Security::hash('password', 'blowfish');
|
|
|
|
$this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
|
|
|
|
|
|
|
|
$stored = '';
|
|
|
|
$hash = Security::hash('anything', 'blowfish', $stored);
|
|
|
|
$this->assertNotEquals($stored, $hash);
|
|
|
|
|
|
|
|
$hash = Security::hash('anything', 'blowfish', false);
|
|
|
|
$this->assertNotEquals($stored, $hash);
|
|
|
|
|
|
|
|
$hash = Security::hash('anything', 'blowfish', null);
|
|
|
|
$this->assertNotEquals($stored, $hash);
|
|
|
|
}
|
|
|
|
|
2012-09-27 02:23:01 +00:00
|
|
|
/**
|
|
|
|
* Test that hash() works with blowfish.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHashBlowfish() {
|
|
|
|
$test = Security::hash('password', 'blowfish');
|
|
|
|
$this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
|
|
|
|
|
2016-04-16 01:49:17 +00:00
|
|
|
Security::setCost(10);
|
2012-09-27 02:23:01 +00:00
|
|
|
$_hashType = Security::$hashType;
|
2012-09-28 01:42:15 +00:00
|
|
|
|
2012-09-27 02:23:01 +00:00
|
|
|
$key = 'someKey';
|
2012-07-21 16:48:14 +00:00
|
|
|
$hashType = 'blowfish';
|
|
|
|
Security::setHash($hashType);
|
2012-09-27 02:23:01 +00:00
|
|
|
|
2014-04-24 10:54:45 +00:00
|
|
|
$this->assertSame($hashType, Security::$hashType);
|
|
|
|
$this->assertSame(60, strlen(Security::hash($key, null, false)));
|
2012-07-21 16:48:14 +00:00
|
|
|
|
|
|
|
$password = $submittedPassword = $key;
|
|
|
|
$storedPassword = Security::hash($password);
|
|
|
|
|
|
|
|
$hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
|
|
|
|
$this->assertSame($storedPassword, $hashedPassword);
|
|
|
|
|
|
|
|
$submittedPassword = 'someOtherKey';
|
|
|
|
$hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
|
|
|
|
$this->assertNotSame($storedPassword, $hashedPassword);
|
|
|
|
|
2012-11-01 13:15:52 +00:00
|
|
|
$expected = sha1('customsaltsomevalue');
|
|
|
|
$result = Security::hash('somevalue', 'sha1', 'customsalt');
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$oldSalt = Configure::read('Security.salt');
|
|
|
|
Configure::write('Security.salt', 'customsalt');
|
|
|
|
|
|
|
|
$expected = sha1('customsaltsomevalue');
|
|
|
|
$result = Security::hash('somevalue', 'sha1', true);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
Configure::write('Security.salt', $oldSalt);
|
2009-03-21 23:55:39 +00:00
|
|
|
Security::setHash($_hashType);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testCipher method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCipher() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$length = 10;
|
|
|
|
$txt = '';
|
2008-06-05 15:20:45 +00:00
|
|
|
for ($i = 0; $i < $length; $i++) {
|
2008-07-05 14:31:22 +00:00
|
|
|
$txt .= mt_rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($txt, Security::cipher($result, $key));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$txt = '';
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($txt, Security::cipher($result, $key));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-03-27 21:19:42 +00:00
|
|
|
$txt = 123456;
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($txt, Security::cipher($result, $key));
|
2010-03-27 21:19:42 +00:00
|
|
|
|
|
|
|
$txt = '123456';
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($txt, Security::cipher($result, $key));
|
2011-09-10 16:23:28 +00:00
|
|
|
}
|
2010-05-19 04:45:35 +00:00
|
|
|
|
2011-09-10 16:23:28 +00:00
|
|
|
/**
|
|
|
|
* testCipherEmptyKey method
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCipherEmptyKey() {
|
2010-05-19 04:45:35 +00:00
|
|
|
$txt = 'some_text';
|
|
|
|
$key = '';
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::cipher($txt, $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-05-29 23:25:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testRijndael method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRijndael() {
|
2012-09-15 10:43:39 +00:00
|
|
|
$this->skipIf(!function_exists('mcrypt_encrypt'));
|
2012-05-29 23:25:01 +00:00
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
|
|
|
|
|
|
|
|
$result = Security::rijndael($txt, $key, 'encrypt');
|
|
|
|
$this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
|
|
|
|
|
|
|
|
$result = Security::rijndael($key, $txt, 'encrypt');
|
|
|
|
$this->assertEquals($key, Security::rijndael($result, $txt, 'decrypt'));
|
|
|
|
|
|
|
|
$result = Security::rijndael('', $key, 'encrypt');
|
|
|
|
$this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
|
|
|
|
|
2013-02-09 19:55:13 +00:00
|
|
|
$key = 'this is my key of over 32 chars, yes it is';
|
|
|
|
$result = Security::rijndael($txt, $key, 'encrypt');
|
2012-05-29 23:25:01 +00:00
|
|
|
$this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
|
|
|
|
}
|
|
|
|
|
2013-02-09 19:55:13 +00:00
|
|
|
/**
|
|
|
|
* Test that rijndael() can still decrypt values with a fixed iv.
|
|
|
|
*
|
2013-07-05 15:19:22 +00:00
|
|
|
* @return void
|
2013-02-09 19:55:13 +00:00
|
|
|
*/
|
|
|
|
public function testRijndaelBackwardCompatibility() {
|
|
|
|
$this->skipIf(!function_exists('mcrypt_encrypt'));
|
|
|
|
|
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
|
|
|
|
|
|
|
|
// Encrypted before random iv
|
|
|
|
$value = base64_decode('1WPjnq96LMzLGwNgmudHF+cAIqVUN5DaUZEpf5tm1EzSgt5iYY9o3d66iRI/fKJLTlTVGsa8HzW0jDNitmVXoQ==');
|
|
|
|
$this->assertEquals($txt, Security::rijndael($value, $key, 'decrypt'));
|
|
|
|
}
|
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
/**
|
|
|
|
* testRijndaelInvalidOperation method
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRijndaelInvalidOperation() {
|
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::rijndael($txt, $key, 'foo');
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRijndaelInvalidKey method
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRijndaelInvalidKey() {
|
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'too small';
|
2013-01-23 12:45:50 +00:00
|
|
|
Security::rijndael($txt, $key, 'encrypt');
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 01:20:22 +00:00
|
|
|
/**
|
|
|
|
* Test encrypt/decrypt.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEncryptDecrypt() {
|
2016-12-05 21:14:33 +00:00
|
|
|
$this->skipIf(!extension_loaded('mcrypt'), 'This test requires mcrypt to be installed');
|
2013-08-28 01:20:22 +00:00
|
|
|
$txt = 'The quick brown fox';
|
|
|
|
$key = 'This key is longer than 32 bytes long.';
|
|
|
|
$result = Security::encrypt($txt, $key);
|
|
|
|
$this->assertNotEquals($txt, $result, 'Should be encrypted.');
|
|
|
|
$this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
|
|
|
|
$this->assertEquals($txt, Security::decrypt($result, $key));
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:40:01 +00:00
|
|
|
/**
|
|
|
|
* Test that changing the key causes decryption to fail.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecryptKeyFailure() {
|
2016-12-05 21:14:33 +00:00
|
|
|
$this->skipIf(!extension_loaded('mcrypt'), 'This test requires mcrypt to be installed');
|
2013-08-29 18:40:01 +00:00
|
|
|
$txt = 'The quick brown fox';
|
|
|
|
$key = 'This key is longer than 32 bytes long.';
|
2015-09-25 15:22:00 +00:00
|
|
|
Security::encrypt($txt, $key);
|
2013-08-29 18:40:01 +00:00
|
|
|
|
|
|
|
$key = 'Not the same key. This one will fail';
|
|
|
|
$this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that decrypt fails when there is an hmac error.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecryptHmacFailure() {
|
2016-12-05 21:14:33 +00:00
|
|
|
$this->skipIf(!extension_loaded('mcrypt'), 'This test requires mcrypt to be installed');
|
2013-08-29 18:40:01 +00:00
|
|
|
$txt = 'The quick brown fox';
|
|
|
|
$key = 'This key is quite long and works well.';
|
|
|
|
$salt = 'this is a delicious salt!';
|
|
|
|
$result = Security::encrypt($txt, $key, $salt);
|
|
|
|
|
|
|
|
// Change one of the bytes in the hmac.
|
|
|
|
$result[10] = 'x';
|
|
|
|
$this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that changing the hmac salt will cause failures.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecryptHmacSaltFailure() {
|
2016-12-05 21:14:33 +00:00
|
|
|
$this->skipIf(!extension_loaded('mcrypt'), 'This test requires mcrypt to be installed');
|
2013-08-29 18:40:01 +00:00
|
|
|
$txt = 'The quick brown fox';
|
|
|
|
$key = 'This key is quite long and works well.';
|
|
|
|
$salt = 'this is a delicious salt!';
|
|
|
|
$result = Security::encrypt($txt, $key, $salt);
|
|
|
|
|
|
|
|
$salt = 'humpty dumpty had a great fall.';
|
|
|
|
$this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
|
|
|
|
}
|
|
|
|
|
2013-08-28 01:20:22 +00:00
|
|
|
/**
|
|
|
|
* Test that short keys cause errors
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEncryptInvalidKey() {
|
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'this is too short';
|
|
|
|
Security::encrypt($txt, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-15 13:56:21 +00:00
|
|
|
* Test encrypting falsey data
|
2013-08-28 01:20:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-12-15 13:56:21 +00:00
|
|
|
public function testEncryptDecryptFalseyData() {
|
2016-12-05 21:14:33 +00:00
|
|
|
$this->skipIf(!extension_loaded('mcrypt'), 'This test requires mcrypt to be installed');
|
2013-08-28 01:20:22 +00:00
|
|
|
$key = 'This is a key that is long enough to be ok.';
|
2013-12-15 13:56:21 +00:00
|
|
|
|
|
|
|
$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));
|
2013-08-28 01:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that short keys cause errors
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecryptInvalidKey() {
|
|
|
|
$txt = 'The quick brown fox jumped over the lazy dog.';
|
|
|
|
$key = 'this is too short';
|
|
|
|
Security::decrypt($txt, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that empty data cause errors
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @expectedExceptionMessage The data to decrypt cannot be empty.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecryptInvalidData() {
|
|
|
|
$txt = '';
|
|
|
|
$key = 'This is a key that is long enough to be ok.';
|
|
|
|
Security::decrypt($txt, $key);
|
|
|
|
}
|
|
|
|
|
2016-02-22 05:14:44 +00:00
|
|
|
/**
|
|
|
|
* Test the random method.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRandomBytes() {
|
|
|
|
$value = Security::randomBytes(16);
|
|
|
|
$this->assertSame(16, strlen($value));
|
|
|
|
|
|
|
|
$value = Security::randomBytes(64);
|
|
|
|
$this->assertSame(64, strlen($value));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|