Public Key Generator

Used to generate RSA Keys which can be used for JWT Signing. Function parameters allow for generation of additional public and private key types.

Source Code


Example Code

Security - Generate a new RSA Key Pair

// Generate a new RSA Key Pair
$key_pair = \FastSitePHP\Security\Crypto\PublicKey::generateRsaKeyPair();
list($private_key, $public_key) = $key_pair;

// Generate a new 3072-Bit RSA Key
$bits = 3072;
$key_pair = \FastSitePHP\Security\Crypto\PublicKey::generateRsaKeyPair($bits);
list($private_key2, $public_key2) = $key_pair;


defaultConfig($bits = 2048)

Static Function

Return the default config options used when generating a new Key Pair. This an array of options set for a 2048-bit RSA Key.

2048-bit is used as because it is provides a combination of acceptable speed for JWT and strong security. If a JWT needs to be signed and validated after the year 2030 then a 3072-bit key is recommended, however 3072-bit keys are much slower to create.

On Windows this will attempt to find and set the [openssl.cnf] file for the instance of PHP that is running. This option is generally required in order to generate RSA Key Pairs on Windows. The file if found by default will exist at a location such as:
    C:\Program Files\PHP\v7.2\extras\ssl\openssl.cnf

Returns: array

generateRsaKeyPair($bits = 2048)

Static Function

Generate a new RSA Key Pair

Returns: array - [private_key, public_key]

generateKeyPair(array $config)

Static Function

Generate a new Public/Private Key Pair

Returns: array - [private_key, public_key]