phpseclib\Crypt\Common\PKCS1::load PHP Метод

load() статический публичный Метод

Break a public or private key down into its constituent components
static public load ( string $key, string $password ) : array
$key string
$password string optional
Результат array
    static function load($key, $password)
    {
        if (!is_string($key)) {
            return false;
        }
        /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is
                   "outside the scope" of PKCS#1.  PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to
                   protect private keys, however, that's not what OpenSSL* does.  OpenSSL protects private keys by adding
                   two new "fields" to the key - DEK-Info and Proc-Type.  These fields are discussed here:
        
                   http://tools.ietf.org/html/rfc1421#section-4.6.1.1
                   http://tools.ietf.org/html/rfc1421#section-4.6.1.3
        
                   DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell.
                   DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation
                   function.  As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's
                   own implementation.  ie. the implementation *is* the standard and any bugs that may exist in that
                   implementation are part of the standard, as well.
        
                   * OpenSSL is the de facto standard.  It's utilized by OpenSSH and other projects */
        if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
            $iv = Hex::decode(trim($matches[2]));
            // remove the Proc-Type / DEK-Info sections as they're no longer needed
            $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
            $ciphertext = ASN1::extractBER($key);
            if ($ciphertext === false) {
                $ciphertext = $key;
            }
            $crypto = self::getEncryptionObject($matches[1]);
            $crypto->setKey(self::generateSymmetricKey($password, $iv, $crypto->getKeyLength() >> 3));
            $crypto->setIV($iv);
            $key = $crypto->decrypt($ciphertext);
        } else {
            if (self::$format != self::MODE_DER) {
                $decoded = ASN1::extractBER($key);
                if ($decoded !== false) {
                    $key = $decoded;
                } elseif (self::$format == self::MODE_PEM) {
                    return false;
                }
            }
        }
        return $key;
    }

Usage 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;
 }