Skip to content

Instantly share code, notes, and snippets.

@emboss
Created June 9, 2012 21:44
Show Gist options
  • Save emboss/2902696 to your computer and use it in GitHub Desktop.
Save emboss/2902696 to your computer and use it in GitHub Desktop.

Revisions

  1. emboss created this gist Jun 9, 2012.
    13 changes: 13 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    require 'openssl'
    require 'base64'

    rsa = OpenSSL::PKey::RSA.new(2048)
    modulus = rsa.n
    exponent = rsa.e

    ary = [OpenSSL::ASN1::Integer.new(modulus), OpenSSL::ASN1::Integer.new(exponent)]
    pub_key = OpenSSL::ASN1::Sequence.new(ary)
    base64 = Base64.encode64(pub_key.to_der)

    #This is the equivalent to the PKCS#1 encoding used before 1.9.3
    pem = "-----BEGIN RSA PUBLIC KEY-----\n#{base64}-----END RSA PUBLIC KEY-----"