Skip to content

Commit

Permalink
Use absolute path in decryption command.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacastelnuovo committed May 1, 2024
1 parent 53b52a3 commit 52c5cf2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 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
16 changes: 9 additions & 7 deletions src/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class PrivateKey
{
private string $privateKey;

public function __construct(string $privateKey = '')
{
if (! $privateKey) {
if (!$privateKey) {
$result = Process::pipe([
'age-keygen',
'grep -E "^AGE-SECRET-KEY-[A-Za-z0-9]{59}$"',
Expand All @@ -28,7 +29,7 @@ public function __construct(string $privateKey = '')

$privateKey = str($privateKey)->trim();

if (! $privateKey->startsWith('AGE-SECRET-KEY-') || $privateKey->length() !== 74) {
if (!$privateKey->startsWith('AGE-SECRET-KEY-') || $privateKey->length() !== 74) {
throw new Exception('Invalid private key provided!');
}

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 52c5cf2

Please sign in to comment.