Fahad Idrees
Fahad Idrees

Reputation: 188

I am getting nil and Empty when used attr_encrypted for activerecord storage

I am using https://github.com/attr-encrypted/attr_encrypted for the encryptions of my active record for ***User model***who have following attributes.

 
  activation_hash string  
  details string  
  email string  
  imei string  
  password string  
  registration_id string  
  secure_hash string  

I used these attributes as

attr_encrypted_options.merge!(:prefix => 'android_', :suffix => '_sheild')  
attr_encrypted :activation_hash, :key => Getter::encryption_key, :encode => true  
attr_encrypted :active, :key => Getter::encryption_key, :encode => true  
attr_encrypted :code, :key => Getter::encryption_key, :encode => true  
attr_encrypted :details, :key => Getter::encryption_key, :encode => true  
attr_encryptor :email, :key => "this is awais"  
attr_encrypted :password, :key => Getter::encryption_key, :encode => true  
attr_encrypted :registration_id, :key => Getter::encryption_key, :encode => true  
attr_encrypted :secure_hash, :key => Getter::encryption_key, :encode => true
attr_encrypted :imei, :key => Getter::encryption_key, :encode => true

as mentioned in the attr_encrypted wiki but when i save the record empty string in stored in databases.

In Getter i have added the generic encryption key method..

    module Getter

      def self.encryption_key
        keys = OpenSSL::Cipher::Cipher.new('aes-256-cbc').random_iv
        return keys
      end
    end

Do i need to add migration with encrypted attributes i added in User model.. My aim to to encrypt the activerecord data and save that fields to databases and when i retrieve i can get decrypted record back but on DB level These records are not accessible.

Can you please tell me what i do wrong?? Do i need to switch gem??

Your suggestions are highly appreciated

Upvotes: 2

Views: 1824

Answers (1)

SylvainB
SylvainB

Reputation: 4820

According to the attr_encrypted documentation:

By default, the encrypted attribute name is encrypted_#{attribute} (e.g. attr_encrypted :email would create an attribute named encrypted_email). So, if you're storing the encrypted attribute in the database, you need to make sure the encrypted_#{attribute} field exists in your table.

It seems like you did not name your fields in the expected format.

Upvotes: 2

Related Questions