McryptTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility\Crypto;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\Crypto\Mcrypt;
  18. /**
  19. * Mcrypt engine tests.
  20. */
  21. class McryptTest extends TestCase
  22. {
  23. /**
  24. * Setup function.
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. $this->skipIf(!function_exists('mcrypt_encrypt'), 'No mcrypt skipping tests');
  32. $this->crypt = new Mcrypt();
  33. }
  34. /**
  35. * testRijndael method
  36. *
  37. * @return void
  38. */
  39. public function testRijndael()
  40. {
  41. $txt = 'The quick brown fox jumped over the lazy dog.';
  42. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  43. $result = $this->crypt->rijndael($txt, $key, 'encrypt');
  44. $this->assertEquals($txt, $this->crypt->rijndael($result, $key, 'decrypt'));
  45. $result = $this->crypt->rijndael($key, $txt, 'encrypt');
  46. $this->assertEquals($key, $this->crypt->rijndael($result, $txt, 'decrypt'));
  47. $result = $this->crypt->rijndael('', $key, 'encrypt');
  48. $this->assertEquals('', $this->crypt->rijndael($result, $key, 'decrypt'));
  49. $key = 'this is my key of over 32 chars, yes it is';
  50. $result = $this->crypt->rijndael($txt, $key, 'encrypt');
  51. $this->assertEquals($txt, $this->crypt->rijndael($result, $key, 'decrypt'));
  52. }
  53. /**
  54. * Test encrypt/decrypt.
  55. *
  56. * @return void
  57. */
  58. public function testEncryptDecrypt()
  59. {
  60. $txt = 'The quick brown fox';
  61. $key = 'This key is enough bytes';
  62. $result = $this->crypt->encrypt($txt, $key);
  63. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  64. $this->assertNotEquals($result, $this->crypt->encrypt($txt, $key), 'Each result is unique.');
  65. $this->assertEquals($txt, $this->crypt->decrypt($result, $key));
  66. }
  67. /**
  68. * Test that changing the key causes decryption to fail.
  69. *
  70. * @return void
  71. */
  72. public function testDecryptKeyFailure()
  73. {
  74. $txt = 'The quick brown fox';
  75. $key = substr(hash('sha256', 'This key is enough bytes'), 0, 32);
  76. $result = $this->crypt->encrypt($txt, $key);
  77. $key = substr(hash('sha256', 'Not the same key.'), 0, 32);
  78. $this->assertFalse($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
  79. }
  80. /**
  81. * Ensure that data encrypted with 2.x encrypt() function can be decrypted with mcrypt engine.
  82. *
  83. * The $cipher variable is base64 encoded data from 2.x encrypt()
  84. *
  85. * @return
  86. */
  87. public function testDecryptOldData()
  88. {
  89. $key = 'My password is nice and long really it is';
  90. $key = substr(hash('sha256', $key), 0, 32);
  91. $cipher = 'ZmFkMjdmY2U2NjgzOTkwMGZmMWJiMzY0ZDA5ZDUwZmNjYTdjNWVkZThkMzhmNzdiY' .
  92. 'Tg3ZDFjMzNjNmViMDljMnk9k0LmYpwSZH5eq7GmDozMwHxzh37YaXFQ2TK5gXb5OfTKXv83K+NjAS9lIo/Zvw==';
  93. $data = base64_decode($cipher);
  94. $cipher = substr($data, 64);
  95. $result = $this->crypt->decrypt($cipher, $key);
  96. $this->assertEquals('This is a secret message', $result);
  97. }
  98. }