public function generateKeyPair($keySize = 4096)
{
Assert::integer($keySize, __METHOD__ . '::$keySize should be an integer. Got: %s');
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => $keySize]);
if (!$key) {
throw new KeyPairGenerationException(sprintf('OpenSSL key creation failed during generation with error: %s', openssl_error_string()));
}
if (!openssl_pkey_export($key, $privateKey)) {
throw new KeyPairGenerationException(sprintf('OpenSSL key export failed during generation with error: %s', openssl_error_string()));
}
$details = openssl_pkey_get_details($key);
if (!is_array($details)) {
throw new KeyPairGenerationException(sprintf('OpenSSL key parsing failed during generation with error: %s', openssl_error_string()));
}
return new KeyPair(new PublicKey($details['key']), new PrivateKey($privateKey));
}