Ejemplo n.º 1
0
 public function testNonAESEncryptionSupport()
 {
     $blowfishParams = Crypto::getDefaultParams(['key' => Crypto::generateRandomKey(128), 'algorithm' => 'bf', 'mode' => 'ecb']);
     $encrypted = Crypto::encrypt('test', $blowfishParams);
     $decrypted = Crypto::decrypt($encrypted, $blowfishParams);
     $this->assertNotEquals($encrypted, $decrypted);
     $this->assertEquals('test', $decrypted);
     try {
         Crypto::getDefaultParams(['key' => Crypto::generateRandomKey(), 'algorithm' => 'fake']);
     } catch (\Exception $ex) {
         $this->assertInstanceOf('Ably\\Exceptions\\AblyException', $ex, 'Expected to raise an exception on unknown encryption mode');
     }
 }
Ejemplo n.º 2
0
 /**
  * Returns an encoded message as a stdClass ready for stringifying
  */
 protected function encode()
 {
     $msg = new \stdClass();
     if ($this->encoding) {
         $msg->encoding = $this->encoding;
         $msg->data = $this->data;
         return $msg;
     }
     if ($this->clientId) {
         $msg->clientId = $this->clientId;
     }
     $isBinary = false;
     $encodings = [];
     if (is_array($this->data) || $this->data instanceof \stdClass) {
         $encodings[] = 'json';
         $msg->data = json_encode($this->data);
     } else {
         if (is_string($this->data)) {
             if (mb_check_encoding($this->data, 'UTF-8')) {
                 // it's a UTF-8 string
                 $msg->data = $this->data;
             } else {
                 // not UTF-8, assuming it's a binary string
                 $msg->data = $this->data;
                 $isBinary = true;
             }
         } else {
             if (!isset($this->data) || $this->data === null) {
                 return $msg;
             } else {
                 throw new AblyException('Message data must be either, string, string with binary data, JSON-encodable array or object, or null.', 40003, 400);
             }
         }
     }
     if ($this->cipherParams) {
         if (!$isBinary) {
             $encodings[] = 'utf-8';
         }
         $msg->data = base64_encode(Crypto::encrypt($msg->data, $this->cipherParams));
         $encodings[] = 'cipher+' . $this->cipherParams->getAlgorithmString();
         $encodings[] = 'base64';
     } else {
         if ($isBinary) {
             $msg->data = base64_encode($this->data);
             $encodings[] = 'base64';
         }
     }
     if (count($encodings)) {
         $msg->encoding = implode('/', $encodings);
     } else {
         $msg->encoding = '';
     }
     return $msg;
 }