Class: Metafusion::Crypto::DigitalSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/metafusion/crypto/base.rb

Overview

Metafusion::Crypto::DigitalSignature provides easier a more streamlined API to create digital signatures without needing to know what that entails.

Usage:

include Metafusion::Crypto
sig = DigitalSignature.from_file('rsa_key.pub', 'rsa_key')
original_text = "my clear text message"
crypted_text = sig.encrypt(original_text)
plain_text = sig.decrypt(crypted_text)
puts "It worked - let's celebrate!" if original_text == plain_text

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_data, private_data = nil) ⇒ DigitalSignature

Constructor that takes the private and public key data. Only the public_data is required since not all DigitalSignature class clients will have access to the private key.



67
68
69
70
# File 'lib/metafusion/crypto/base.rb', line 67

def initialize(public_data, private_data = nil)
  @private_key = OpenSSL::PKey::RSA.new(private_data) if private_data
  @public_key = OpenSSL::PKey::RSA.new(public_data)
end

Class Method Details

.from_keys(public_filename = "rsa_key.pub", private_filename = nil) ⇒ Object

Loads and constructs key from file private_filename given.



56
57
58
59
60
# File 'lib/metafusion/crypto/base.rb', line 56

def from_keys(public_filename = "rsa_key.pub", 
                   private_filename = nil)
  return self.new(File.read(public_filename)) unless private_filename
  self.new(File.read(public_filename), File.read(private_filename))
end

Instance Method Details

#decrypt(text) ⇒ Object

Decrypts given text using key instance.



79
80
81
# File 'lib/metafusion/crypto/base.rb', line 79

def decrypt(text)
  @public_key.send("public_decrypt", Base64.decode64(text))
end

#encrypt(text) ⇒ Object

Encrypts given text using key instance. This assumes that private key data was provided at instantiation step.



74
75
76
# File 'lib/metafusion/crypto/base.rb', line 74

def encrypt(text)
  Base64.encode64(@private_key.send("private_encrypt", text)) if @private_key
end