1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-13 01:27:41 +01:00
tgseclib/phpseclib/Crypt/RSA/Keys/Raw.php

109 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* Raw RSA Key Handler
*
* PHP version 5
*
* An array containing two \phpseclib\Math\BigInteger objects.
*
* The exponent can be indexed with any of the following:
*
* 0, e, exponent, publicExponent
*
* The modulus can be indexed with any of the following:
*
* 1, n, modulo, modulus
*
* @category Crypt
* @package RSA
* @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
*/
2016-12-23 17:02:07 +01:00
namespace phpseclib\Crypt\RSA\Keys;
use phpseclib\Math\BigInteger;
/**
* Raw RSA Key Handler
*
* @package RSA
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2016-12-09 23:46:34 +01:00
abstract class Raw
{
/**
* Break a public or private key down into its constituent components
*
* @access public
* @param string $key
* @param string $password optional
2018-10-25 03:00:37 +02:00
* @return array
*/
2016-12-09 23:46:34 +01:00
public static function load($key, $password = '')
{
if (!is_array($key)) {
2018-10-25 03:00:37 +02:00
throw new \UnexpectedValueException('Key should be a array - not a ' . gettype($key));
}
if (isset($key['isPublicKey']) && isset($key['modulus'])) {
if (isset($key['privateExponent']) || isset($key['publicExponent'])) {
if (!isset($key['primes'])) {
return $key;
}
if (isset($key['exponents']) && isset($key['coefficients']) && isset($key['publicExponent']) && isset($key['privateExponent'])) {
return $key;
}
}
}
2016-11-30 22:19:23 +01:00
$components = ['isPublicKey' => true];
switch (true) {
case isset($key['e']):
$components['publicExponent'] = $key['e'];
break;
case isset($key['exponent']):
$components['publicExponent'] = $key['exponent'];
break;
case isset($key['publicExponent']):
$components['publicExponent'] = $key['publicExponent'];
break;
case isset($key[0]):
$components['publicExponent'] = $key[0];
}
switch (true) {
case isset($key['n']):
$components['modulus'] = $key['n'];
break;
case isset($key['modulo']):
$components['modulus'] = $key['modulo'];
break;
case isset($key['modulus']):
$components['modulus'] = $key['modulus'];
break;
case isset($key[1]):
$components['modulus'] = $key[1];
}
2018-10-25 03:00:37 +02:00
if (isset($components['modulus']) && isset($components['publicExponent'])) {
return $components;
}
throw new \UnexpectedValueException('Modulus / exponent not present');
}
/**
* Convert a public key to the appropriate format
*
* @access public
* @param \phpseclib\Math\BigInteger $n
* @param \phpseclib\Math\BigInteger $e
2017-08-03 09:14:32 +02:00
* @return array
*/
2016-12-09 23:46:34 +01:00
public static function savePublicKey(BigInteger $n, BigInteger $e)
{
2016-11-30 22:19:23 +01:00
return ['e' => clone $e, 'n' => clone $n];
}
}