Class: NotAnalyticsClient::MessageEncryptor

Inherits:
Struct
  • Object
show all
Defined in:
lib/not_analytics_client/message_encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



7
8
9
# File 'lib/not_analytics_client/message_encryptor.rb', line 7

def key
  @key
end

Instance Method Details

#decrypt_and_verify(ciphertext, iv:, auth_tag:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/not_analytics_client/message_encryptor.rb', line 24

def decrypt_and_verify(ciphertext, iv:, auth_tag:)
  cipher = OpenSSL::Cipher.new('aes-256-gcm')
  cipher.decrypt
  cipher.key = Base64.decode64(key)
  cipher.iv = Base64.decode64(iv)
  cipher.auth_data = ''
  cipher.auth_tag = Base64.decode64(auth_tag)

  cipher.update(Base64.decode64(ciphertext)) + cipher.final
end

#encrypt_and_sign(plain) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/not_analytics_client/message_encryptor.rb', line 8

def encrypt_and_sign(plain)
  cipher = OpenSSL::Cipher.new('aes-256-gcm')
  cipher.encrypt
  cipher.key = Base64.decode64(key)
  iv = cipher.random_iv
  cipher.auth_data = ''
  ciphertext = cipher.update(plain) + cipher.final
  auth_tag = cipher.auth_tag

  Struct.new(:ciphertext, :iv, :auth_tag).new(
    Base64.encode64(ciphertext),
    Base64.encode64(iv),
    Base64.encode64(auth_tag),
  )
end