Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Castelnuovo committed Apr 30, 2021
1 parent c086a18 commit 213bc49
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/Crypto/Helpers/Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(

public function getSecretKey(): SignatureSecretKey | EncryptionSecretKey
{
if (! $this->secretKey) {
if (!$this->secretKey) {
throw new KeyException(
message: 'secretKey not set'
);
Expand All @@ -37,19 +37,19 @@ public function getPublicKey(): SignaturePublicKey | EncryptionPublicKey

public function exportSecretKey(): string
{
if (! $this->secretKey) {
if (!$this->secretKey) {
return null;
}

return KeyFactory::export(
key: $this->secretKey
key: $this->secretKey
)->getString();
}

public function exportPublicKey(): string
{
return KeyFactory::export(
key: $this->publicKey
key: $this->publicKey
)->getString();
}
}
104 changes: 52 additions & 52 deletions src/Crypto/Models/AsymmetricKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,58 @@ final class AsymmetricKey extends KeyProvider
private Keypair $authentication;
private Keypair $encryption;

/**
* Export private key
*/
public function export(): string
{
$keypair = [
'authentication' => [
'publicKey' => $this->authentication->exportPublicKey(),
'secretKey' => $this->authentication->exportSecretKey(),
],
'encryption' => [
'publicKey' => $this->encryption->exportPublicKey(),
'secretKey' => $this->encryption->exportSecretKey(),
],
];

return base64_encode(
json_encode($keypair)
);
}

/**
* Export public key
*/
public function exportPublic(): string
{
$keypair = [
'authentication' => [
'publicKey' => $this->authentication->exportPublicKey(),
'secretKey' => null,
],
'encryption' => [
'publicKey' => $this->encryption->exportPublicKey(),
'secretKey' => null,
],
];

return base64_encode(
json_encode($keypair)
);
}

public function getAuthentication(): Keypair
{
return $this->authentication;
}

public function getEncryption(): Keypair
{
return $this->encryption;
}

/**
* Generate encryption keypair
*/
Expand Down Expand Up @@ -70,56 +122,4 @@ protected function import(string $encodedKey): void
) : null // If secretKey isset import it otherwise set null
);
}

/**
* Export private key
*/
public function export(): string
{
$keypair = [
'authentication' => [
'publicKey' => $this->authentication->exportPublicKey(),
'secretKey' => $this->authentication->exportSecretKey(),
],
'encryption' => [
'publicKey' => $this->encryption->exportPublicKey(),
'secretKey' => $this->encryption->exportSecretKey(),
],
];

return base64_encode(
json_encode($keypair)
);
}

/**
* Export public key
*/
public function exportPublic(): string
{
$keypair = [
'authentication' => [
'publicKey' => $this->authentication->exportPublicKey(),
'secretKey' => null,
],
'encryption' => [
'publicKey' => $this->encryption->exportPublicKey(),
'secretKey' => null,
],
];

return base64_encode(
json_encode($keypair)
);
}

public function getAuthentication(): Keypair
{
return $this->authentication;
}

public function getEncryption(): Keypair
{
return $this->encryption;
}
}
36 changes: 18 additions & 18 deletions src/Crypto/Models/SymmetricKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@ final class SymmetricKey extends KeyProvider
{
private string $keystring;

/**
* Generate key
*/
protected function genKey(): void
{
$key = KeyFactory::generateEncryptionKey();

$this->keystring = KeyFactory::export(key: $key)->getString();
}

/**
* Import key
*/
protected function import(string $encodedKey): void
{
$this->keystring = base64_decode($encodedKey);
}

/**
* Export key
*/
Expand All @@ -53,4 +35,22 @@ public function getEncryption(): EncryptionKey
keyData: new HiddenString(value: $this->keystring)
);
}

/**
* Generate key
*/
protected function genKey(): void
{
$key = KeyFactory::generateEncryptionKey();

$this->keystring = KeyFactory::export(key: $key)->getString();
}

/**
* Import key
*/
protected function import(string $encodedKey): void
{
$this->keystring = base64_decode($encodedKey);
}
}
24 changes: 10 additions & 14 deletions src/Crypto/Providers/KeyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
namespace CQ\Crypto\Providers;

use CQ\Crypto\Helpers\Keypair;
use ParagonIE\Halite\Asymmetric\EncryptionPublicKey;
use ParagonIE\Halite\Asymmetric\SignaturePublicKey;
use ParagonIE\Halite\Symmetric\AuthenticationKey;
use ParagonIE\Halite\Symmetric\EncryptionKey;
use ParagonIE\Halite\SignatureKeyPair;
use ParagonIE\Halite\EncryptionKeyPair;

abstract class KeyProvider
{
public function __construct(string | null $encodedKey = null)
{
if (! $encodedKey) {
if (!$encodedKey) {
return $this->genKey();
}

Expand All @@ -25,6 +21,15 @@ public function __construct(string | null $encodedKey = null)
);
}

/**
* Export (private) key
*/
abstract public function export(): string;

abstract public function getAuthentication(): AuthenticationKey | Keypair;

abstract public function getEncryption(): EncryptionKey | Keypair;

/**
* Generate key
*/
Expand All @@ -34,13 +39,4 @@ abstract protected function genKey(): void;
* Import key
*/
abstract protected function import(string $encodedKey): void;

/**
* Export (private) key
*/
abstract public function export(): string;

abstract public function getAuthentication(): AuthenticationKey | Keypair;

abstract public function getEncryption(): EncryptionKey | Keypair;
}

0 comments on commit 213bc49

Please sign in to comment.