I have an RSA public key modulus and exponent string.
I want to create a OpenSSL::PKey::RSA
from these two strings.
Basically they come in as:
How would I do this in Ruby? The end goal is to get this to the JWT gem.
I'm currently in Ruby 2.3.1, so this works:
key = OpenSSL::PKey::RSA.new
key.e = OpenSSL::BN.new(Base64.decode64(e), 2)
key.n = OpenSSL::BN.new(Base64.decode64(n), 2)
However, it won't work during an upgrade.
I got it working this way, based on this python implementation:
https://github.com/jpf/okta-jwks-to-pem/blob/master/jwks_to_pem.py
key = OpenSSL::PKey::RSA.new
exponent = kid_header['e']
modulus = kid_header['n']
# Voila !
key.set_key(base64_to_long(modulus), base64_to_long(exponent), nil)
def base64_to_long(data)
decoded_with_padding = Base64.urlsafe_decode64(data) + Base64.decode64('==')
decoded_with_padding.to_s.unpack('C*').map do |byte|
to_hex(byte)
end.join.to_i(16)
end
def to_hex(int)
int < 16 ? '0' + int.to_s(16) : int.to_s(16)
end