1<?php 2 3/** 4 * DH Private Key 5 * 6 * @category Crypt 7 * @package DH 8 * @author Jim Wigginton <terrafrost@php.net> 9 * @copyright 2015 Jim Wigginton 10 * @license http://www.opensource.org/licenses/mit-license.html MIT License 11 * @link http://phpseclib.sourceforge.net 12 */ 13 14namespace phpseclib3\Crypt\DH; 15 16use phpseclib3\Crypt\Common; 17use phpseclib3\Crypt\DH; 18 19/** 20 * DH Private Key 21 * 22 * @package DH 23 * @author Jim Wigginton <terrafrost@php.net> 24 * @access public 25 */ 26class PrivateKey extends DH 27{ 28 use Common\Traits\PasswordProtected; 29 30 /** 31 * Private Key 32 * 33 * @var \phpseclib3\Math\BigInteger 34 * @access private 35 */ 36 protected $privateKey; 37 38 /** 39 * Public Key 40 * 41 * @var \phpseclib3\Math\BigInteger 42 * @access private 43 */ 44 protected $publicKey; 45 46 /** 47 * Returns the public key 48 * 49 * @access public 50 * @return DH\PublicKey 51 */ 52 public function getPublicKey() 53 { 54 $type = self::validatePlugin('Keys', 'PKCS8', 'savePublicKey'); 55 56 if (!isset($this->publicKey)) { 57 $this->publicKey = $this->base->powMod($this->privateKey, $this->prime); 58 } 59 60 $key = $type::savePublicKey($this->prime, $this->base, $this->publicKey); 61 62 return DH::loadFormat('PKCS8', $key); 63 } 64 65 /** 66 * Returns the private key 67 * 68 * @param string $type 69 * @param array $options optional 70 * @return string 71 */ 72 public function toString($type, array $options = []) 73 { 74 $type = self::validatePlugin('Keys', $type, 'savePrivateKey'); 75 76 if (!isset($this->publicKey)) { 77 $this->publicKey = $this->base->powMod($this->privateKey, $this->prime); 78 } 79 80 return $type::savePrivateKey($this->prime, $this->base, $this->privateKey, $this->publicKey, $this->password, $options); 81 } 82} 83