Skip to content
class method

OpenSSL::PKey.generate_key

OpenSSL::PKey.generate_key(algo_name [, options]) → pkey
OpenSSL::PKey.generate_key(pkey [, options]) → pkey

Generates a new key (pair).

If a String is given as the first argument, it generates a new random key for the algorithm specified by the name just as ::generate_parameters does. If an OpenSSL::PKey::PKey is given instead, it generates a new random key for the same algorithm as the key, using the parameters the key contains.

See ::generate_parameters for the details of options and the given block.

Example

Example
pkey_params = OpenSSL::PKey.generate_parameters("DSA", "dsa_paramgen_bits" => 2048)
pkey_params.priv_key #=> nil
pkey = OpenSSL::PKey.generate_key(pkey_params)
pkey.priv_key #=> #<OpenSSL::BN 6277...
See also .generate_parameters .new_raw_private_key also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1