McryptTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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') || version_compare(PHP_VERSION, '7.1', '>='), '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. * @group deprecated
  57. * @return void
  58. */
  59. public function testEncryptDecrypt()
  60. {
  61. $this->deprecated(function () {
  62. $txt = 'The quick brown fox';
  63. $key = 'This key is enough bytes';
  64. $result = $this->crypt->encrypt($txt, $key);
  65. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  66. $this->assertNotEquals($result, $this->crypt->encrypt($txt, $key), 'Each result is unique.');
  67. $this->assertEquals($txt, $this->crypt->decrypt($result, $key));
  68. });
  69. }
  70. /**
  71. * Test that changing the key causes decryption to fail.
  72. *
  73. * @group deprecated
  74. * @return void
  75. */
  76. public function testDecryptKeyFailure()
  77. {
  78. $this->deprecated(function () {
  79. $txt = 'The quick brown fox';
  80. $key = substr(hash('sha256', 'This key is enough bytes'), 0, 32);
  81. $result = $this->crypt->encrypt($txt, $key);
  82. $key = substr(hash('sha256', 'Not the same key.'), 0, 32);
  83. $this->assertFalse($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
  84. });
  85. }
  86. /**
  87. * Ensure that data encrypted with 2.x encrypt() function can be decrypted with mcrypt engine.
  88. *
  89. * The $cipher variable is base64 encoded data from 2.x encrypt()
  90. *
  91. * @group deprecated
  92. * @return
  93. */
  94. public function testDecryptOldData()
  95. {
  96. $this->deprecated(function () {
  97. $key = 'My password is nice and long really it is';
  98. $key = substr(hash('sha256', $key), 0, 32);
  99. $cipher = 'ZmFkMjdmY2U2NjgzOTkwMGZmMWJiMzY0ZDA5ZDUwZmNjYTdjNWVkZThkMzhmNzdiY' .
  100. 'Tg3ZDFjMzNjNmViMDljMnk9k0LmYpwSZH5eq7GmDozMwHxzh37YaXFQ2TK5gXb5OfTKXv83K+NjAS9lIo/Zvw==';
  101. $data = base64_decode($cipher);
  102. $cipher = substr($data, 64);
  103. $result = $this->crypt->decrypt($cipher, $key);
  104. $this->assertEquals('This is a secret message', $result);
  105. });
  106. }
  107. }