Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Encode pre-1.9.3 RSA public keys using X.509 format

View gist:1470287
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'openssl'
require 'base64'
 
rsa = OpenSSL::PKey::RSA.new(2048)
modulus = rsa.n
exponent = rsa.e
 
oid = OpenSSL::ASN1::ObjectId.new("rsaEncryption")
alg_id = OpenSSL::ASN1::Sequence.new([oid, OpenSSL::ASN1::Null.new(nil)])
ary = [OpenSSL::ASN1::Integer.new(modulus), OpenSSL::ASN1::Integer.new(exponent)]
pub_key = OpenSSL::ASN1::Sequence.new(ary)
enc_pk = OpenSSL::ASN1::BitString.new(pub_key.to_der)
subject_pk_info = OpenSSL::ASN1::Sequence.new([alg_id, enc_pk])
 
base64 = Base64.encode64(subject_pk_info.to_der)
 
#This is the equivalent to the X.509 encoding used in >= 1.9.3
pem = "-----BEGIN PUBLIC KEY-----\n#{base64}-----END PUBLIC KEY-----"
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.