> For the complete documentation index, see [llms.txt](https://svgtas-organization.gitbook.io/php-oidc-client/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://svgtas-organization.gitbook.io/php-oidc-client/utils/generate-a-key-pair.md).

# Generate a key pair

You can use openssl if you prefer. The library offers you the possibility to generate one.

**With the library :**

```php
use Svgta\OidcLib\OidcUtils;
// RSA
  $RsaKey = OidcUtils::genRSAKey();
  // default 2048 length. To change length : Svgta\OidcUtils::genRSAKey(4096);

// EC
  $ECKey = OidcUtils::genEcKey();
  // default curve P-256. To change curve : Svgta\OidcUtils::genEcKey('P-521');
```

The result is an array given the private and public key in two formats :

* JWK
* PEM

**Example of a response** :

```php
Array
(
    [JWK] => Array
        (
            [privateKey] => {"kty":"EC","crv":"P-256","d":"2t8bH5rd-VvD_ZuOKIOQXz5UymqMTvnX4gsOTPEzwW8","x":"D2XFGdEmpygLSqqn5SMXeR740smRBfULJet3hzkUZ-Y","y":"Mkis4wh5Ei1ccN3YpgpNeHXtgDXFcaLDlOYTgj-kgwc"}
            [publicKey] => {"kty":"EC","crv":"P-256","x":"D2XFGdEmpygLSqqn5SMXeR740smRBfULJet3hzkUZ-Y","y":"Mkis4wh5Ei1ccN3YpgpNeHXtgDXFcaLDlOYTgj-kgwc"}
        )

    [PEM] => Array
        (
            [privateKey] => -----BEGIN EC PRIVATE KEY-----
MHcCAQEEINrfGx+a3flbw/2bjiiDkF8+VMpqjE751+ILDkzxM8FvoAoGCCqGSM49
AwEHoUQDQgAED2XFGdEmpygLSqqn5SMXeR740smRBfULJet3hzkUZ+YySKzjCHkS
LVxw3dimCk14de2ANcVxosOU5hOCP6SDBw==
-----END EC PRIVATE KEY-----

            [publicKey] => -----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAED2XFGdEmpygLSqqn5SMXeR740smR
BfULJet3hzkUZ+YySKzjCHkSLVxw3dimCk14de2ANcVxosOU5hOCP6SDBw==
-----END PUBLIC KEY-----

        )

)

```

**With openssl**

```shell
# RSA
openssl genrsa -out key.pem 2048
openssl rsa -in key.pem -outform PEM -pubout -out public.pem

# EC
openssl ecparam -name prime256v1 -genkey -noout -out key.pem
openssl ec -in key.pem -pubout -out public.pem
```

***

####


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://svgtas-organization.gitbook.io/php-oidc-client/utils/generate-a-key-pair.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
