CryptorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Tests\RNCryptor;
  3. use PHPUnit\Framework\TestCase;
  4. use RNCryptor\RNCryptor\Decryptor;
  5. use RNCryptor\RNCryptor\Encryptor;
  6. class CryptorTest extends TestCase
  7. {
  8. // relative to __DIR__
  9. const TEXT_FILENAME = 'lorem-ipsum.txt';
  10. const SAMPLE_PLAINTEXT = 'What\'s your name? My name is Tilgath Pilesar. Why are you crying?';
  11. const SAMPLE_PASSWORD = 'do-not-write-this-down';
  12. const SAMPLE_PLAINTEXT_V2_BLOCKSIZE = 'Lorem ipsum dolor sit amet, cons';
  13. public static function main()
  14. {
  15. $suite = new PHPUnit_Framework_TestSuite(get_called_class());
  16. PHPUnit_TextUI_TestRunner::run($suite);
  17. }
  18. public function testCanDecryptSelfEncryptedDefaultVersion()
  19. {
  20. $encryptor = new Encryptor;
  21. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD);
  22. $decryptor = new Decryptor;
  23. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  24. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  25. }
  26. public function testCanDecryptSelfEncryptedStringEqualToBlockSizeMultiple()
  27. {
  28. $encryptor = new Encryptor;
  29. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT_V2_BLOCKSIZE, self::SAMPLE_PASSWORD);
  30. $decryptor = new Decryptor;
  31. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  32. $this->assertEquals(self::SAMPLE_PLAINTEXT_V2_BLOCKSIZE, $decrypted);
  33. }
  34. public function testCanDecryptSelfEncryptedVersion0()
  35. {
  36. $encryptor = new Encryptor;
  37. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 0);
  38. $decryptor = new Decryptor;
  39. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  40. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  41. }
  42. public function testCanDecryptSelfEncryptedVersion1()
  43. {
  44. $encryptor = new Encryptor;
  45. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 1);
  46. $decryptor = new Decryptor;
  47. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  48. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  49. }
  50. public function testCanDecryptSelfEncryptedVersion2()
  51. {
  52. $encryptor = new Encryptor;
  53. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 2);
  54. $decryptor = new Decryptor;
  55. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  56. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  57. }
  58. public function testCanDecryptLongText()
  59. {
  60. $text = file_get_contents(__DIR__ . '/_files/lorem-ipsum.txt');
  61. $encryptor = new Encryptor;
  62. $encrypted = $encryptor->encrypt($text, self::SAMPLE_PASSWORD);
  63. $decryptor = new Decryptor;
  64. $decrypted = $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  65. $this->assertEquals($text, $decrypted);
  66. }
  67. public function testVersion1TruncatesMultibytePasswords()
  68. {
  69. $password1 = '中文密码';
  70. $encryptor = new Encryptor;
  71. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, $password1, 1);
  72. // Yikes, it's truncated! So with an all-multibyte password
  73. // like above, we can replace the last half of the string
  74. // with whatver we want, and decryption will still work.
  75. $password2 = '中文中文';
  76. $decryptor = new Decryptor;
  77. $decrypted = $decryptor->decrypt($encrypted, $password2);
  78. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  79. $decryptor = new Decryptor;
  80. $decrypted = $decryptor->decrypt($encrypted, $password1);
  81. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  82. }
  83. public function testVersion2TruncatesMultibytePasswords()
  84. {
  85. $password1 = '中文密码';
  86. $encryptor = new Encryptor;
  87. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, $password1, 2);
  88. // Yikes, it's truncated! So with an all-multibyte password
  89. // like above, we can replace the last half of the string
  90. // with whatver we want, and decryption will still work.
  91. $password2 = '中文中文';
  92. $decryptor = new Decryptor;
  93. $decrypted = $decryptor->decrypt($encrypted, $password2);
  94. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  95. $decryptor = new Decryptor;
  96. $decrypted = $decryptor->decrypt($encrypted, $password1);
  97. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  98. }
  99. public function testVersion3AcceptsMultibytePasswords()
  100. {
  101. $password1 = '中文密码';
  102. $encryptor = new Encryptor;
  103. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, $password1, 3);
  104. $password2 = '中文中文';
  105. $decryptor = new Decryptor;
  106. $decrypted = $decryptor->decrypt($encrypted, $password2);
  107. $this->assertFalse($decrypted);
  108. $decryptor = new Decryptor;
  109. $decrypted = $decryptor->decrypt($encrypted, $password1);
  110. $this->assertEquals(self::SAMPLE_PLAINTEXT, $decrypted);
  111. }
  112. /**
  113. * @expectedException \Exception
  114. */
  115. public function testCannotUseWithUnsupportedSchemaVersions()
  116. {
  117. $fakeSchemaNumber = 57;
  118. $encrypted = $this->generateEncryptedStringWithUnsupportedSchemaNumber($fakeSchemaNumber);
  119. $decryptor = new Decryptor;
  120. $decryptor->decrypt($encrypted, self::SAMPLE_PASSWORD);
  121. }
  122. private function generateEncryptedStringWithUnsupportedSchemaNumber($fakeSchemaNumber)
  123. {
  124. $encryptor = new Encryptor;
  125. $plaintext = 'The price of ice is nice for mice';
  126. $encrypted = $encryptor->encrypt($plaintext, self::SAMPLE_PASSWORD);
  127. $encryptedBinary = base64_decode($encrypted);
  128. $encryptedBinary = chr($fakeSchemaNumber) . substr($encryptedBinary, 1);
  129. return base64_encode($encryptedBinary);
  130. }
  131. }