halite icon indicating copy to clipboard operation
halite copied to clipboard

Decrypting a file to output buffer fails

Open Nathan-Furnal opened this issue 2 years ago • 1 comments

Hi, I'm creating an application where files are encrypted when they're received and decrypted only when an authenticated user clicks on the file link in their own dashboard. As such, I'm trying to write to the output buffer so it's downloaded on their end but never appears decrypted on the server's storage.

To do this I do something like the following in a function body, that function is called with a POST request that fetches the $file name and id in storage. I got the writing to output lines from this pull request.

$filepath = storage_path().'/app/'.$file->name;
$stream = fopen('php://output', 'wb');
ob_start();
File::decrypt($filepath, $stream, $enc_key);

But when I use is, I get a type error:

ParagonIE\Halite\File::decrypt(): Argument #2 ($output) must be of type ParagonIE\Halite\Stream\MutableFile|string, 
resource given

How can I fix this?

Sorry if this seems trivial, I'm not used to web dev at all.

This is all running inside a VM using Homestead ("laravel/homestead": "^13.2") with the most recent version of Halite as well.

PHP: 8.1.3
Laravel Framework 9.23.0
Homestead: 13.2.1
Halite: 5.1

Nathan-Furnal avatar Aug 10 '22 11:08 Nathan-Furnal

I could fix this issue by wrapping the handle into a new MutableFile, the decrypted file was then successfully sent thanks to a stream download. I'll leave it open in case though, since I don't believe the type error was intended.

(Solution to my problem here).

$decipheredName = Crypto::decrypt($file->name, $enc_key)->getString();
$filepath = storage_path().'/app/'.$file->name;
$stream = new MutableFile(fopen('php://output', 'wb'));
ob_start();
File::decrypt($filepath, $stream, $enc_key);
return response()->streamDownload(function() {
    echo ob_get_clean();
    } ,$decipheredName);

Nathan-Furnal avatar Aug 10 '22 12:08 Nathan-Furnal