Ejemplo n.º 1
0
 /**
  * RSA instance factory
  *
  * @param  array|Traversable $options
  * @return Rsa
  * @throws Rsa\Exception\RuntimeException
  * @throws Rsa\Exception\InvalidArgumentException
  */
 public static function factory($options)
 {
     if (!extension_loaded('openssl')) {
         throw new Exception\RuntimeException('Can not create Zend\\Crypt\\PublicKey\\Rsa; openssl extension to be loaded');
     }
     if ($options instanceof Traversable) {
         $options = ArrayUtils::iteratorToArray($options);
     } elseif (!is_array($options)) {
         throw new Exception\InvalidArgumentException('The options parameter must be an array or a Traversable');
     }
     $privateKey = null;
     $passPhrase = isset($options['pass_phrase']) ? $options['pass_phrase'] : null;
     if (isset($options['private_key'])) {
         if (is_file($options['private_key'])) {
             $privateKey = Rsa\PrivateKey::fromFile($options['private_key'], $passPhrase);
         } elseif (is_string($options['private_key'])) {
             $privateKey = new Rsa\PrivateKey($options['private_key'], $passPhrase);
         } else {
             throw new Exception\InvalidArgumentException('Parameter "private_key" must be PEM formatted string or path to key file');
         }
         unset($options['private_key']);
     }
     $publicKey = null;
     if (isset($options['public_key'])) {
         if (is_file($options['public_key'])) {
             $publicKey = Rsa\PublicKey::fromFile($options['public_key']);
         } elseif (is_string($options['public_key'])) {
             $publicKey = new Rsa\PublicKey($options['public_key']);
         } else {
             throw new Exception\InvalidArgumentException('Parameter "public_key" must be PEM/certificate string or path to key/certificate file');
         }
         unset($options['public_key']);
     }
     $options = new RsaOptions($options);
     if ($privateKey instanceof Rsa\PrivateKey) {
         $options->setPrivateKey($privateKey);
     }
     if ($publicKey instanceof Rsa\PublicKey) {
         $options->setPublicKey($publicKey);
     }
     return new Rsa($options);
 }
Ejemplo n.º 2
0
 public function testPrivateKeyInstanceCreation()
 {
     $privateKey = Rsa\PrivateKey::fromFile($this->testPemFile);
     $this->assertInstanceOf('Zend\\Crypt\\PublicKey\\Rsa\\PrivateKey', $privateKey);
     $privateKey = new Rsa\PrivateKey($this->testPemString);
     $this->assertInstanceOf('Zend\\Crypt\\PublicKey\\Rsa\\PrivateKey', $privateKey);
 }