Skip to content

Commit 0d1684b

Browse files
Merge pull request #26 from CodeLieutenant/chore/keys
Refactor Keys **BREAKING**
2 parents c61a569 + 4679f2b commit 0d1684b

26 files changed

+100
-113
lines changed

benchmarks/EncryptionBench.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function providerChaCha20Crypto(): Generator
6262
foreach ($encoders as $encoderName => $encoder) {
6363
yield 'ChaCha20-' . $encoderName . '-' . $dataName => [
6464
'data' => $dataValue,
65-
'encrypter' => new XChaCha20Poly1305Encrypter(new KeyLoader(Random::bytes(32)), $logger, $encoder),
65+
'encrypter' => new XChaCha20Poly1305Encrypter(new KeyKeyLoader(Random::bytes(32)), $logger, $encoder),
6666
];
6767
}
6868
}
@@ -91,7 +91,7 @@ public function providerAESGCMCrypto(): Generator
9191
foreach ($encoders as $encoderName => $encoder) {
9292
yield 'ChaCha20-' . $encoderName . '-' . $dataName => [
9393
'data' => $dataValue,
94-
'encrypter' => new AesGcm256Encrypter(new KeyLoader(Random::bytes(32)), $logger, $encoder),
94+
'encrypter' => new AesGcm256Encrypter(new KeyKeyLoader(Random::bytes(32)), $logger, $encoder),
9595
];
9696
}
9797
}

benchmarks/KeyLoader.php renamed to benchmarks/KeyKeyLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace CodeLieutenant\LaravelCrypto\Benchmarks;
66

7-
use CodeLieutenant\LaravelCrypto\Keys\Loader;
7+
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
88

9-
class KeyLoader implements Loader
9+
class KeyKeyLoader implements KeyLoader
1010
{
1111
public function __construct(
1212
private readonly string $key

src/Console/GenerateCryptoKeysCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace CodeLieutenant\LaravelCrypto\Console;
66

77
use CodeLieutenant\LaravelCrypto\Keys\Generators\AppKeyGenerator;
8-
use CodeLieutenant\LaravelCrypto\Keys\Generators\Blake2bHashingKeyGenerator;
8+
use CodeLieutenant\LaravelCrypto\Keys\Generators\Blake2BHashingKeyGenerator;
99
use CodeLieutenant\LaravelCrypto\Keys\Generators\EdDSASignerKeyGenerator;
1010
use CodeLieutenant\LaravelCrypto\Keys\Generators\HmacKeyGenerator;
1111
use Exception;
@@ -31,7 +31,7 @@ class GenerateCryptoKeysCommand extends Command
3131
public function handle(
3232
EdDSASignerKeyGenerator $edDSAGenerator,
3333
AppKeyGenerator $appKeyGenerator,
34-
Blake2bHashingKeyGenerator $blake2bKeyGenerator,
34+
Blake2BHashingKeyGenerator $blake2bKeyGenerator,
3535
HmacKeyGenerator $hmacKeyGenerator,
3636
): int {
3737
$show = $this->option('show');

src/Contracts/KeyGeneration.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Keys/Generators/Generator.php renamed to src/Contracts/KeyGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace CodeLieutenant\LaravelCrypto\Keys\Generators;
5+
namespace CodeLieutenant\LaravelCrypto\Contracts;
66

7-
interface Generator
7+
interface KeyGenerator
88
{
99
public function generate(?string $write): ?string;
1010
}

src/Keys/Loader.php renamed to src/Contracts/KeyLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace CodeLieutenant\LaravelCrypto\Keys;
5+
namespace CodeLieutenant\LaravelCrypto\Contracts;
66

7-
interface Loader
7+
interface KeyLoader
88
{
99
public function getKey(): string|array;
1010
}

src/Encryption/AesGcm256Encrypter.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44

55
namespace CodeLieutenant\LaravelCrypto\Encryption;
66

7-
use Exception;
87
use CodeLieutenant\LaravelCrypto\Contracts\Encoder;
9-
use CodeLieutenant\LaravelCrypto\Contracts\KeyGeneration;
8+
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
109
use CodeLieutenant\LaravelCrypto\Encoder\JsonEncoder;
11-
use CodeLieutenant\LaravelCrypto\Keys\Loader;
1210
use CodeLieutenant\LaravelCrypto\Support\Base64;
1311
use CodeLieutenant\LaravelCrypto\Traits\Crypto;
12+
use Exception;
1413
use Illuminate\Contracts\Encryption\DecryptException;
1514
use Illuminate\Contracts\Encryption\Encrypter;
1615
use Illuminate\Contracts\Encryption\EncryptException;
1716
use Illuminate\Contracts\Encryption\StringEncrypter;
1817
use Psr\Log\LoggerInterface;
1918

20-
final class AesGcm256Encrypter implements Encrypter, KeyGeneration, StringEncrypter
19+
final class AesGcm256Encrypter implements Encrypter, StringEncrypter
2120
{
2221
use Crypto;
2322

2423
public function __construct(
25-
private readonly Loader $keyLoader,
24+
private readonly KeyLoader $keyLoader,
2625
private readonly Encoder $encoder = new JsonEncoder(),
2726
private readonly ?LoggerInterface $logger = null,
2827
) {
@@ -70,11 +69,6 @@ public function decrypt($payload, $unserialize = true)
7069
};
7170
}
7271

73-
public static function generateKey(string $cipher): string
74-
{
75-
return sodium_crypto_aead_aes256gcm_keygen();
76-
}
77-
7872
public static function nonceSize(): int
7973
{
8074
return SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES;

src/Encryption/XChaCha20Poly1305Encrypter.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
namespace CodeLieutenant\LaravelCrypto\Encryption;
66

77
use CodeLieutenant\LaravelCrypto\Contracts\Encoder;
8-
use CodeLieutenant\LaravelCrypto\Contracts\KeyGeneration;
8+
use CodeLieutenant\LaravelCrypto\Contracts\KeyLoader;
99
use CodeLieutenant\LaravelCrypto\Encoder\JsonEncoder;
10-
use CodeLieutenant\LaravelCrypto\Keys\Loader;
1110
use CodeLieutenant\LaravelCrypto\Support\Base64;
1211
use CodeLieutenant\LaravelCrypto\Traits\Crypto;
1312
use Exception;
@@ -17,12 +16,12 @@
1716
use Illuminate\Contracts\Encryption\StringEncrypter;
1817
use Psr\Log\LoggerInterface;
1918

20-
final class XChaCha20Poly1305Encrypter implements Encrypter, KeyGeneration, StringEncrypter
19+
final class XChaCha20Poly1305Encrypter implements Encrypter, StringEncrypter
2120
{
2221
use Crypto;
2322

2423
public function __construct(
25-
private readonly Loader $keyLoader,
24+
private readonly KeyLoader $keyLoader,
2625
private readonly Encoder $encoder = new JsonEncoder(),
2726
private readonly ?LoggerInterface $logger = null,
2827
) {
@@ -81,11 +80,6 @@ public function decrypt($payload, $unserialize = true)
8180
return $decrypted;
8281
}
8382

84-
public static function generateKey(string $_): string
85-
{
86-
return sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
87-
}
88-
8983
public static function nonceSize(): int
9084
{
9185
return SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;

src/Keys/Blake2bHashingKey.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Keys/Generators/AppKeyGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace CodeLieutenant\LaravelCrypto\Keys\Generators;
66

7+
use CodeLieutenant\LaravelCrypto\Contracts\KeyGenerator;
78
use CodeLieutenant\LaravelCrypto\Encryption\AesGcm256Encrypter;
89
use CodeLieutenant\LaravelCrypto\Encryption\XChaCha20Poly1305Encrypter;
910
use CodeLieutenant\LaravelCrypto\Enums\Encryption;
1011
use CodeLieutenant\LaravelCrypto\Traits\EnvKeySaver;
1112
use Illuminate\Contracts\Config\Repository;
1213
use Illuminate\Encryption\Encrypter;
1314

14-
class AppKeyGenerator implements Generator
15+
class AppKeyGenerator implements KeyGenerator
1516
{
1617
use EnvKeySaver;
1718

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

3233
$new = $this->formatKey(
3334
match (Encryption::tryFrom($cipher)) {
34-
Encryption::SodiumAES256GCM => AesGcm256Encrypter::generateKey($cipher),
35-
Encryption::SodiumXChaCha20Poly1305 => XChaCha20Poly1305Encrypter::generateKey($cipher),
35+
Encryption::SodiumAES256GCM => sodium_crypto_aead_aes256gcm_keygen(),
36+
Encryption::SodiumXChaCha20Poly1305 => sodium_crypto_aead_xchacha20poly1305_ietf_keygen(),
3637
default => Encrypter::generateKey($cipher),
3738
}
3839
);

0 commit comments

Comments
 (0)