Esempio n. 1
0
 function testBinary()
 {
     $key = uniqid();
     $file = __DIR__ . '/binary';
     $source = file_get_contents($file);
     //binary file
     $encrypted = OC_Crypt::encrypt($source, $key);
     $decrypted = OC_Crypt::decrypt($encrypted, $key);
     $decrypted = rtrim($decrypted, "");
     $this->assertEqual($decrypted, $source);
     $encrypted = OC_Crypt::blockEncrypt($source, $key);
     $decrypted = OC_Crypt::blockDecrypt($encrypted, $key, strlen($source));
     $this->assertEqual($decrypted, $source);
 }
 public static function changekeypasscode($newpasscode)
 {
     if (OC_User::isLoggedIn()) {
         $username = OC_USER::getUser();
         // read old key
         $key = file_get_contents(OC_Config::getValue("datadirectory") . '/' . $username . '/encryption.key');
         // decrypt key with old passcode
         $key = OC_Crypt::decrypt($key, $_SESSION['user_password']);
         // encrypt again with new passcode
         $key = OC_Crypt::encrypt($key, $newpassword);
         // store the new key
         file_put_contents(OC_Config::getValue("datadirectory") . '/' . $username . '/encryption.key', $key);
         $_SESSION['user_password'] = $newpasscode;
     }
 }
 function testEncryption()
 {
     $key = uniqid();
     $file = OC::$SERVERROOT . '/3rdparty/MDB2.php';
     $source = file_get_contents($file);
     //nice large text file
     $encrypted = OC_Crypt::encrypt($source, $key);
     $decrypted = OC_Crypt::decrypt($encrypted, $key);
     $this->assertNotEqual($encrypted, $source);
     $this->assertEqual($decrypted, $source);
     $chunk = substr($source, 0, 8192);
     $encrypted = OC_Crypt::encrypt($chunk, $key);
     $this->assertEqual(strlen($chunk), strlen($encrypted));
     $decrypted = OC_Crypt::decrypt($encrypted, $key);
     $this->assertEqual($decrypted, $chunk);
     $encrypted = OC_Crypt::blockEncrypt($source, $key);
     $decrypted = OC_Crypt::blockDecrypt($encrypted, $key);
     $this->assertNotEqual($encrypted, $source);
     $this->assertEqual($decrypted, $source);
     $tmpFileEncrypted = OCP\Files::tmpFile();
     OC_Crypt::encryptfile($file, $tmpFileEncrypted, $key);
     $encrypted = file_get_contents($tmpFileEncrypted);
     $decrypted = OC_Crypt::blockDecrypt($encrypted, $key);
     $this->assertNotEqual($encrypted, $source);
     $this->assertEqual($decrypted, $source);
     $tmpFileDecrypted = OCP\Files::tmpFile();
     OC_Crypt::decryptfile($tmpFileEncrypted, $tmpFileDecrypted, $key);
     $decrypted = file_get_contents($tmpFileDecrypted);
     $this->assertEqual($decrypted, $source);
     $file = OC::$SERVERROOT . '/core/img/weather-clear.png';
     $source = file_get_contents($file);
     //binary file
     $encrypted = OC_Crypt::encrypt($source, $key);
     $decrypted = OC_Crypt::decrypt($encrypted, $key);
     $this->assertEqual($decrypted, $source);
     $encrypted = OC_Crypt::blockEncrypt($source, $key);
     $decrypted = OC_Crypt::blockDecrypt($encrypted, $key);
     $this->assertEqual($decrypted, $source);
 }
 public function stream_write($data)
 {
     $length = strlen($data);
     $written = 0;
     $currentPos = ftell($this->source);
     if ($this->writeCache) {
         $data = $this->writeCache . $data;
         $this->writeCache = '';
     }
     if ($currentPos % 8192 != 0) {
         //make sure we always start on a block start
         fseek($this->source, -($currentPos % 8192), SEEK_CUR);
         $encryptedBlock = fread($this->source, 8192);
         fseek($this->source, -($currentPos % 8192), SEEK_CUR);
         $block = OC_Crypt::decrypt($encryptedBlock);
         $data = substr($block, 0, $currentPos % 8192) . $data;
         fseek($this->source, -($currentPos % 8192), SEEK_CUR);
     }
     while (strlen($data) > 0) {
         if (strlen($data) < 8192) {
             $this->writeCache = $data;
             $data = '';
         } else {
             $encrypted = OC_Crypt::encrypt(substr($data, 0, 8192));
             fwrite($this->source, $encrypted);
             $data = substr($data, 8192);
         }
     }
     return $length;
 }
Esempio n. 5
0
 public static function changekeypasscode($oldPassword, $newPassword)
 {
     if (OCP\User::isLoggedIn()) {
         $username = OCP\USER::getUser();
         $view = new OC_FilesystemView('/' . $username);
         // read old key
         $key = $view->file_get_contents('/encryption.key');
         // decrypt key with old passcode
         $key = OC_Crypt::decrypt($key, $oldPassword);
         // encrypt again with new passcode
         $key = OC_Crypt::encrypt($key, $newPassword);
         // store the new key
         $view->file_put_contents('/encryption.key', $key);
     }
 }