Class: Voynich::ActiveRecord::Value

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/voynich/active_record/models/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/voynich/active_record/models/value.rb', line 8

def context
  @context
end

#plain_valueObject

Returns the value of attribute plain_value.



8
9
10
# File 'lib/voynich/active_record/models/value.rb', line 8

def plain_value
  @plain_value
end

Instance Method Details

#decryptObject



19
20
21
22
23
24
25
26
# File 'lib/voynich/active_record/models/value.rb', line 19

def decrypt
  encrypted_data = JSON.parse(self.ciphertext, symbolize_names: true)
  @plain_value = AES.new(data_key.plaintext, (context || {}).to_json).decrypt(
    encrypted_data[:c],
    iv: encrypted_data[:iv],
    tag: encrypted_data[:t]
  )
end

#encryptObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/voynich/active_record/models/value.rb', line 28

def encrypt
  return if plain_value.nil?
  encrypted = AES.new(data_key.plaintext, (context || {}).to_json).encrypt(plain_value)
  self.ciphertext = {
    c:  encrypted[:content],
    t:  encrypted[:tag],
    iv: encrypted[:iv],
    ad: encrypted[:auth_data]
  }.to_json
end