class TrustpilotBusinessLinks

Constants

ENCRYPTION_CIPHER
HMAC_DIGEST

Attributes

authentication_key[RW]
encryption_key[RW]

Public Class Methods

new(encryption_key, authentication_key) click to toggle source
# File lib/trustpilot-business-links.rb, line 9
def initialize(encryption_key, authentication_key)
  @encryption_key = Base64.strict_decode64(encryption_key)
  @authentication_key = Base64.strict_decode64(authentication_key)
end

Public Instance Methods

encrypt(payload) click to toggle source
# File lib/trustpilot-business-links.rb, line 14
def encrypt(payload)
  encrypted_payload, iv = encrypt_iv(payload)
  hmac = hmac(iv, encrypted_payload)

  encode(iv + encrypted_payload + hmac)
end

Private Instance Methods

encode(payload) click to toggle source
# File lib/trustpilot-business-links.rb, line 39
def encode(payload)
  CGI.escape(Base64.strict_encode64(payload))
end
encrypt_iv(data) click to toggle source
# File lib/trustpilot-business-links.rb, line 25
def encrypt_iv(data)
  cipher = OpenSSL::Cipher.new(ENCRYPTION_CIPHER)
  cipher.encrypt
  cipher.key = encryption_key
  iv = cipher.random_iv
  encrypted_payload = cipher.update(data) + cipher.final

  [encrypted_payload, iv]
end
hmac(iv, encrypted_payload) click to toggle source
# File lib/trustpilot-business-links.rb, line 35
def hmac(iv, encrypted_payload)
  OpenSSL::HMAC.digest(HMAC_DIGEST, authentication_key, iv + encrypted_payload)
end