Class: Nuntius::Key
- Inherits:
-
Object
- Object
- Nuntius::Key
- Defined in:
- lib/nuntius/key.rb
Instance Attribute Summary collapse
-
#encryption_key ⇒ Object
Returns the value of attribute encryption_key.
-
#signature_key ⇒ Object
Returns the value of attribute signature_key.
Instance Method Summary collapse
- #==(key) ⇒ Object
- #decrypt(string) ⇒ Object
- #encrypt(string) ⇒ Object
-
#initialize(key_data) ⇒ Key
constructor
A new instance of Key.
- #private? ⇒ Boolean
- #sign(string) ⇒ Object
- #validate(message, signature) ⇒ Object
Constructor Details
#initialize(key_data) ⇒ Key
Returns a new instance of Key.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nuntius/key.rb', line 6 def initialize(key_data) case key_data when Hash self.signature_key = OpenSSL::PKey::RSA.new( key_data[:signature] ) self.encryption_key = OpenSSL::PKey::RSA.new( key_data[:encryption] ) when Nuntius::Key self.signature_key = key_data.signature_key self.encryption_key = key_data.encryption_key else self.signature_key = self.encryption_key = OpenSSL::PKey::RSA.new(key_data) end end |
Instance Attribute Details
#encryption_key ⇒ Object
Returns the value of attribute encryption_key.
4 5 6 |
# File 'lib/nuntius/key.rb', line 4 def encryption_key @encryption_key end |
#signature_key ⇒ Object
Returns the value of attribute signature_key.
3 4 5 |
# File 'lib/nuntius/key.rb', line 3 def signature_key @signature_key end |
Instance Method Details
#==(key) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/nuntius/key.rb', line 23 def ==(key) case key when Nuntius::Key signature_key == key.signature_key && encryption_key == key.encryption_key else false end end |
#decrypt(string) ⇒ Object
48 49 50 |
# File 'lib/nuntius/key.rb', line 48 def decrypt(string) encryption_key.private_decrypt(string) end |
#encrypt(string) ⇒ Object
44 45 46 |
# File 'lib/nuntius/key.rb', line 44 def encrypt(string) encryption_key.public_encrypt(string) end |
#private? ⇒ Boolean
19 20 21 |
# File 'lib/nuntius/key.rb', line 19 def private? signature_key.private? && encryption_key.private? end |
#sign(string) ⇒ Object
33 34 35 36 37 |
# File 'lib/nuntius/key.rb', line 33 def sign(string) digest = OpenSSL::Digest::SHA512.new.digest(string) signature_key.private_encrypt(digest) end |
#validate(message, signature) ⇒ Object
39 40 41 42 |
# File 'lib/nuntius/key.rb', line 39 def validate(,signature) digest = OpenSSL::Digest::SHA512.new.digest() digest == signature_key.public_decrypt(signature) end |