Sha256: 0cf57c7d977cb25395aa6efdc9fb2862d948b207ea032a406c84e940cb4099a6

Contents?: true

Size: 893 Bytes

Versions: 3

Compression:

Stored size: 893 Bytes

Contents

require 'openssl' 
require 'base64'

module Crypto
  
  def self.create_keys(priv = "rsa_key", pub = "#{priv}.pub", bits = 4096)
    private_key = OpenSSL::PKey::RSA.new(bits)
    File.open(priv, "w+") { |fp| fp << private_key.to_s }
    File.open(pub,  "w+") { |fp| fp << private_key.public_key.to_s }    
    private_key
  end
  
  class Key
    def initialize(data)
      @public = (data =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
      @key = OpenSSL::PKey::RSA.new(data)
    end
  
    def self.from_file(filename)    
      self.new File.read( filename )
    end
  
    def encrypt(text)
      @key.send("#{key_type}_encrypt", text)
    end
    
    def decrypt(text)
      @key.send("#{key_type}_decrypt", text)
    end
  
    def private?
      !@public
    end
  
    def public?
      @public
    end
    
    def key_type
      @public ? :public : :private
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
encbs-0.1.0 lib/crypto.rb
palobr-0.1.0.1 lib/crypto.rb
palobr-0.1.0 lib/crypto.rb