Author: Jim Wigginton (terrafrost@php.net)
Inheritance: extends PKCS
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 = '')
 {
     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;
 }