Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Oct 3, 2023
1 parent 59d8462 commit cf8e5f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/PubNub/Crypto/AesCbcCryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function encrypt(string $text, ?string $cipherKey = null): CryptoPayload
return new CryptoPayload($encrypted, $iv, self::CRYPTOR_ID);
}

public function decrypt(CryptoPayload $payload, ?string $cipherKey = null): string
public function decrypt(CryptoPayload $payload, ?string $cipherKey = null)
{
$text = $payload->getData();
$secret = $this->getSecret($cipherKey);
Expand Down
2 changes: 1 addition & 1 deletion src/PubNub/Crypto/Cryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ abstract class Cryptor
public const CRYPTOR_ID = null;

abstract public function encrypt(string $text, ?string $cipherKey = null): CryptoPayload;
abstract public function decrypt(CryptoPayload $payload, ?string $cipherKey = null): string;
abstract public function decrypt(CryptoPayload $payload, ?string $cipherKey = null);
}
2 changes: 1 addition & 1 deletion src/PubNub/Crypto/LegacyCryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function encrypt(string $text, ?string $cipherKey = null): Payload
return new Payload($encryptedWithIV, '', self::CRYPTOR_ID);
}

public function decrypt(Payload $payload, ?string $cipherKey = null): string
public function decrypt(Payload $payload, ?string $cipherKey = null)
{
$text = $payload->getData();
if (strlen($text) === 0) {
Expand Down
30 changes: 25 additions & 5 deletions src/PubNub/CryptoModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function registerCryptor(Cryptor $cryptor, ?string $cryptorId = null): se
return $this;
}

protected function stringify(mixed $data): string
protected function stringify($data): string
{
if (is_string($data)) {
return $data;
Expand All @@ -57,7 +57,7 @@ protected function stringify(mixed $data): string
}
}

public function encrypt(mixed $data, ?string $cryptorId = null): string
public function encrypt($data, ?string $cryptorId = null): string
{
if (($data) == '') {
throw new PubNubResponseParsingException("Encryption error: message is empty");
Expand All @@ -70,12 +70,26 @@ public function encrypt(mixed $data, ?string $cryptorId = null): string
return base64_encode($header . $cryptoPayload->getData());
}

public function decrypt(string $input)
public function decrypt($cipherText)
{
if (strlen($input) == '') {
if (is_array($cipherText)) {
if (array_key_exists("pn_other", $cipherText)) {
$cipherText = $cipherText["pn_other"];
} else {
if (is_array($cipherText)) {
throw new PubNubResponseParsingException("Decryption error: message is not a string");
} else {
throw new PubNubResponseParsingException("Decryption error: pn_other object key missing");
}
}
} elseif (!is_string($cipherText)) {
throw new PubNubResponseParsingException("Decryption error: message is not a string or object");
}

if (strlen($cipherText) == '') {
throw new PubNubResponseParsingException("Decryption error: message is empty");
}
$data = base64_decode($input);
$data = base64_decode($cipherText);
$header = $this->decodeHeader($data);

if (!$this->cryptorMap[$header->getCryptorId()]) {
Expand Down Expand Up @@ -157,4 +171,10 @@ public static function aesCbcCryptor(string $cipherKey, bool $useRandomIV): self
aesCbcCryptor::CRYPTOR_ID
);
}

// for backward compatibility
public function getCipherKey()
{
return $this->cryptorMap[$this->defaultCryptorId]->getCipherKey();
}
}

0 comments on commit cf8e5f0

Please sign in to comment.