Module: Pod4::Encrypting::InstanceMethods

Defined in:
lib/pod4/encrypting.rb

Instance Method Summary collapse

Instance Method Details

#encryption_ivObject

The value of the IV field (whatever it is) _as currently stored on the model_



169
170
171
172
# File 'lib/pod4/encrypting.rb', line 169

def encryption_iv
  return nil unless use_iv?
  instance_variable_get( "@#{self.class.encryption_iv_column}".to_sym )
end

#map_to_interfaceObject

When mapping to the interface, encrypt the encryptable columns from the model



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pod4/encrypting.rb', line 133

def map_to_interface
  hash   = super.to_h
  cipher = get_cipher(:encrypt)

  # If the IV is not set we need to set it both in the model object AND the hash, since we've
  # already obtained the hash from the model object.
  if use_iv? && encryption_iv.nil?
    set_encryption_iv( cipher.random_iv )
    hash[self.class.encryption_iv_column] = encryption_iv
  end

  self.class.encryption_columns.each do |col|
    hash[col] = crypt(cipher, encryption_iv, hash[col].to_s)
  end

  Octothorpe.new(hash)
end

#map_to_model(ot) ⇒ Object

When mapping to the model, decrypt the encrypted columns from the interface



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pod4/encrypting.rb', line 154

def map_to_model(ot)
  hash   = ot.to_h
  cipher = get_cipher(:decrypt)
  iv     = hash[self.class.encryption_iv_column] # not yet set on the model

  self.class.encryption_columns.each do |col|
    hash[col] = crypt(cipher, iv, hash[col])
  end

  super Octothorpe.new(hash)
end