Esempio n. 1
0
 public function testSetSalt()
 {
     $salt = str_repeat('a', $this->blockCipher->getCipher()->getSaltSize() + 2);
     $result = $this->blockCipher->setSalt($salt);
     $this->assertEquals($result, $this->blockCipher);
     $this->assertEquals(substr($salt, 0, $this->blockCipher->getCipher()->getSaltSize()), $this->blockCipher->getSalt());
     $this->assertEquals($salt, $this->blockCipher->getOriginalSalt());
 }
 /**
  * Encrypt using a keyrings
  *
  * @param string $plaintext
  * @param array|string $keys
  * @return string
  * @throws RuntimeException
  */
 public function encrypt($plaintext, $keys = null)
 {
     // generate a random session key
     $sessionKey = Rand::getBytes($this->bCipher->getCipher()->getKeySize());
     // encrypt the plaintext with blockcipher algorithm
     $this->bCipher->setKey($sessionKey);
     $ciphertext = $this->bCipher->encrypt($plaintext);
     if (!is_array($keys)) {
         $keys = ['' => $keys];
     }
     $encKeys = '';
     // encrypt the session key with public keys
     foreach ($keys as $id => $pubkey) {
         if (!$pubkey instanceof PubKey && !is_string($pubkey)) {
             throw new Exception\RuntimeException(sprintf("The public key must be a string in PEM format or an instance of %s", PubKey::class));
         }
         $pubkey = is_string($pubkey) ? new PubKey($pubkey) : $pubkey;
         $encKeys .= sprintf("%s:%s:", base64_encode($id), base64_encode($this->rsa->encrypt($sessionKey, $pubkey)));
     }
     return $encKeys . ';' . $ciphertext;
 }
Esempio n. 3
0
 public function testFactoryEmptyOptions()
 {
     $this->blockCipher = BlockCipher::factory('mcrypt');
     $this->assertTrue($this->blockCipher->getCipher() instanceof Mcrypt);
 }