生成加密和解密属性的 attr_accessors
生成可透明地加密和解密属性的 attr_accessors。
但是,当您使用 ActiveRecord 或 Sequel 时,您会获得一些额外的功能。
在 gemfile 中添加 attr_encrypted:
gem "attr_encrypted"然后安装 gem:
bundle install如果您使用了像 ActiveRecord 或 Sequel 这样的 ORM,使用 attr_encrypted 非常简单:
class User
attr_encrypted :ssn, key: 'This is a key that is 256 bits!!'
end如果您使用的是 PORO,则需要通过扩展类来做一些额外的工作:
class User
extend AttrEncrypted
attr_accessor :name
attr_encrypted :ssn, key: 'This is a key that is 256 bits!!'
def load
# loads the stored data
end
def save
# saves the :name and :encrypted_ssn attributes somewhere (e.g. filesystem, database, etc)
end
end
user = User.new
user.ssn = '123-45-6789'
user.ssn # returns the unencrypted object ie. '123-45-6789'
user.encrypted_ssn # returns the encrypted version of :ssn
user.save
user = User.load
user.ssn # decrypts :encrypted_ssn and returns '123-45-6789'每个属性都有两个类方法可用: User.encrypt_email 和 User.decrypt_email。它们接受的参数与 attr_encrypted 类方法接受的参数相同。例如:
创建或修改模型使用的表,以添加一个 co
暂无开放 Issues,或尚未同步最近议题。