Skip to content

Commit

Permalink
Implement Encoder to Cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
IlicMiljan committed Mar 15, 2024
1 parent 6a22674 commit dff4e0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/Cipher/AdvancedEncryptionStandardCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use IlicMiljan\SecureProps\Cipher\Exception\FailedEncryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedGeneratingInitializationVector;
use IlicMiljan\SecureProps\Cipher\Exception\InvalidKeyLength;
use IlicMiljan\SecureProps\Encoder\Base64Encoder;
use IlicMiljan\SecureProps\Encoder\Encoder;
use SensitiveParameter;

class AdvancedEncryptionStandardCipher implements Cipher
Expand All @@ -15,11 +17,18 @@ class AdvancedEncryptionStandardCipher implements Cipher
private const TAG_LENGTH = 16;
private const KEY_LENGTH = 32;

private Encoder $encoder;

public function __construct(
#[SensitiveParameter]
private string $key
private string $key,
?Encoder $encoder = null
) {
$this->validateKey($key);

if ($encoder === null) {
$this->encoder = new Base64Encoder();
}
}

/**
Expand All @@ -35,15 +44,16 @@ public function encrypt(#[SensitiveParameter] string $string): string
throw new FailedEncryptingValue();
}

return base64_encode($iv . $encryptedString . $tag);
return $this->encoder->encode($iv . $encryptedString . $tag);
}

/**
* @inheritDoc
*
*/
public function decrypt(#[SensitiveParameter] string $string): string
{
$data = base64_decode($string);
$data = $this->encoder->decode($string);

$ivLength = $this->calculateInitializationVectorLength(self::CIPHER);

Expand Down
14 changes: 11 additions & 3 deletions src/Cipher/AsymmetricEncryptionCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

use IlicMiljan\SecureProps\Cipher\Exception\FailedDecryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedEncryptingValue;
use IlicMiljan\SecureProps\Encoder\Base64Encoder;
use IlicMiljan\SecureProps\Encoder\Encoder;
use SensitiveParameter;

class AsymmetricEncryptionCipher implements Cipher
{
private Encoder $encoder;

public function __construct(
private string $publicKey,
private string $privateKey
private string $privateKey,
?Encoder $encoder = null
) {
if ($encoder === null) {
$this->encoder = new Base64Encoder();
}
}

/**
Expand All @@ -23,15 +31,15 @@ public function encrypt(#[SensitiveParameter] string $string): string
throw new FailedEncryptingValue();
}

return base64_encode($encrypted);
return $this->encoder->encode($encrypted);
}

/**
* @inheritDoc
*/
public function decrypt(#[SensitiveParameter] string $string): string
{
$data = base64_decode($string);
$data = $this->encoder->decode($string);

if (!openssl_private_decrypt($data, $decrypted, $this->privateKey)) {
throw new FailedDecryptingValue();
Expand Down

0 comments on commit dff4e0e

Please sign in to comment.