diff --git a/composer.json b/composer.json index 67df64e..e34bf2b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/PrivateKey.php b/src/PrivateKey.php index 0577eb2..f2d1574 100644 --- a/src/PrivateKey.php +++ b/src/PrivateKey.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use Spatie\TemporaryDirectory\TemporaryDirectory; class PrivateKey { @@ -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!');