Creating Large Primes for Diffie-Hellman in Ruby

I am writing a diffie-hellman key exchange implementation in ruby ​​for a project for one of my university classes. I need to generate large (safe) primes with a length of at least 500 bits. Any ideas? Should I use the OpenSSL library? If so, what features would you recommend?

+4
source share
2 answers

Use openssl gem

OpenSSL :: BN :: rand

You can specify the desired size - for example, OpenSSL :: BN :: rand (212)

+2
source

OpenSSL::BN::generate_prime(500) will do this as abdollar said. Be sure to place require 'openssl' at the top to include it in your ruby ​​file

To check the correct number of bits, you can print the binary just by running OpenSSL::BN::generate_prime(500).to_i.to_s(2).length and it will output 500 and the most significant bit will be 1

Open SSL documentation

0
source

All Articles