Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Encryptor/DefaultEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
private readonly string $key,
private readonly string $cipher = 'aes-256-cbc',
) {
if (\strlen($key) < 16) {
throw new \InvalidArgumentException('Encryption key must be at least 16 bytes.');
}

if (!\in_array($cipher, \openssl_get_cipher_methods(), true)) {
throw new \InvalidArgumentException(\sprintf('Invalid cipher "%s".', $cipher));
}
Expand All @@ -23,12 +27,18 @@
throw new \RuntimeException(\sprintf('Could not determine IV length for cipher "%s".', $this->cipher));
}

return \base64_encode(\random_bytes($length));

Check warning on line 30 in src/Encryptor/DefaultEncryptor.php

View workflow job for this annotation

GitHub Actions / build (8.4)

ArgumentTypeCoercion

src/Encryptor/DefaultEncryptor.php:30:45: ArgumentTypeCoercion: Argument 1 of random_bytes expects int<1, max>, but parent type int provided (see https://psalm.dev/193)

Check warning on line 30 in src/Encryptor/DefaultEncryptor.php

View workflow job for this annotation

GitHub Actions / build (8.1)

ArgumentTypeCoercion

src/Encryptor/DefaultEncryptor.php:30:45: ArgumentTypeCoercion: Argument 1 of random_bytes expects int<1, max>, but parent type int provided (see https://psalm.dev/193)

Check warning on line 30 in src/Encryptor/DefaultEncryptor.php

View workflow job for this annotation

GitHub Actions / build (8.2)

ArgumentTypeCoercion

src/Encryptor/DefaultEncryptor.php:30:45: ArgumentTypeCoercion: Argument 1 of random_bytes expects int<1, max>, but parent type int provided (see https://psalm.dev/193)
}

public function encrypt(string $data, string $iv): string
{
$encrypted = \openssl_encrypt($data, $this->cipher, $this->key, 0, \base64_decode($iv));
$decodedIv = \base64_decode($iv, true);

if ($decodedIv === false) {
throw new \InvalidArgumentException('Invalid IV: base64 decoding failed.');
}

$encrypted = \openssl_encrypt($data, $this->cipher, $this->key, 0, $decodedIv);

if ($encrypted === false) {
throw new \RuntimeException('Encryption failed.');
Expand Down
Loading