php8.5
Home/ Manual/ openssl / functions/ openssl_csr_get_public_key

openssl_csr_get_public_key

PHP function Edit on GitHub ✎

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

Returns the public key of a CSR

Description

openssl_csr_get_public_key(OpenSSLCertificateSigningRequest|string $csr, bool $short_names = true): OpenSSLAsymmetricKey|false

openssl_csr_get_public_key() extracts the public key from csr and prepares it for use by other functions.

Parameters

short_names
Warning

This parameter is ignored

Return Values

Returns an OpenSSLAsymmetricKey on success, or false on error.

Changelog

VersionDescription
8.0.0On success, this function returns an OpenSSLAsymmetricKey instance now; previously, a Resource of type OpenSSL key was returned.
8.0.0csr accepts an OpenSSLCertificateSigningRequest instance now; previously, a Resource of type OpenSSL X.509 CSR was accepted.

Examples

openssl_csr_get_public_key() example

php
<?php
$subject = array(
    "commonName" => "example.com",
);
$private_key = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));
$csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha256') );
$public_key = openssl_csr_get_public_key($csr);
$info = openssl_pkey_get_details($public_key);
echo $info['key'];
?>

See Also

Source: reference/openssl/functions/openssl-csr-get-public-key.xml · from the official PHP manual (php/doc-en)