1

何时encryption_key由 调用attr_encrypted:passphrase尚未设置。加密密钥最终是盐的 sha1 散列;它应该是密码和盐的 sha1 哈希。

salt 在创建时生成并保存在数据库中。

如何:passphrase在加密密钥中使用虚拟属性?

有什么建议么?

为简洁起见,我省略了一堆代码。

class Employee < ActiveRecord::Base
    require 'digest/sha1'
    attr_accessor :passphrase, :ssn
    attr_accessible :passphrase, :ssn
    attr_encrypted :ssn, :key => proc { |employee| "#{employee.encryption_key}" }

    def encryption_key
        unless salt?
            self.salt = Digest::SHA1.hexdigest(generate_salt)
        end

        Digest::SHA1.hexdigest([passphrase, self.salt].join)
    end
end

class EmployeesController < ApplicationController
    def create
        @employee = @parent.employees.new(params[:employee])
        if @employee.save
            redirect_to @parent
        else
            render action: "new"
        end
    end
end

提前致谢!

4

1 回答 1

0

尝试在密码和其他属性之后设置 ssn

class EmployeesController < ApplicationController
  def create
    ssn = params[:employee].delete(:ssn)
    @employee = @parent.employees.new(params[:employee])
    @employee.ssn = ssn
    if @employee.save
        redirect_to @parent
    else
        render action: "new"
    end
  end
end
于 2012-05-31T23:28:00.830 回答