【问题标题】:I am getting nil and Empty when used attr_encrypted for activerecord storage将 attr_encrypted 用于 activerecord 存储时,我得到 nil 和 Empty
【发布时间】:2014-06-17 09:39:26
【问题描述】:

我正在使用https://github.com/attr-encrypted/attr_encrypted 为具有以下属性的***用户模型***的活动记录加密

激活哈希字符串 详细信息字符串 电子邮件字符串 imei 字符串 密码字符串 registration_id 字符串 安全哈希字符串

我将这些属性用作

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

如 attr_encrypted wiki 中所述,但是当我将记录空字符串保存在存储在数据库中时。

在 Getter 中,我添加了通用加密密钥方法..

    module Getter

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

我是否需要使用我在用户模型中添加的加密属性添加迁移.. 我的目标是加密活动记录数据并将该字段保存到数据库中,当我检索时,我可以取回解密的记录,但在数据库级别这些记录不可访问。

你能告诉我我做错了什么吗?需要换宝石吗??

我们非常感谢您的建议

【问题讨论】:

  • From the doc: "默认情况下,加密的属性名称是 encrypted_#{attribute}(例如 attr_encrypted :email 会创建一个名为 encrypted_email 的属性)。因此,如果您将加密的属性存储在数据库,您需要确保表中存在 encrypted_#{attribute} 字段。”你的数据库中有这样的字段吗?

标签: ruby-on-rails ruby activerecord encryption attr-encrypted


【解决方案1】:

According to the attr_encrypted documentation:

默认情况下,加密属性名称为 encrypted_#{attribute}(例如 attr_encrypted :email 将创建一个名为 encrypted_email 的属性)。因此,如果您将加密属性存储在数据库中,则需要确保表中存在 encrypted_#{attribute} 字段。

您似乎没有以预期的格式命名您的字段。

【讨论】:

  • 或者你可以重命名它们?
  • 我需要在用户表中添加新列。` encrypted_activation_hash encrypted_details encrypted_email encrypted_imei encrypted_pa​​ssword encrypted_registration_id `如果我有这些属性,那么我是否需要将用户创建为 user.encrypted_email="blah blah" 或user.email=“废话”
  • 第一个选项,我会说。编辑:没关系。我不知道,你必须尝试一下。
  • 如何将加密数据添加到这些字段?我需要另存为 user.encrypted_email 吗?我这样做了,但空电子邮件存储在数据库中:(你能指导我以正确的方式这样做吗
  • 我的猜测是:由于它像 attr_accessor 助手一样使用,因此在您的 rails 应用程序代码中,您应该使用 attr_encrypted 调用旁边作为参数给出的名称(因此对于 encrypted_email 仅使用“电子邮件”)但是在您的迁移中,将实际字段命名为 encrypted_email。我的猜测是 attr_encrypted 会从那里解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2012-06-08
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多