Skip to content

Commit

Permalink
Merge pull request #26 from CodeLieutenant/chore/keys
Browse files Browse the repository at this point in the history
Refactor Keys **BREAKING**
  • Loading branch information
CodeLieutenant authored Mar 5, 2024
2 parents c61a569 + 4679f2b commit 0d1684b
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 113 deletions.
4 changes: 2 additions & 2 deletions benchmarks/EncryptionBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function providerChaCha20Crypto(): Generator
foreach ($encoders as $encoderName => $encoder) {
yield 'ChaCha20-' . $encoderName . '-' . $dataName => [
'data' => $dataValue,
'encrypter' => new XChaCha20Poly1305Encrypter(new KeyLoader(Random::bytes(32)), $logger, $encoder),
'encrypter' => new XChaCha20Poly1305Encrypter(new KeyKeyLoader(Random::bytes(32)), $logger, $encoder),
];
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public function providerAESGCMCrypto(): Generator
foreach ($encoders as $encoderName => $encoder) {
yield 'ChaCha20-' . $encoderName . '-' . $dataName => [
'data' => $dataValue,
'encrypter' => new AesGcm256Encrypter(new KeyLoader(Random::bytes(32)), $logger, $encoder),
'encrypter' => new AesGcm256Encrypter(new KeyKeyLoader(Random::bytes(32)), $logger, $encoder),
];
}
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/KeyLoader.php → benchmarks/KeyKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace CodeLieutenant\LaravelCrypto\Benchmarks;

use CodeLieutenant\LaravelCrypto\Keys\Loader;
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;

class KeyLoader implements Loader
class KeyKeyLoader implements KeyLoader
{
public function __construct(
private readonly string $key
Expand Down
4 changes: 2 additions & 2 deletions src/Console/GenerateCryptoKeysCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CodeLieutenant\LaravelCrypto\Console;

use CodeLieutenant\LaravelCrypto\Keys\Generators\AppKeyGenerator;
use CodeLieutenant\LaravelCrypto\Keys\Generators\Blake2bHashingKeyGenerator;
use CodeLieutenant\LaravelCrypto\Keys\Generators\Blake2BHashingKeyGenerator;
use CodeLieutenant\LaravelCrypto\Keys\Generators\EdDSASignerKeyGenerator;
use CodeLieutenant\LaravelCrypto\Keys\Generators\HmacKeyGenerator;
use Exception;
Expand All @@ -31,7 +31,7 @@ class GenerateCryptoKeysCommand extends Command
public function handle(
EdDSASignerKeyGenerator $edDSAGenerator,
AppKeyGenerator $appKeyGenerator,
Blake2bHashingKeyGenerator $blake2bKeyGenerator,
Blake2BHashingKeyGenerator $blake2bKeyGenerator,
HmacKeyGenerator $hmacKeyGenerator,
): int {
$show = $this->option('show');
Expand Down
18 changes: 0 additions & 18 deletions src/Contracts/KeyGeneration.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys\Generators;
namespace CodeLieutenant\LaravelCrypto\Contracts;

interface Generator
interface KeyGenerator
{
public function generate(?string $write): ?string;
}
4 changes: 2 additions & 2 deletions src/Keys/Loader.php → src/Contracts/KeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys;
namespace CodeLieutenant\LaravelCrypto\Contracts;

interface Loader
interface KeyLoader
{
public function getKey(): string|array;
}
14 changes: 4 additions & 10 deletions src/Encryption/AesGcm256Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

namespace CodeLieutenant\LaravelCrypto\Encryption;

use Exception;
use CodeLieutenant\LaravelCrypto\Contracts\Encoder;
use CodeLieutenant\LaravelCrypto\Contracts\KeyGeneration;
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
use CodeLieutenant\LaravelCrypto\Encoder\JsonEncoder;
use CodeLieutenant\LaravelCrypto\Keys\Loader;
use CodeLieutenant\LaravelCrypto\Support\Base64;
use CodeLieutenant\LaravelCrypto\Traits\Crypto;
use Exception;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Contracts\Encryption\StringEncrypter;
use Psr\Log\LoggerInterface;

final class AesGcm256Encrypter implements Encrypter, KeyGeneration, StringEncrypter
final class AesGcm256Encrypter implements Encrypter, StringEncrypter
{
use Crypto;

public function __construct(
private readonly Loader $keyLoader,
private readonly KeyLoader $keyLoader,
private readonly Encoder $encoder = new JsonEncoder(),
private readonly ?LoggerInterface $logger = null,
) {
Expand Down Expand Up @@ -70,11 +69,6 @@ public function decrypt($payload, $unserialize = true)
};
}

public static function generateKey(string $cipher): string
{
return sodium_crypto_aead_aes256gcm_keygen();
}

public static function nonceSize(): int
{
return SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES;
Expand Down
12 changes: 3 additions & 9 deletions src/Encryption/XChaCha20Poly1305Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace CodeLieutenant\LaravelCrypto\Encryption;

use CodeLieutenant\LaravelCrypto\Contracts\Encoder;
use CodeLieutenant\LaravelCrypto\Contracts\KeyGeneration;
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
use CodeLieutenant\LaravelCrypto\Encoder\JsonEncoder;
use CodeLieutenant\LaravelCrypto\Keys\Loader;
use CodeLieutenant\LaravelCrypto\Support\Base64;
use CodeLieutenant\LaravelCrypto\Traits\Crypto;
use Exception;
Expand All @@ -17,12 +16,12 @@
use Illuminate\Contracts\Encryption\StringEncrypter;
use Psr\Log\LoggerInterface;

final class XChaCha20Poly1305Encrypter implements Encrypter, KeyGeneration, StringEncrypter
final class XChaCha20Poly1305Encrypter implements Encrypter, StringEncrypter
{
use Crypto;

public function __construct(
private readonly Loader $keyLoader,
private readonly KeyLoader $keyLoader,
private readonly Encoder $encoder = new JsonEncoder(),
private readonly ?LoggerInterface $logger = null,
) {
Expand Down Expand Up @@ -81,11 +80,6 @@ public function decrypt($payload, $unserialize = true)
return $decrypted;
}

public static function generateKey(string $_): string
{
return sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
}

public static function nonceSize(): int
{
return SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;
Expand Down
13 changes: 0 additions & 13 deletions src/Keys/Blake2bHashingKey.php

This file was deleted.

7 changes: 4 additions & 3 deletions src/Keys/Generators/AppKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace CodeLieutenant\LaravelCrypto\Keys\Generators;

use CodeLieutenant\LaravelCrypto\Contracts\KeyGenerator;
use CodeLieutenant\LaravelCrypto\Encryption\AesGcm256Encrypter;
use CodeLieutenant\LaravelCrypto\Encryption\XChaCha20Poly1305Encrypter;
use CodeLieutenant\LaravelCrypto\Enums\Encryption;
use CodeLieutenant\LaravelCrypto\Traits\EnvKeySaver;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Encryption\Encrypter;

class AppKeyGenerator implements Generator
class AppKeyGenerator implements KeyGenerator
{
use EnvKeySaver;

Expand All @@ -31,8 +32,8 @@ public function generate(?string $write): ?string

$new = $this->formatKey(
match (Encryption::tryFrom($cipher)) {
Encryption::SodiumAES256GCM => AesGcm256Encrypter::generateKey($cipher),
Encryption::SodiumXChaCha20Poly1305 => XChaCha20Poly1305Encrypter::generateKey($cipher),
Encryption::SodiumAES256GCM => sodium_crypto_aead_aes256gcm_keygen(),
Encryption::SodiumXChaCha20Poly1305 => sodium_crypto_aead_xchacha20poly1305_ietf_keygen(),
default => Encrypter::generateKey($cipher),
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace CodeLieutenant\LaravelCrypto\Keys\Generators;

use CodeLieutenant\LaravelCrypto\Contracts\KeyGenerator;
use CodeLieutenant\LaravelCrypto\Support\Random;
use CodeLieutenant\LaravelCrypto\Traits\EnvKeySaver;
use Illuminate\Contracts\Config\Repository;

class Blake2bHashingKeyGenerator implements Generator
class Blake2BHashingKeyGenerator implements KeyGenerator
{
use EnvKeySaver;

Expand Down
3 changes: 2 additions & 1 deletion src/Keys/Generators/EdDSASignerKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace CodeLieutenant\LaravelCrypto\Keys\Generators;

use CodeLieutenant\LaravelCrypto\Contracts\KeyGenerator;
use Illuminate\Contracts\Config\Repository;
use Psr\Log\LoggerInterface;
use RuntimeException;
use SplFileObject;

class EdDSASignerKeyGenerator implements Generator
class EdDSASignerKeyGenerator implements KeyGenerator
{
private const CONFIG_KEY_PATH = 'crypto.signing.keys.eddsa';

Expand Down
2 changes: 1 addition & 1 deletion src/Keys/Generators/HmacKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CodeLieutenant\LaravelCrypto\Keys\Generators;

class HmacKeyGenerator extends Blake2bHashingKeyGenerator
class HmacKeyGenerator extends Blake2BHashingKeyGenerator
{
public const ENV = 'CRYPTO_HMAC_KEY';
public const CONFIG_KEY_PATH = 'crypto.signing.keys.hmac';
Expand Down
5 changes: 3 additions & 2 deletions src/Keys/AppKey.php → src/Keys/Loaders/AppKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys;
namespace CodeLieutenant\LaravelCrypto\Keys\Loaders;

use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
use CodeLieutenant\LaravelCrypto\Traits\LaravelKeyParser;
use Illuminate\Contracts\Config\Repository;

class AppKey implements Loader
class AppKeyLoader implements KeyLoader
{
use LaravelKeyParser;

Expand Down
11 changes: 11 additions & 0 deletions src/Keys/Loaders/Blake2BHashingKeyLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys\Loaders;


class Blake2BHashingKeyLoader extends AppKeyLoader
{
public const CONFIG_KEY_PATH = 'crypto.signing.keys.hmac';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys;
namespace CodeLieutenant\LaravelCrypto\Keys\Loaders;

use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Encryption\MissingAppKeyException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use SplFileObject;

class EdDSASignerKey implements Loader
class EdDSASignerKeyLoader implements KeyLoader
{
public const KEY_LENGTH = SODIUM_CRYPTO_SIGN_KEYPAIRBYTES;
public const PUBLIC_KEY_LENGTH = SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES;
Expand Down
4 changes: 2 additions & 2 deletions src/Keys/HmacKey.php → src/Keys/Loaders/HmacKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace CodeLieutenant\LaravelCrypto\Keys;
namespace CodeLieutenant\LaravelCrypto\Keys\Loaders;

class HmacKey extends AppKey
class HmacKeyLoader extends AppKeyLoader
{
public const CONFIG_KEY_PATH = 'crypto.signing.keys.hmac';
}
Loading

0 comments on commit 0d1684b

Please sign in to comment.