Rails: storing encrypted data in database

后端 未结 5 932
后悔当初
后悔当初 2020-12-30 06:03

I want to encrypt database because confidential data is being stored. I use mongodb with mongoid. It possible for this kind of database? And what alternatives can you recome

5条回答
  •  不知归路
    2020-12-30 06:46

    I have gotten attr_encrypted working with Mongo and Mongoid. It takes only a few tweaks.

    Make sure that all of the encrypted_ fields that are automatically created by attr_encrypted are explicitly created in the model. For instance, if you have:

        attr_encrypted :email, :key => 'blah blah blah', :encode => true
    

    you need to have:

        field :email, :type => String
        field :encrypted_email, :type => String
    

    Also notice you need to tell it to encode the encrypted string otherwise Mongo will complain loudly.

    Lastly, if you're encrypting a hash, do this:

        field :raw_auth_hash, :type => Hash
        field :encrypted_raw_auth_hash, :type => String
    
        attr_encrypted :raw_auth_hash, :key => 'blah', :marshal => true, :encode => true
    

提交回复
热议问题