Exemplo n.º 1
0
 /**
  * Tests the serialization and de-serialization of an EncryptedFile object
  */
 public function testSerializiation()
 {
     // Test whether serialization doesn't return empty
     $serialized = serialize($this->file);
     $this->assertNotEmpty($serialized);
     // Test whether serialization returned the proper object
     $file = unserialize($serialized);
     $this->assertInstanceOf(EncryptedFile::class, $file);
     // Test whether the objects are the same
     $this->assertEquals($this->file->getIv(), $file->getIv());
     $this->assertEquals($this->file->getChecksum(), $file->getChecksum());
     $this->assertEquals($this->file->getPadding(), $file->getPadding());
     $this->assertEquals($this->file->getFile(), $file->getFile());
     // Test whether the file property is still an instance of SplFileInfo
     $this->assertInstanceOf(\SplFileInfo::class, $file->getFile());
 }
Exemplo n.º 2
0
 /**
  * Decrypts a file in a stream, performing the callback on each successive decrypted block.
  * If the checksum is provided it checks it against the encrypted file for integrity.
  *
  * The callback can accept two arguments:
  *  - $data   - A chunk of decrypted data
  *  - $stream - The resource stream that is decrypting
  *
  * @param EncryptedFile $encryptedFile
  * @param \Closure      $callback
  * @throws DecryptException
  */
 public function streamDecrypt(EncryptedFile $encryptedFile, \Closure $callback)
 {
     // Get the path to the encrypted file
     $source = $encryptedFile->getFile()->getRealPath();
     // Get the decryption stream
     try {
         $stream = $this->createDecryptionStream($source, $this->getOptions($encryptedFile->getIv()));
     } catch (\Exception $e) {
         throw new DecryptException('Unable to create decryption stream', 0, $e);
     }
     // Run the callback while the file pointer isn't at the end
     while (!feof($stream)) {
         $data = fread($stream, self::CHUNK_BYTES);
         // Trim the padded bytes off of the data once EOF has been reached
         if (feof($stream)) {
             $data = substr($data, 0, -$encryptedFile->getPadding());
         }
         $callback($data, $stream);
     }
 }