Sha256: aba547c05bd5439612ccb557bc6255a3bf1ef04965db70c4b9e22e28e0df907c

Contents?: true

Size: 917 Bytes

Versions: 1

Compression:

Stored size: 917 Bytes

Contents

require 'openssl'

module CertificateGenerator
  
  class Base
    
    def generate_core_cert (cname, serial)

      key = OpenSSL::PKey::RSA.new(2048)

      cert = OpenSSL::X509::Certificate.new
      subject = "/C=GB/ST=London/L=London/O=Acme Inc/OU=Tech/CN=#{cname}/emailAddress=ngsmrk@gmail.com"
      parsed_subject = OpenSSL::X509::Name.parse(subject)
      cert.subject = parsed_subject

      cert.not_before = Time.now
      cert.not_after = Time.now + (3600*24*365) # add a year
      cert.public_key = key.public_key
      cert.serial = serial
      cert.version = 2

      return cert, key

    end   
      
    def save_cert_and_key (cert, key, output_dir, prefix = '')
    
      FileUtils.mkdir_p("#{output_dir}")
      File.open("#{output_dir}/cert.pem", "w") { |f| f.write(cert.to_pem) }
      File.open("#{output_dir}/key.pem", "w") { |f| f.write(key.to_pem) }
    
    end     
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
certificate_generator-0.0.2 lib/certificate_generator/base.rb