class Crypto::Key

Public Class Methods

from_file(filename, size = 4096) click to toggle source
# File lib/crypto.rb, line 19
def self.from_file(filename, size = 4096)
  self.new(File.read(filename), size)
end
new(data, size) click to toggle source
# File lib/crypto.rb, line 13
def initialize(data, size)
  @public = (data =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
  @key = OpenSSL::PKey::RSA.new(data)
  @size = (size == 4096 ? 512 : 256)
end

Public Instance Methods

decrypt(text) click to toggle source
# File lib/crypto.rb, line 54
def decrypt(text)
  @key.send("#{key_type}_decrypt", text)
 rescue Exception => e
   puts_fail "RSA decrypt error: #{e.message}"
end
decrypt_from_stream(data) click to toggle source
# File lib/crypto.rb, line 36
def decrypt_from_stream(data)
  encrypt_data = StringIO.new data
  encrypt_data.seek 0
  decrypt_data = ""

  while buf = encrypt_data.read(@size) do
    decrypt_data += decrypt(buf)
  end

  decrypt_data
end
encrypt(text) click to toggle source
# File lib/crypto.rb, line 48
def encrypt(text)
  @key.send("#{key_type}_encrypt", text)
rescue Exception => e
  puts_fail "RSA encrypt error: #{e.message}"
end
encrypt_to_stream(data) click to toggle source
# File lib/crypto.rb, line 23
def encrypt_to_stream(data)
  encrypt_data = StringIO.new
  data = data.read if data.is_a? StringIO
  i = 0

  while buf = data[i..(i+=117)] do
    encrypt_data << encrypt(buf)
  end

  encrypt_data.seek 0
  encrypt_data
end
key_type() click to toggle source
# File lib/crypto.rb, line 68
def key_type
  @public ? :public : :private
end
private?() click to toggle source
# File lib/crypto.rb, line 60
def private?
  !@public
end
public?() click to toggle source
# File lib/crypto.rb, line 64
def public?
  @public
end