Skip to content

Commit

Permalink
Merge pull request #6 from cleverage/bugfix/fix-blank-decryption
Browse files Browse the repository at this point in the history
Fix a bug when decrypting blank string
  • Loading branch information
johnkrovitch authored Dec 1, 2020
2 parents ef41dc9 + 9bcfa97 commit 272ca6b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Encryption/AbstractEncryptionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function generateKey(): string
public function parseNonce(string &$message): string
{
if (mb_strlen($message, static::ENCODING) < $this->getNonceSize()) {
throw new BadNonceException('Unable to parse nounce from message');
throw new BadNonceException('Unable to parse nounce from message "'.$message.'"');
}
$nonce = mb_substr($message, 0, $this->getNonceSize(), static::ENCODING);
$message = mb_substr($message, $this->getNonceSize(), null, static::ENCODING);
Expand Down
12 changes: 8 additions & 4 deletions src/Manager/EncryptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ public function encryptString(string $string, string $nonce = null): string
*/
public function decryptString(string $encryptedString, string $nonce = null): string
{
if ($nonce === null) {
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
// Do not try to decrypt blank string
if ($encryptedString === '') {
return '';
}

try {
if ($nonce === null) {
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
}
$decrypted = $this->encryptionAdapter->decrypt(
$encryptedString,
$nonce,
Expand All @@ -134,10 +138,10 @@ public function decryptString(string $encryptedString, string $nonce = null): st
if ($this->throwExceptions) {
throw $exception;
}

return '';
}

return rtrim($decrypted, "\0");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getDataProvider(): array
[bin2hex(random_bytes(400))],
[bin2hex(random_bytes(1000))],
[bin2hex(random_bytes(10000))],
[''],
];
}

Expand Down

0 comments on commit 272ca6b

Please sign in to comment.