On this page
End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class · Ruby 3.1
OpenSSL::PKey::DH
An implementation of the Diffie-Hellman key exchange protocol based on discrete logarithms in finite fields, the same basis that DSA is built on.
Accessor methods for the Diffie-Hellman parameters
DH#p-
The prime (an
OpenSSL::BN) of the Diffie-Hellman parameters. - DH#g
-
The generator (an
OpenSSL::BN) g of the Diffie-Hellman parameters. - DH#pub_key
-
The per-session public key (an
OpenSSL::BN) matching the private key. This needs to be passed toDH#compute_key. - DH#priv_key
-
The per-session private key, an
OpenSSL::BN.
Example of a key exchange
Example
# you may send the parameters (der) and own public key (pub1) publicly # to the participating party dh1 = OpenSSL::PKey::DH.new(2048) der = dh1.to_der pub1 = dh1.pub_key # the other party generates its per-session key pair dhparams = OpenSSL::PKey::DH.new(der) dh2 = OpenSSL::PKey.generate_key(dhparams) pub2 = dh2.pub_key symm_key1 = dh1.compute_key(pub2) symm_key2 = dh2.compute_key(pub1) puts symm_key1 == symm_key2 # => true