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_



176
177
178
179
# File 'lib/pod4/encrypting.rb', line 176

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



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

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?
    iv = cipher.random_iv
    set_encryption_iv(iv)
    hash[self.class.encryption_iv_column] = Base64.strict_encode64(iv)
  end

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

  Octothorpe.new(hash)
end

#map_to_model(ot) ⇒ Object

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/pod4/encrypting.rb', line 156

def map_to_model(ot)
  hash   = ot.to_h
  cipher = get_cipher(:decrypt)

  # The IV is not in columns, we need to de-base-64 it and set it on the model ourselves
  if use_iv?
    iv = Base64.strict_decode64 hash[self.class.encryption_iv_column]
    set_encryption_iv(iv)
  end

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

  super Octothorpe.new(hash)
end