Class: Ribbon::Intercom::Utils::Signer
- Inherits:
-
Object
- Object
- Ribbon::Intercom::Utils::Signer
- Defined in:
- lib/ribbon/intercom/utils/signer.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(key = self.class.random_key) ⇒ Signer
constructor
A new instance of Signer.
- #sign(data) ⇒ Object
- #verify(signed_data) ⇒ Object
Constructor Details
#initialize(key = self.class.random_key) ⇒ Signer
Returns a new instance of Signer.
19 20 21 22 23 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 19 def initialize(key=self.class.random_key) raise ArgumentError, "key must be defined" unless key @key = key.dup.freeze @_digest = OpenSSL::Digest::SHA256.new end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
17 18 19 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 17 def key @key end |
Class Method Details
.random_key ⇒ Object
8 9 10 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 8 def random_key SecureRandom.random_bytes(32) end |
.random_salt ⇒ Object
12 13 14 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 12 def random_salt SecureRandom.random_bytes(8) end |
Instance Method Details
#sign(data) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 25 def sign(data) unless data.is_a?(String) && data.encoding == Encoding::BINARY raise ArgumentError, "data must be a binary encoded string" end salt = self.class.random_salt signature = _sign(salt, data) _encode(signature, salt, data) end |
#verify(signed_data) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/ribbon/intercom/utils/signer.rb', line 35 def verify(signed_data) unless signed_data.is_a?(String) && signed_data.encoding == Encoding::BINARY raise ArgumentError, "signed_data must be a binary encoded string" end signature, salt, data = _decode(signed_data) data if _sign(salt, data) == signature end |