Skip to content

Commit

Permalink
Merge pull request #5 from castelnuovo/temporary-directory
Browse files Browse the repository at this point in the history
Create temporary directory
  • Loading branch information
lucacastelnuovo authored May 1, 2024
2 parents 53b52a3 + 32f012c commit dd349ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
],
"require": {
"php": "^8.2",
"illuminate/contracts": "^10.0|^11.0",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0|^11.0"
"spatie/temporary-directory": "^2.2"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
12 changes: 7 additions & 5 deletions src/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class PrivateKey
{
Expand Down Expand Up @@ -57,13 +58,14 @@ public function getPublicKey(): PublicKey
*/
public function decrypt(string $message, bool $base64): string
{
$ulid = Str::ulid();
Storage::put($ulid, $base64 ? base64_decode($message) : $message);
$dir = TemporaryDirectory::make();
$disk = Storage::build(['driver' => 'local', 'root' => $dir->path()]);

$path = Storage::path($ulid);
$result = Process::input($this->encode())->run("age -d -i - {$path}");
$ulid = Str::ulid();
$disk->put($ulid, $base64 ? base64_decode($message) : $message);

Storage::delete($ulid);
$result = Process::input($this->encode())->run("age -d -i - {$dir->path($ulid)}");
$dir->delete();

if ($result->failed()) {
throw new Exception('Failed to decrypt message!');
Expand Down

0 comments on commit dd349ec

Please sign in to comment.