class Voynich::ActiveRecord::Value

Attributes

context[RW]
plain_value[RW]

Public Instance Methods

decrypt() click to toggle source
# 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
encrypt() click to toggle source
# 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

Private Instance Methods

find_or_create_data_key() click to toggle source
# File lib/voynich/active_record/models/value.rb, line 41
def find_or_create_data_key
  if data_key.nil?
    self.data_key = DataKey.find_or_create_by!(name: random_key_name, cmk_id: Voynich.kms_cmk_id)
  end
end
generate_uuid() click to toggle source
# File lib/voynich/active_record/models/value.rb, line 51
def generate_uuid
  self.uuid ||= SecureRandom.uuid
end
random_key_name() click to toggle source
# File lib/voynich/active_record/models/value.rb, line 47
def random_key_name
  "auto:#{Random.rand(Voynich.auto_data_key_count)}"
end