百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

attr_encrypted

> 编程语言
开源

生成加密和解密属性的 attr_accessors

2.0K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

生成加密和解密属性的 attr_accessors

attr_encrypted

生成可透明地加密和解密属性的 attr_accessors。 但是,当您使用 ActiveRecord 或 Sequel 时,您会获得一些额外的功能。

安装

在 gemfile 中添加 attr_encrypted:

ruby
  gem "attr_encrypted"

然后安装 gem:

bash
  bundle install

使用方法

如果您使用了像 ActiveRecord 或 Sequel 这样的 ORM,使用 attr_encrypted 非常简单:

ruby
  class User
    attr_encrypted :ssn, key: 'This is a key that is 256 bits!!'
  end

如果您使用的是 PORO,则需要通过扩展类来做一些额外的工作:

ruby
  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· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言