You can use openssl library. As for the more complete solution you want, I don't think it is available. Do not work too much with the library.
http://ruby-doc.org/stdlib-1.9.2/libdoc/openssl/rdoc/OpenSSL/PKey/RSA.html
Basically do something like:
# private_key_str can be in PEM or DER format OpenSSL::PKey::RSA.new(private_key_str, 'passphrase').public_key
And compare it with the public key. You can get a string representation of the C # to_pem or #to_der key, depending on the format you use (play a bit with it). You can also use the rsa library.
I think you can use
ssh-keygen -e -f id_rsa.pub > pemkey.pub
Convert from default ssh-keygen format to PEM. You can run this command on the server to do the conversion, if necessary - you will have to try and see to match the formats correctly, since I'm not sure that OpenSSL :: PKey :: RSA accepts the default ssh format -keygen. You can also make ssh-keygen read from STDIN and write to STDOUT, so you do not need to use files for conversion.
source share