Merge branch '3.0'

This commit is contained in:
terrafrost 2021-02-09 04:05:16 -06:00
commit 99fb313234
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;
}
}