Module: SecurizeString::RSAMethods::InstanceMethods
- Defined in:
- lib/securize_string/rsa_methods.rb
Overview
Adds instance methods for OpenSSL::PKey::RSA support via inclusion of SecurizeString::RSAMethods to a class.
Instance Method Summary collapse
-
#from_rsa(key) ⇒ Object
Given an RSA private key, it decrypts the data string back into the original text.
-
#private_rsa_key? ⇒ Boolean
Interpret the conents of the string as an RSA key, and determine if it is private.
-
#public_rsa_key? ⇒ Boolean
Interpret the conetents of the string as an RSA key, and determine if it is public.
-
#sign(private_key, digest_method = 'SHA-256') ⇒ Object
Signs the given message using hte given private key.
-
#to_rsa(key) ⇒ Object
Given an RSA public key, it RSA encrypts the data string.
-
#verify?(public_key, signature, digest_method = 'SHA-256') ⇒ Boolean
Verifies the given signature matches the messages digest, using the signer’s public key.
Instance Method Details
#from_rsa(key) ⇒ Object
Given an RSA private key, it decrypts the data string back into the original text.
52 53 54 55 56 |
# File 'lib/securize_string/rsa_methods.rb', line 52 def from_rsa(key) key = OpenSSL::PKey::RSA.new(key) plain_text = key.private? ? key.private_decrypt(self.to_s) : key.public_decrypt(self.to_s) return self.class.new(plain_text) end |
#private_rsa_key? ⇒ Boolean
Interpret the conents of the string as an RSA key, and determine if it is private.
92 93 94 95 |
# File 'lib/securize_string/rsa_methods.rb', line 92 def private_rsa_key? key = OpenSSL::PKey::RSA.new(self.to_s) return key.private? end |
#public_rsa_key? ⇒ Boolean
Interpret the conetents of the string as an RSA key, and determine if it is public.
Even though private keys contain all the information necessary to reconstitute a public key, this method returns false. This is in contrast to the behavior of OpenSSL::PKey::RSA, which return true for both public and private checks with a private key (since it reconstituted the public key and it is available for use).
86 87 88 89 |
# File 'lib/securize_string/rsa_methods.rb', line 86 def public_rsa_key? # There is an interesting bug I came across, where +public?+ can be true on a private key! return !private_rsa_key? end |
#sign(private_key, digest_method = 'SHA-256') ⇒ Object
Signs the given message using hte given private key.
By default, verifies using SHA256, but another digest method can be given using the list of DigestFinder supported digests.
62 63 64 65 66 |
# File 'lib/securize_string/rsa_methods.rb', line 62 def sign(private_key, digest_method='SHA-256') digest_obj = DigestFinder.find(digest_method).new key = OpenSSL::PKey::RSA.new(private_key) return self.class.new( key.sign(digest_obj, self) ) end |
#to_rsa(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 49 |
# File 'lib/securize_string/rsa_methods.rb', line 45 def to_rsa(key) key = OpenSSL::PKey::RSA.new(key) cipher_text = key.private? ? key.private_encrypt(self.to_s) : key.public_encrypt(self.to_s) return self.class.new(cipher_text) end |
#verify?(public_key, signature, digest_method = 'SHA-256') ⇒ Boolean
Verifies the given signature matches the messages digest, using the signer’s public key.
By default, verifies using SHA256, but another digest method can be given using the list of DigestFinder supported digests.
73 74 75 76 77 |
# File 'lib/securize_string/rsa_methods.rb', line 73 def verify?(public_key, signature, digest_method='SHA-256') digest_obj = DigestFinder.find(digest_method).new key = OpenSSL::PKey::RSA.new(public_key) return key.verify(digest_obj, signature.to_s, self) end |