Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Create PublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacastelnuovo committed Mar 1, 2024
1 parent 122beed commit c7096ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/LaravelAge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class LaravelAge
public function __construct(private ?PrivateKey $privateKey = null, private ?PublicKey $publicKey = null)

Check failure on line 9 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $privateKey of method Castelnuovo\LaravelAge\LaravelAge::__construct() has invalid type Castelnuovo\LaravelAge\PrivateKey.

Check failure on line 9 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Castelnuovo\LaravelAge\LaravelAge::$privateKey has unknown class Castelnuovo\LaravelAge\PrivateKey as its type.
{
/* If no keys are provided, use the one from .env or create a pair. */
if (!isset($privateKey) && !isset($publicKey)) {
if (! isset($privateKey) && ! isset($publicKey)) {
$this->privateKey = new PrivateKey(config('laravel-age.identity'));

Check failure on line 13 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Instantiated class Castelnuovo\LaravelAge\PrivateKey not found.
$this->publicKey = $this->privateKey->getPublicKey();

Check failure on line 14 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getPublicKey() on an unknown class Castelnuovo\LaravelAge\PrivateKey.
}

/* If only the private key is provided, generate the public key. */
if (isset($privateKey) && !isset($publicKey)) {
if (isset($privateKey) && ! isset($publicKey)) {
$this->publicKey = $privateKey->getPublicKey();

Check failure on line 19 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getPublicKey() on an unknown class Castelnuovo\LaravelAge\PrivateKey.
}
}
Expand All @@ -29,7 +29,7 @@ public static function generateKeypair(): LaravelAge

public function getPublicKey(): PublicKey
{
if (!isset($this->publicKey)) {
if (! isset($this->publicKey)) {
throw new Exception('Public key not set!');
}

Expand All @@ -38,7 +38,7 @@ public function getPublicKey(): PublicKey

public function getPrivateKey(): PrivateKey

Check failure on line 39 in src/LaravelAge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Castelnuovo\LaravelAge\LaravelAge::getPrivateKey() has invalid return type Castelnuovo\LaravelAge\PrivateKey.
{
if (!isset($this->privateKey)) {
if (! isset($this->privateKey)) {
throw new Exception('Private key not set!');
}

Expand All @@ -47,7 +47,7 @@ public function getPrivateKey(): PrivateKey

public function encrypt(string $message): string
{
if (!isset($this->publicKey)) {
if (! isset($this->publicKey)) {
throw new Exception('Public key not set!');
}

Expand All @@ -56,7 +56,7 @@ public function encrypt(string $message): string

public function decrypt(string $message): string
{
if (!isset($this->privateKey)) {
if (! isset($this->privateKey)) {
throw new Exception('Private key not set!');
}

Expand Down
36 changes: 36 additions & 0 deletions src/PublicKey.php
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) {

Check failure on line 13 in src/PublicKey.php

View workflow job for this annotation

GitHub Actions / phpstan

Strict comparison using === between bool and 62 will always evaluate to false.
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());
}
}

0 comments on commit c7096ef

Please sign in to comment.