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 2a91fd5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 19 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
5 changes: 4 additions & 1 deletion src/PubNub/PNConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PNConfiguration

private $usingUserId = null;

private string $cipherKey;

/**
* PNConfiguration constructor.
*/
Expand Down Expand Up @@ -122,7 +124,7 @@ public function setSecretKey($secretKey)
*/
public function getCipherKey()
{
return $this->getCrypto()->getCipherKey();
return $this->cipherKey;
}

public function isAesEnabled()
Expand All @@ -136,6 +138,7 @@ public function isAesEnabled()
*/
public function setCipherKey($cipherKey)
{
$this->cipherKey = $cipherKey;
if ($this->crypto == null) {
$this->crypto = CryptoModule::legacyCryptor($cipherKey, $this->getUseRandomIV());
} else {
Expand Down

0 comments on commit 2a91fd5

Please sign in to comment.