Exemple #1
0
 public function testKeyGenerationWithDefaults()
 {
     if (!$this->openSslConf) {
         $this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
     }
     $rsa = new Rsa();
     $rsa->getOptions()->generateKeys();
     $this->assertInstanceOf('Zend\\Crypt\\PublicKey\\Rsa\\PrivateKey', $rsa->getOptions()->getPrivateKey());
     $this->assertInstanceOf('Zend\\Crypt\\PublicKey\\Rsa\\PublicKey', $rsa->getOptions()->getPublicKey());
 }
Exemple #2
0
 public function testEncryptionUsingPrivateKeyBase64Encryption()
 {
     $rsa       = new Rsa(new RsaOptions(array('pem_string' => $this->_testPemString)));
     $encrypted = $rsa->encrypt('1234567890', $rsa->getOptions()->getPrivateKey(), Rsa::FORMAT_BASE64);
     $this->assertEquals(
         '1234567890',
         $rsa->decrypt($encrypted, $rsa->getOptions()->getPublicKey(), Rsa::FORMAT_BASE64)
     );
 }
 /**
  * Persist a key pair using the provided name for identification
  * @param Rsa $rsa
  * @param string $name
  * @return KeyStorageInterface $this
  * @throws Exception\InvalidArgumentException if no key pair name is provided
  * @throws Exception\RuntimeException if we fail to write 
  * 
  */
 public function set(Rsa $rsa, $name = self::DEFAULT_KEY_NAME)
 {
     if (empty($name) || !is_string($name)) {
         throw new Exception\InvalidArgumentException("A name for the key pair must be provided");
     }
     $this->checkBasePath('write');
     $base = $this->options->getBasePath() . DIRECTORY_SEPARATOR;
     $keyList = $this->getKeyList();
     $oldKey = false;
     if (isset($keyList[$name])) {
         $oldKey = $keyList[$name]['file'];
     }
     $private = $rsa->getOptions()->getPrivateKey()->toString();
     $privateHash = md5($private);
     $file = $base . $privateHash;
     $bytes = file_put_contents($file, $private, LOCK_EX);
     if (false === $bytes) {
         throw new Exception\RuntimeException('Failed to write the private key to disk');
     }
     chmod($file, $this->options->getPrivateKeyFileMode());
     $public = $rsa->getOptions()->getPublicKey()->toString();
     $file .= '.pub';
     $bytes = file_put_contents($file, $public, LOCK_EX);
     if (false === $bytes) {
         throw new Exception\RuntimeException('Failed to write the public key to disk');
     }
     chmod($file, $this->options->getPublicKeyFileMode());
     $pass = $rsa->getOptions()->getPassPhrase();
     $requiresPass = !empty($pass);
     $this->keyList[$name] = array('file' => $privateHash, 'requiresPassword' => $requiresPass, 'binaryOutput' => $rsa->getOptions()->getBinaryOutput(), 'hashAlgorithm' => $rsa->getOptions()->getHashAlgorithm());
     $this->keys[$name] = $rsa;
     $this->saveKeyList();
     if ($this->options->getDeleteOldKeys() && false !== $oldKey) {
         if (file_exists($base . $oldKey)) {
             unlink($base . $oldKey);
         }
         if (file_exists($base . $oldKey . '.pub')) {
             unlink($base . $oldKey . '.pub');
         }
     }
     return $this;
 }