D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
local
/
psa
/
admin
/
plib
/
modules
/
letsencrypt
/
vendor
/
acmephp
/
ssl
/
Generator
/
Filename :
KeyPairGenerator.php
back
Copy
<?php /* * This file is part of the Acme PHP project. * * (c) Titouan Galopin <galopintitouan@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace PleskLetsEncrypt\AcmePhp\Ssl\Generator; use PleskLetsEncrypt\AcmePhp\Ssl\Exception\KeyGenerationException; use PleskLetsEncrypt\AcmePhp\Ssl\Exception\KeyPairGenerationException; use PleskLetsEncrypt\AcmePhp\Ssl\Generator\DhKey\DhKeyGenerator; use PleskLetsEncrypt\AcmePhp\Ssl\Generator\DsaKey\DsaKeyGenerator; use PleskLetsEncrypt\AcmePhp\Ssl\Generator\EcKey\EcKeyGenerator; use PleskLetsEncrypt\AcmePhp\Ssl\Generator\RsaKey\RsaKeyGenerator; use PleskLetsEncrypt\AcmePhp\Ssl\Generator\RsaKey\RsaKeyOption; use PleskLetsEncrypt\AcmePhp\Ssl\KeyPair; /** * Generate random KeyPair using OpenSSL. * * @author Jérémy Derussé <jeremy@derusse.com> */ class KeyPairGenerator { private $generator; public function __construct(PrivateKeyGeneratorInterface $generator = null) { $this->generator = $generator ?: new ChainPrivateKeyGenerator([new RsaKeyGenerator(), new EcKeyGenerator(), new DhKeyGenerator(), new DsaKeyGenerator()]); } /** * @param KeyOption|null $keyOption configuration of the key to generate * * @throws KeyPairGenerationException when OpenSSL failed to generate keys */ public function generateKeyPair(KeyOption $keyOption = null) : KeyPair { if (null === $keyOption) { $keyOption = new RsaKeyOption(); } try { $privateKey = $this->generator->generatePrivateKey($keyOption); } catch (KeyGenerationException $e) { throw new KeyPairGenerationException('Fail to generate a KeyPair with the given options', 0, $e); } return new KeyPair($privateKey->getPublicKey(), $privateKey); } }