Module: SecureString::RSAMethods::InstanceMethods

Defined in:
lib/secure_string/rsa_methods.rb

Overview

Adds instance methods for OpenSSL::PKey::RSA support via inclusion of SecureString::RSAMethods to a class.

Instance Method Summary collapse

Instance Method Details

#from_rsa(private_key) ⇒ Object

Given an RSA private key, it decrypts the data string back into the original text.



51
52
53
54
# File 'lib/secure_string/rsa_methods.rb', line 51

def from_rsa(private_key)
  key = OpenSSL::PKey::RSA.new(private_key)
  return self.class.new( key.private_decrypt(self) )
end

#sign(private_key, digest_obj = OpenSSL::Digest::SHA256.new) ⇒ Object

Signs the given message using hte given private key.

By default, signs using SHA256, but another digest object can be given.



59
60
61
62
63
# File 'lib/secure_string/rsa_methods.rb', line 59

def sign(private_key, digest_obj=OpenSSL::Digest::SHA256.new)
  digest_obj = (digest_obj.kind_of?(Class) ? digest_obj.new : digest_obj)
  key = OpenSSL::PKey::RSA.new(private_key)
  return self.class.new( key.sign(digest_obj, self) )
end

#to_rsa(public_key) ⇒ Object

Given an RSA public key, it RSA encrypts the data string.

Note that the key must be 11 bytes longer than the data string or it doesn’t work.



45
46
47
48
# File 'lib/secure_string/rsa_methods.rb', line 45

def to_rsa(public_key)
  key = OpenSSL::PKey::RSA.new(public_key)
  return self.class.new( key.public_encrypt(self) )
end

#verify?(public_key, signature, digest_obj = OpenSSL::Digest::SHA256.new) ⇒ Boolean

Verifies the given signature matches the messages digest, using the signer’s public key.

By default, verifies using SHA256, but another digest object can be given.

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/secure_string/rsa_methods.rb', line 69

def verify?(public_key, signature, digest_obj=OpenSSL::Digest::SHA256.new)
  digest_obj = (digest_obj.kind_of?(Class) ? digest_obj.new : digest_obj)
  key = OpenSSL::PKey::RSA.new(public_key)
  return key.verify(digest_obj, signature.to_s, self)
end