
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP - Function openssl_pkey_new()
Definition and Usage
The openssl_pkey_new() function will return resource identifier that has new private and public key pair.
Description
The function openssl_pkey_new() returns a resource identifier. You can make use of openssl_pkey_get_details() function to get all the details of the key generated. The key (public/private pair) is used later with other openssl function like openssl_sign(), openssl_csr_new() - to get CSR certificate, that helps to create cryptographic digital signature.
Syntax
openssl_pkey_new ([ array $configargs ] ) : resource
Parameters
Sr.No | Parameter | Description |
---|---|---|
1 |
configargs |
The parameter configargs is the configuration details that need to be given to the function to generate the private/public key pair. The details are described below. |
configargs
By default openssl_pkey_new() makes use of configuration details present in openssl.cnf. But using configargs you can overwrite them.The config details are as follows:
key in configargs | type | key used in openssl.conf | description |
---|---|---|---|
digest_alg | string | default_md | Digest methods that you get from openssl_get_md_methods(). |
x509_extensions | string | x509_extensions | Extensions used when creating an x509 certificate. |
req_extensions | string | req_extensions | Extensions used when creating a CSR. |
private_key_bits | integer | default_bits | Specifies how many bits to be used while generating a private key. |
private_key_type | integer | none | The type of private key to create. It can be one of OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA or OPENSSL_KEYTYPE_EC. The default value is OPENSSL_KEYTYPE_RSA. |
encrypt_key | boolean | encrypt_key | Whether the exported key be encrypted? |
encrypt_key_cipher | integer | none | Cipher constants like OPENSSL_CIPHER_RC2_40 (integer),OPENSSL_CIPHER_RC2_128 (integer),OPENSSL_CIPHER_RC2_64 (integer),OPENSSL_CIPHER_DES (integer), OPENSSL_CIPHER_3DES (integer) etc. |
curve_name | string | none | One of the curve name returned by this function openssl_get_curve_names(). |
config | string | N/A | You can change the configuration in openssl.conf as per your requirement and give the path of it here. |
Return Values
PHP openssl_pkey_new() function returns a resource identifier if there is no error. It will return false if the key generation fails.
PHP Version
This function will work from PHP Version greater than 5.0.0.
Example 1
Working of openssl_pkey_new():
<?php // Generate a new private (and public) key pair $privkey = openssl_pkey_new(array( "digest_alg"=>'md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); var_dump($privkey); ?>
This will produce the following result:
resource(2) of type (OpenSSL key)
Example 2
Working of openssl_pkey_new() and openssl_pkey_get_details:
<?php // Generate a new private (and public) key pair $privkey = openssl_pkey_new(array( "digest_alg"=>'md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $key_details = openssl_pkey_get_details($privkey); print_r($key_details); ?>
This will produce the following result:
Array ( [bits] => 2048 [key] => -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1IGWxnWzICSkjrOVrYkw 9EKpIhcAYbhaymiwQY/ii9d7hnuGhpjjitDxzFftGafL6XOFKOhgbO2yrcpFxRKu AY3t5wyUcqUJV6CNwV2Hipk90psUcTmK3+pcCzcqCKr7sLWlHI48lse92xane8Sf CATNNbr9vmqUaTZ9FQqWihm3o/rNGuZTwMSKvcKsVguFpwrEDJaSLP1nt7RSHGc+ PixQSXp3PtQCH+S0CM9jt1jD9NkYXuuAlNbrsPm1fl2zAGR5Vh15evz5765lZ2mH LIZScfsO/qgai3R6foaBlJM5tiSeiVZgnnQDKFBi5XK2GhzDnKggJe4tdY7awTFm CQIDAQAB -----END PUBLIC KEY----- [rsa] => Array ( [n] => u $0B"aZhA{{Ws(`lEr W]=q9\7*>,PIzw>cX^~]dyVyzegi,Rqtz~9$V`t(Pbr %-u1f [e] => [d] => F"34!KyFRlY9]A@f~>u)1bKrpHEkLBEy'3-/PdjeFV1?.O:bl2zU{{*A z\96=V]"k2w>r6/-Z_8!YIL_3Ym.p^>I:oZ"=81: [dmp1] => z"TW*m$HQ$0saj # P*PQG\42a26(5K7zjxj7@e$.6bDq]] I}Lmd54)@:# [dmq1] => +4}\dcRKBw\E6%_m$39FhDuz$8HPK^Kk]eg>X" !=z=7V6q~3VtP$Okgv3=W\m J5^Kat~U.!lv#z* r ) [type] => 0 )
Example 3
To read public key from openssl_pkey_new():
<?php echo "The hash of Welcome to Tutorialspoint is - ". hash('crc32b', 'Welcome to Tutorialspoint'); ?>
This will produce following result:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArAWYwKIaf3uN1bwww4R8 51ifDPGsreqr9cV3J1gSKC8kaAEsAQaZ/6gQlDBsUGs4nE8zKgOlcdOV/JApgAdz +CwoLJUgmEUkLDxOcWaCIUVuHAiyBGJaRKZ+MASy7wRG8hb+INd0/zoQRGXk3+jf Fj6rvSinVg49C4RAkRtkEdNnH0G5s6cedV6ec30DouRTuEQ/Fgizf0qaVtQbAURP n+/LT9V8c4LMaCyID7caTQOXAEjQqD4ooXGkOzmcsp03j2/F+T2mSIQRtI1gGJkZ oCMGX/xRxh5uemCcC4jcshn45Ikmb/S7WFqTCOC0e8l8CiTZ5Rr8EKFgtwliMds8 pQIDAQAB -----END PUBLIC KEY-----
Example 4
To store public key from openssl_pkey_new() in a file:
<?php // Generate a new private (and public) key pair $privkey = openssl_pkey_new(array( "digest_alg"=>'md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $keydetails = openssl_pkey_get_details($privkey); // To save the public key to a file file_put_contents('mypublic.key', $keydetails['key']); ?>
The contents of file mypublic.key is:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3QxA7vWIz+F5t12/fl0H vyavVy/ZNZFWGK6BID/koYeVA2wKdXx9De3gn0hs4sSrN3aV58ctuxDVx36rKvYd AjKHfnfh7NmXnCEeUE4SgUUe0UUleoEMtsPP2Q8BC1HUjcC6SyJQKZG0bQqQlnAb HL7ou2TNsjA/SiJbPD+0OpsLAcW1c/DeoM+TAkZo0JIlgxjcJ5ZlEbJ0Mxv6m9XK k3bbMYHtKmZl+fzfPNcxCuK8Djnm5mYVR9KX1L86m1jz2kUQT/+wW84QRnZ7G+z8 4rQ77sZvWiIwwO2JmUvIsYeUxEP6/keZbDRuyO/2tWk/VxqQry4+Ktix/M2/iKWo QQIDAQAB -----END PUBLIC KEY-----