Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

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 .read also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1