This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
122beed
commit c7096ef
Showing
2 changed files
with
42 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Castelnuovo\LaravelAge; | ||
|
||
use Exception; | ||
use Illuminate\Support\Facades\Process; | ||
|
||
class PublicKey | ||
{ | ||
public function __construct(private string $publicKey) | ||
{ | ||
$publicKeyStr = str($this->publicKey); | ||
if (! $publicKeyStr->startsWith('age') || ! $publicKeyStr->length() === 62) { | ||
throw new Exception('Invalid public key provided!'); | ||
} | ||
} | ||
|
||
public function encode(): string | ||
{ | ||
return $this->publicKey; | ||
} | ||
|
||
public function encrypt(string $message): string | ||
{ | ||
$result = Process::pipe([ | ||
"echo {$message}", | ||
"age -r {$this->publicKey}", | ||
]); | ||
|
||
if ($result->failed()) { | ||
throw new Exception('Failed to encrypt message!'); | ||
} | ||
|
||
return base64_encode($result->output()); | ||
} | ||
} |