Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypting a file to output buffer fails #182

Open
Nathan-Furnal opened this issue Aug 10, 2022 · 1 comment
Open

Decrypting a file to output buffer fails #182

Nathan-Furnal opened this issue Aug 10, 2022 · 1 comment

Comments

@Nathan-Furnal
Copy link

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
Copy link
Author

Nathan-Furnal commented Aug 10, 2022

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant