openssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters.
The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.
What It Does
The operation is intended for: openssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters.
The OpenSSL extension must be enabled.
Syntax and Return Value
openssl_pkey_new(?array $options = ...): OpenSSLAsymmetricKey|false
| Parameter | Purpose |
|---|---|
| $options (?array) | Driver or algorithm-specific options; avoid unchecked user-controlled settings. |
The declared return type is shown above. Errors can include false results or exceptions depending on this API: Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.
Example 1: Basic Operation
Use modern recommended algorithms for deployment and protect the private key.
<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
var_dump($key instanceof OpenSSLAsymmetricKey);
Example 2: A Different Use Case
Inspect a different key algorithm or output property without exposing private-key contents.
<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_EC, 'curve_name' => 'prime256v1']);
var_dump($key instanceof OpenSSLAsymmetricKey);
Practical Example: Checking the Result
Inspect a different key algorithm or output property without exposing private-key contents.
<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
echo openssl_pkey_get_details($key)['bits'];
Important Behavior and Edge Cases
Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.
Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.
Comparison with Related Functions
| Approach | Key distinction |
|---|---|
| openssl_pkey_new | openssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters. |
| Related technique | openssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | openssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash. |
| Ignoring a security prerequisite or failure case | Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored. |
Key Takeaways
- openssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters.
- Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.
- openssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash.
Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.