Class: Ribbon::Intercom::Utils::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/ribbon/intercom/utils/signer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = self.class.random_key) ⇒ Signer

Returns a new instance of Signer.

Raises:

  • (ArgumentError)


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

#keyObject (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_keyObject



8
9
10
# File 'lib/ribbon/intercom/utils/signer.rb', line 8

def random_key
  SecureRandom.random_bytes(32)
end

.random_saltObject



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