Skip to content

Deferred Encryption

prashantjois edited this page Jun 16, 2014 · 3 revisions

Defer encryption in order to validate your attribute

The default is for Strongbox to encrypt when the value is set. If you enable deferred encryption by adding :deferred_encryption => true to the encrypt_with_public_key configuration, then you should be able to validate with something like:

class MyUser < ActiveRecord::Base
	validate :validate_contact_number_format
	encrypt_with_public_key :contact_number, 
                            key_pair: Rails.root.join("config", "keypair.pem"),
                            deferred_encryption: true
	
	def validate_contact_number_format
		if entry.start_with?('555')
			errors.add :contact_number, "Invalid Contact Number"
		end
	end
end

If works because the deferred encryption setting means a password isn't required to decrypt until the record is saved.

Reference: https://github.com/spikex/strongbox/issues/41#issuecomment-46045637