Example #1
0
 /**
  * start receiving chunks from a file. This is the place where you can
  * perform some initial step before starting encrypting/decrypting the
  * chunks
  *
  * @param string $path to the file
  * @param string $user who read/write the file
  * @param string $mode php stream open mode
  * @param array $header contains the header data read from the file
  * @param array $accessList who has access to the file contains the key 'users' and 'public'
  *
  * @return array $header contain data as key-value pairs which should be
  *                       written to the header, in case of a write operation
  *                       or if no additional data is needed return a empty array
  */
 public function begin($path, $user, $mode, array $header, array $accessList)
 {
     $this->path = $this->getPathToRealFile($path);
     $this->accessList = $accessList;
     $this->user = $user;
     $this->isWriteOperation = false;
     $this->writeCache = '';
     $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
     if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') {
         $this->isWriteOperation = true;
         if (empty($this->fileKey)) {
             $this->fileKey = $this->crypt->generateFileKey();
         }
     }
     if (isset($header['cipher'])) {
         $this->cipher = $header['cipher'];
     } elseif ($this->isWriteOperation) {
         $this->cipher = $this->crypt->getCipher();
     } else {
         // if we read a file without a header we fall-back to the legacy cipher
         // which was used in <=oC6
         $this->cipher = $this->crypt->getLegacyCipher();
     }
     return array('cipher' => $this->cipher);
 }
Example #2
0
 /**
  * encrypt data
  *
  * @param string $data you want to encrypt
  * @return mixed encrypted data
  */
 public function encrypt($data)
 {
     $this->isWriteOperation = true;
     if (empty($this->fileKey)) {
         $this->fileKey = $this->crypt->generateFileKey();
     }
     // If extra data is left over from the last round, make sure it
     // is integrated into the next 6126 / 8192 block
     if ($this->writeCache) {
         // Concat writeCache to start of $data
         $data = $this->writeCache . $data;
         // Clear the write cache, ready for reuse - it has been
         // flushed and its old contents processed
         $this->writeCache = '';
     }
     $encrypted = '';
     // While there still remains some data to be processed & written
     while (strlen($data) > 0) {
         // Remaining length for this iteration, not of the
         // entire file (may be greater than 8192 bytes)
         $remainingLength = strlen($data);
         // If data remaining to be written is less than the
         // size of 1 6126 byte block
         if ($remainingLength < 6126) {
             // Set writeCache to contents of $data
             // The writeCache will be carried over to the
             // next write round, and added to the start of
             // $data to ensure that written blocks are
             // always the correct length. If there is still
             // data in writeCache after the writing round
             // has finished, then the data will be written
             // to disk by $this->flush().
             $this->writeCache = $data;
             // Clear $data ready for next round
             $data = '';
         } else {
             // Read the chunk from the start of $data
             $chunk = substr($data, 0, 6126);
             $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey);
             // Remove the chunk we just processed from
             // $data, leaving only unprocessed data in $data
             // var, for handling on the next round
             $data = substr($data, 6126);
         }
     }
     return $encrypted;
 }
Example #3
0
 /**
  * start receiving chunks from a file. This is the place where you can
  * perform some initial step before starting encrypting/decrypting the
  * chunks
  *
  * @param string $path to the file
  * @param string $user who read/write the file
  * @param string $mode php stream open mode
  * @param array $header contains the header data read from the file
  * @param array $accessList who has access to the file contains the key 'users' and 'public'
  *
  * @return array $header contain data as key-value pairs which should be
  *                       written to the header, in case of a write operation
  *                       or if no additional data is needed return a empty array
  */
 public function begin($path, $user, $mode, array $header, array $accessList)
 {
     $this->path = $this->getPathToRealFile($path);
     $this->accessList = $accessList;
     $this->user = $user;
     $this->isWriteOperation = false;
     $this->writeCache = '';
     if ($this->session->decryptAllModeActivated()) {
         $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path);
         $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid());
         $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, $shareKey, $this->session->getDecryptAllKey());
     } else {
         $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
     }
     // always use the version from the original file, also part files
     // need to have a correct version number if they get moved over to the
     // final location
     $this->version = (int) $this->keyManager->getVersion($this->stripPartFileExtension($path), new View());
     if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') {
         $this->isWriteOperation = true;
         if (empty($this->fileKey)) {
             $this->fileKey = $this->crypt->generateFileKey();
         }
     } else {
         // if we read a part file we need to increase the version by 1
         // because the version number was also increased by writing
         // the part file
         if (Scanner::isPartialFile($path)) {
             $this->version = $this->version + 1;
         }
     }
     if ($this->isWriteOperation) {
         $this->cipher = $this->crypt->getCipher();
     } elseif (isset($header['cipher'])) {
         $this->cipher = $header['cipher'];
     } else {
         // if we read a file without a header we fall-back to the legacy cipher
         // which was used in <=oC6
         $this->cipher = $this->crypt->getLegacyCipher();
     }
     return array('cipher' => $this->cipher, 'signed' => 'true');
 }