2019-07-27 22:28:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DH Private Key
|
|
|
|
*
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
namespace phpseclib3\Crypt\DH;
|
2019-07-27 22:28:18 +00:00
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Crypt\Common;
|
2022-01-30 15:34:42 +00:00
|
|
|
use phpseclib3\Crypt\DH;
|
2022-08-18 13:05:57 +00:00
|
|
|
use phpseclib3\Math\BigInteger;
|
2019-07-27 22:28:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DH Private Key
|
|
|
|
*
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
*/
|
2023-02-05 23:44:51 +00:00
|
|
|
final class PrivateKey extends DH
|
2019-07-27 22:28:18 +00:00
|
|
|
{
|
|
|
|
use Common\Traits\PasswordProtected;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private Key
|
|
|
|
*
|
2022-08-18 13:05:57 +00:00
|
|
|
* @var BigInteger
|
2019-07-27 22:28:18 +00:00
|
|
|
*/
|
|
|
|
protected $privateKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public Key
|
|
|
|
*
|
2022-08-18 13:05:57 +00:00
|
|
|
* @var BigInteger
|
2019-07-27 22:28:18 +00:00
|
|
|
*/
|
|
|
|
protected $publicKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the public key
|
|
|
|
*
|
2021-04-13 21:18:26 +00:00
|
|
|
* @return DH\PublicKey
|
2019-07-27 22:28:18 +00:00
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function getPublicKey(): PublicKey
|
2019-07-27 22:28:18 +00:00
|
|
|
{
|
|
|
|
$type = self::validatePlugin('Keys', 'PKCS8', 'savePublicKey');
|
|
|
|
|
|
|
|
if (!isset($this->publicKey)) {
|
|
|
|
$this->publicKey = $this->base->powMod($this->privateKey, $this->prime);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $type::savePublicKey($this->prime, $this->base, $this->publicKey);
|
|
|
|
|
|
|
|
return DH::loadFormat('PKCS8', $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the private key
|
|
|
|
*
|
|
|
|
* @param array $options optional
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function toString(string $type, array $options = []): string
|
2019-07-27 22:28:18 +00:00
|
|
|
{
|
|
|
|
$type = self::validatePlugin('Keys', $type, 'savePrivateKey');
|
|
|
|
|
|
|
|
if (!isset($this->publicKey)) {
|
|
|
|
$this->publicKey = $this->base->powMod($this->privateKey, $this->prime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type::savePrivateKey($this->prime, $this->base, $this->privateKey, $this->publicKey, $this->password, $options);
|
|
|
|
}
|
|
|
|
}
|