From: Brian Candler Date: 2004-09-09T22:46:53+09:00 Subject: Suggestion: OpenSSL::PKey::RSA.new_from_parameters I would like to propose that a new constructor method be added to the Ruby OpenSSL library to allow an RSA key to be build from its parameters. A Ruby implementation is given below, or it could be rewritten in C. This method takes parameters (n, e, d, p, q) and calculates d from p and q if necessary, in the same way as Perl's Crypt::OpenSSL::RSA->new_key_from_parameters() does. It's useful when you've factored 'n' from a public key and want to generate a PEM file containing the private key :-) Regards, Brian Candler. module OpenSSL module PKey class RSA # Construct an RSA key from parameters. # For private keys, if d is nil then it is calculated from p and q def self.new_from_parameters(n, e, d=nil, p=nil, q=nil) a = self.new # self.new(64) for ruby < 1.8.2 a.n = n # converted to OpenSSL::BN automatically a.e = e if p and q a.p = p a.q = q raise "n != p * q" unless a.n == a.p * a.q a.d = d || a.e.mod_inverse((a.p-1)*(a.q-1)) a.dmp1 = a.d % (a.p-1) a.dmq1 = a.d % (a.q-1) a.iqmp = a.q.mod_inverse(a.p) else a.d = d a.p = nil a.q = nil end a end end end end