collimarco collimarco - 1 year ago 187
Ruby Question

Export public key to base64 in Ruby

I have a public key in PEM format that was generated with:

ecdsa_public_key = ecdsa_private_key
ecdsa_public_key.private_key = nil

I have to read the PEM string and get a base64 url encoded string.
How can I do that in Ruby?

ecdsa_public_key = pem_string
ecdsa_public_key.to_base64 # pseudo code...

BTW I have to do this for the WebPush protocol, which states:

you must add your VAPID public key to the Crypto-Key header as a base64 url encoded string with p256ecdsa= prepended to it.

Answer Source

The PEM string actually is base 64 encoded (at least partially), but I don’t think it’s what you want here, it includes other details and I think you want the “raw” public key data.

Here’s one way you can get your key into the format I think you want. It’s a bit long winded but I don’t think Ruby’s OpenSSL bindings provide a more direct method (you’ll need to require "base64" first):

# Assuming the key is in ecdsa_public_key
Base64.urlsafe_encode64(ecdsa_public_key.public_key.to_bn.to_s(2), padding: false)

This calls public_key to get the underlying OpenSSL::PKey::EC::Point, then converts that to an OpenSSL::BN in the correct format, and converts that to a binary string. Finally this string is base64 encoded.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download