Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PHP | openssl_pkey_get_public() Function



Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP openssl_pkey_get_public() Function.

The openssl_pkey_get_public() function returns the public key.

This built-in function returns the public key from the given certificate so that it can be used with other functions.

syntax

Following below is the syntax to use this function -

openssl_pkey_get_public ( mixed $certificate ) : resource

READ: PHP | openssl_pkey_get_private Function

Parameter Details

Sr.NoParameterDescription
1

certificate

You can make use of following certificates :

1. An X.509 certificate resource

2. Public key from the file in the format file://path/to/file.pem.

3. A PEM formatted public key.


Return Value

This built-in PHP function returns a positive resource identifier if there is no error, else it returns false if it fails.

PHP Version

This PHP function works from PHP version greater than 5.0.0.

Example1

The following example illustrates the usage of PHP openssl_pkey_get_public() function with X.509 certificate -

php
$dn
= array(
"countryName" => "NRA",
"stateOrProvinceName" => "Rivers",
"localityName" => "test1",
"organizationName" => "test2",
"organizationalUnitName" => "test3",
"commonName" => "www.test.com",
"emailAddress" => "[email protected]"
);

// Generate a new private /public key pair
$privkey
= openssl_pkey_new();

// Generate a certificate
$csr
= openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256'));
$res_cert
= openssl_csr_sign($csr, null, $privkey, 365);
openssl_x509_export
($res_cert, $x_509_certificate);
echo $res_pubkey
= openssl_pkey_get_public($x_509_certificate);
?>

Output

When the above code is executed, it will produce the following result -

Resource id #5

Example2

The following example illustrates the usage of PHP openssl_pkey_get_public() function using .pem file -

php
$dn
= array(
"countryName" => "NRA",
"stateOrProvinceName" => "Rivers",
"localityName" => "test1",
"organizationName" => "test2",
"organizationalUnitName" => "test3",
"commonName" => "www.test.com",
"emailAddress" => "[email protected]"
);

// Generate a new private /public key pair
$privkey
= openssl_pkey_new();

// Generate a certificate
$csr
= openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256'));
$res_cert
= openssl_csr_sign($csr, null, $privkey, 365);
openssl_x509_export_to_file
($res_cert, 'C:/xampp/htdocs/modules/openssl/x_509.pem');
echo $res_pubkey
= openssl_pkey_get_public(file_get_contents('C:/xampp/htdocs/modules/openssl/x_509.pem'));
?>

Output

When the above code is executed, it will produce the following result -

Resource id #7

READ: PHP | openssl_pkey_new() Function

Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be studying about the openssl_pkey_export_to_file() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


This post first appeared on Web Design Tutorialz, please read the originial post: here

Share the post

PHP | openssl_pkey_get_public() Function

×

Subscribe to Web Design Tutorialz

Get updates delivered right to your inbox!

Thank you for your subscription

×