Skip to content

Commit 272ca6b

Browse files
authored
Merge pull request #6 from cleverage/bugfix/fix-blank-decryption
Fix a bug when decrypting blank string
2 parents ef41dc9 + 9bcfa97 commit 272ca6b

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/Encryption/AbstractEncryptionAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function generateKey(): string
6969
public function parseNonce(string &$message): string
7070
{
7171
if (mb_strlen($message, static::ENCODING) < $this->getNonceSize()) {
72-
throw new BadNonceException('Unable to parse nounce from message');
72+
throw new BadNonceException('Unable to parse nounce from message "'.$message.'"');
7373
}
7474
$nonce = mb_substr($message, 0, $this->getNonceSize(), static::ENCODING);
7575
$message = mb_substr($message, $this->getNonceSize(), null, static::ENCODING);

src/Manager/EncryptionManager.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ public function encryptString(string $string, string $nonce = null): string
120120
*/
121121
public function decryptString(string $encryptedString, string $nonce = null): string
122122
{
123-
if ($nonce === null) {
124-
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
123+
// Do not try to decrypt blank string
124+
if ($encryptedString === '') {
125+
return '';
125126
}
126127

127128
try {
129+
if ($nonce === null) {
130+
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
131+
}
128132
$decrypted = $this->encryptionAdapter->decrypt(
129133
$encryptedString,
130134
$nonce,
@@ -134,10 +138,10 @@ public function decryptString(string $encryptedString, string $nonce = null): st
134138
if ($this->throwExceptions) {
135139
throw $exception;
136140
}
137-
141+
138142
return '';
139143
}
140-
144+
141145
return rtrim($decrypted, "\0");
142146
}
143147

tests/PHPUnit/Adapter/Aes256GcmSodiumEncryptionAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getDataProvider(): array
3737
[bin2hex(random_bytes(400))],
3838
[bin2hex(random_bytes(1000))],
3939
[bin2hex(random_bytes(10000))],
40+
[''],
4041
];
4142
}
4243

0 commit comments

Comments
 (0)