1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-04 02:28:06 +01:00

PublicKeyLoader: add loadPublic, loadPrivate and loadParameters

This commit is contained in:
terrafrost 2021-02-08 23:44:24 -06:00
parent a18b86ae26
commit 3bddf4d962
2 changed files with 152 additions and 0 deletions

View File

@ -180,6 +180,55 @@ abstract class AsymmetricKey
$new;
}
/**
* Loads a private key
*
* @return PrivateKey
* @access public
* @param string|array $key
* @param string $password optional
*/
public function loadPrivate($key, $password = '')
{
$key = self::load($key, $password);
if (!$key instanceof PrivateKey) {
throw new NoKeyLoadedException('The key that was loaded was not a private key');
}
return $key;
}
/**
* Loads a public key
*
* @return PublicKey
* @access public
* @param string|array $key
*/
public function loadPublic($key)
{
$key = self::load($key);
if (!$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a public key');
}
return $key;
}
/**
* Loads parameters
*
* @return AsymmetricKey
* @access public
* @param string|array $key
*/
public function loadParameters($key)
{
$key = self::load($key);
if (!$key instanceof PrivateKey && !$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a parameter');
}
return $key;
}
/**
* Load the key, assuming a specific format
*
@ -212,6 +261,58 @@ abstract class AsymmetricKey
$new;
}
/**
* Loads a private key
*
* @return PrivateKey
* @access public
* @param string $type
* @param string $key
* @param string $password optional
*/
public function loadPrivateFormat($type, $key, $password = false)
{
$key = self::loadFormat($type, $key, $password);
if (!$key instanceof PrivateKey) {
throw new NoKeyLoadedException('The key that was loaded was not a private key');
}
return $key;
}
/**
* Loads a public key
*
* @return PublicKey
* @access public
* @param string $type
* @param string $key
*/
public function loadPublicFormat($type, $key)
{
$key = self::loadFormat($type, $key);
if (!$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a public key');
}
return $key;
}
/**
* Loads parameters
*
* @return AsymmetricKey
* @access public
* @param string $type
* @param string|array $key
*/
public function loadParametersFormat($type, $key)
{
$key = self::loadFormat($type, $key);
if (!$key instanceof PrivateKey && !$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a parameter');
}
return $key;
}
/**
* Validate Plugin
*

View File

@ -16,6 +16,8 @@
namespace phpseclib3\Crypt;
use phpseclib3\Crypt\Common\AsymmetricKey;
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Exception\NoKeyLoadedException;
use phpseclib3\File\X509;
@ -61,4 +63,53 @@ abstract class PublicKeyLoader
throw new NoKeyLoadedException('Unable to read key');
}
/**
* Loads a private key
*
* @return PrivateKey
* @access public
* @param string|array $key
* @param string $password optional
*/
public function loadPrivate($key, $password = false)
{
$key = self::load($key, $password);
if (!$key instanceof PrivateKey) {
throw new NoKeyLoadedException('The key that was loaded was not a private key');
}
return $key;
}
/**
* Loads a public key
*
* @return PublicKey
* @access public
* @param string|array $key
*/
public function loadPublic($key)
{
$key = self::load($key);
if (!$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a public key');
}
return $key;
}
/**
* Loads parameters
*
* @return AsymmetricKey
* @access public
* @param string|array $key
*/
public function loadParameters($key)
{
$key = self::load($key);
if (!$key instanceof PrivateKey && !$key instanceof PublicKey) {
throw new NoKeyLoadedException('The key that was loaded was not a parameter');
}
return $key;
}
}