encryption.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Encryption extends UnitTestCase {
  9. function testEncryption() {
  10. $key=uniqid();
  11. $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
  12. $source=file_get_contents($file); //nice large text file
  13. $encrypted=OC_Crypt::encrypt($source, $key);
  14. $decrypted=OC_Crypt::decrypt($encrypted, $key);
  15. $decrypted=rtrim($decrypted, "\0");
  16. $this->assertNotEqual($encrypted, $source);
  17. $this->assertEqual($decrypted, $source);
  18. $chunk=substr($source, 0, 8192);
  19. $encrypted=OC_Crypt::encrypt($chunk, $key);
  20. $this->assertEqual(strlen($chunk), strlen($encrypted));
  21. $decrypted=OC_Crypt::decrypt($encrypted, $key);
  22. $decrypted=rtrim($decrypted, "\0");
  23. $this->assertEqual($decrypted, $chunk);
  24. $encrypted=OC_Crypt::blockEncrypt($source, $key);
  25. $decrypted=OC_Crypt::blockDecrypt($encrypted, $key);
  26. $this->assertNotEqual($encrypted, $source);
  27. $this->assertEqual($decrypted, $source);
  28. $tmpFileEncrypted=OCP\Files::tmpFile();
  29. OC_Crypt::encryptfile($file, $tmpFileEncrypted, $key);
  30. $encrypted=file_get_contents($tmpFileEncrypted);
  31. $decrypted=OC_Crypt::blockDecrypt($encrypted, $key);
  32. $this->assertNotEqual($encrypted, $source);
  33. $this->assertEqual($decrypted, $source);
  34. $tmpFileDecrypted=OCP\Files::tmpFile();
  35. OC_Crypt::decryptfile($tmpFileEncrypted, $tmpFileDecrypted, $key);
  36. $decrypted=file_get_contents($tmpFileDecrypted);
  37. $this->assertEqual($decrypted, $source);
  38. $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
  39. $source=file_get_contents($file); //binary file
  40. $encrypted=OC_Crypt::encrypt($source, $key);
  41. $decrypted=OC_Crypt::decrypt($encrypted, $key);
  42. $decrypted=rtrim($decrypted, "\0");
  43. $this->assertEqual($decrypted, $source);
  44. $encrypted=OC_Crypt::blockEncrypt($source, $key);
  45. $decrypted=OC_Crypt::blockDecrypt($encrypted, $key);
  46. $this->assertEqual($decrypted, $source);
  47. }
  48. function testBinary() {
  49. $key=uniqid();
  50. $file=__DIR__.'/binary';
  51. $source=file_get_contents($file); //binary file
  52. $encrypted=OC_Crypt::encrypt($source, $key);
  53. $decrypted=OC_Crypt::decrypt($encrypted, $key);
  54. $decrypted=rtrim($decrypted, "\0");
  55. $this->assertEqual($decrypted, $source);
  56. $encrypted=OC_Crypt::blockEncrypt($source, $key);
  57. $decrypted=OC_Crypt::blockDecrypt($encrypted, $key, strlen($source));
  58. $this->assertEqual($decrypted, $source);
  59. }
  60. }