phpseclib\Crypt\RSA\PKCS1::load PHP Method

load() static public method

Break a public or private key down into its constituent components
static public load ( string $key, string $password = '' ) : array
$key string
$password string optional
return array
    static function load($key, $password = '')
    {
        if (!is_string($key)) {
            return false;
        }
        $components = ['isPublicKey' => strpos($key, 'PUBLIC') !== false];
        $key = parent::load($key, $password);
        if ($key === false) {
            return false;
        }
        $asn1 = new ASN1();
        $decoded = $asn1->decodeBER($key);
        if (empty($decoded)) {
            return false;
        }
        $key = $asn1->asn1map($decoded[0], RSAPrivateKey);
        if (is_array($key)) {
            $components += ['modulus' => $key['modulus'], 'publicExponent' => $key['publicExponent'], 'privateExponent' => $key['privateExponent'], 'primes' => [1 => $key['prime1'], $key['prime2']], 'exponents' => [1 => $key['exponent1'], $key['exponent2']], 'coefficients' => [2 => $key['coefficient']]];
            if ($key['version'] == 'multi') {
                foreach ($key['otherPrimeInfos'] as $primeInfo) {
                    $components['primes'][] = $primeInfo['prime'];
                    $components['exponents'][] = $primeInfo['exponent'];
                    $components['coefficients'][] = $primeInfo['coefficient'];
                }
            }
            return $components;
        }
        $key = $asn1->asn1map($decoded[0], RSAPublicKey);
        return is_array($key) ? $components + $key : false;
    }

Usage Example

Example #1
0
 /**
  * Break a public or private key down into its constituent components
  *
  * @access public
  * @param string $key
  * @param string $password optional
  * @return array
  */
 static function load($key, $password = '')
 {
     $components = ['isPublicKey' => strpos($key, 'PUBLIC') !== false];
     $key = parent::load($key, $password);
     if ($key === false) {
         return false;
     }
     $type = isset($key['privateKey']) ? 'private' : 'public';
     if ($key[$type . 'KeyAlgorithm']['algorithm'] != '1.2.840.113549.1.1.1') {
         return false;
     }
     $result = $components + PKCS1::load($key[$type . 'Key']);
     if (isset($key['meta'])) {
         $result['meta'] = $key['meta'];
     }
     return $result;
 }