实际上,我从user_pass
使用硬编码密钥加密字段开始。
class Credential < ApplicationRecord
..
attr_encrypted :user_pass, key: 'This is a key that is 256 bits!!'
..
end
我已经用这个密钥加密了一些数据。现在,我不想以硬编码格式保存密钥,因此将一半密钥保存在文件系统中,另一半保存在表中并将它们组合起来。
class Credential < ApplicationRecord
..
attr_encrypted :user_pass, key: :encryption_key
..
def encryption_key
Rails.root.join('private', 'key').read + Setting.where(name: 'key').last.value
end
end
如何使用当前密钥加密已经加密的数据?