{"id":22340331,"url":"https://github.com/kuntoaji/enkrip","last_synced_at":"2025-06-21T05:35:13.017Z","repository":{"id":54232791,"uuid":"154975110","full_name":"kuntoaji/enkrip","owner":"kuntoaji","description":"encrypt \u0026 decrypt Active Record attributes with Message Encryptor","archived":false,"fork":false,"pushed_at":"2023-01-20T13:26:09.000Z","size":19,"stargazers_count":9,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T16:06:10.151Z","etag":null,"topics":["activemodel","activerecord","activesupport","decryption","encryption","gem","message-encryptor","ruby","ruby-gem","ruby-on-rails","security"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kuntoaji.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-27T14:44:07.000Z","updated_at":"2020-02-08T00:02:21.000Z","dependencies_parsed_at":"2023-02-12T02:30:37.576Z","dependency_job_id":null,"html_url":"https://github.com/kuntoaji/enkrip","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuntoaji%2Fenkrip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuntoaji%2Fenkrip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuntoaji%2Fenkrip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuntoaji%2Fenkrip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kuntoaji","download_url":"https://codeload.github.com/kuntoaji/enkrip/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234862689,"owners_count":18898398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activemodel","activerecord","activesupport","decryption","encryption","gem","message-encryptor","ruby","ruby-gem","ruby-on-rails","security"],"created_at":"2024-12-04T07:11:17.394Z","updated_at":"2025-01-20T22:07:01.938Z","avatar_url":"https://github.com/kuntoaji.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/enkrip.svg)](https://badge.fury.io/rb/enkrip)\n\n# Enkrip\n\nEncrypt \u0026 decrypt Active Record's model attributes with [ActiveSupport::MessageEncryptor](https://api.rubyonrails.org/v5.2.1/classes/ActiveSupport/MessageEncryptor.html). See [Enkrip Example Rails Application](https://github.com/kuntoaji/enkrip_example) for demo.\n\n## Goals\n\n* Seamlessly encrypt and decrypt value for both string and numeric attribute\n* Compatible with Active Model validation\n* Automatically convert numeric attributes to desired format after decryption\n\n## Limitations\n\n* All attributes that are defined in `numeric_attributes` will be forced to use UTF-8 encoding\n* Enkrip requires Active Record 5.2 or newer\n* Does not compatible with [activerecord-import](https://rubygems.org/gems/activerecord-import)\n* In some cases, does not compatible with [ActiveRecord::Attributes](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html)\n\n## Installation\n\nAdd `enkrip` to your Rails app’s Gemfile and run bundle install:\n\n```ruby\ngem 'enkrip'\n```\n\n## Configuration\n\nAfter installation, you need to define `ENKRIP_LENGTH`, `ENKRIP_SALT`, and `ENKRIP_SECRET` environment variables:\n\n```bash\n# example\n# 32 is default value from ActiveSupport::MessageEncryptor.key_len\nexport ENKRIP_LENGTH=32\n\n# you can generate this value with SecureRandom.random_bytes(YOUR_ENKRIP_LENGTH)\nexport ENKRIP_SALT=random_salt_with_length_32\n\n# 32 random characters\nexport ENKRIP_SECRET=random_secret_with_length_32\n```\n\n## Usage\n\nUse `text` data type for encrypted attributes\n\n```ruby\n# migration\n\nclass CreatePosts \u003c ActiveRecord::Migration[5.2]\n  def change\n    create_table :posts do |t|\n      t.text :my_string\n      t.text :my_numeric\n\n      t.timestamps\n    end\n  end\nend\n```\n\nAfter run the migration, define your encrypted attributes\n\n```ruby\n# Active Record model\n\nclass Post \u003c ActiveRecord::Base\n  include Enkrip::Model\n\n  enkrip_configure do |config|\n    config.string_attributes \u003c\u003c :my_string\n    config.numeric_attributes \u003c\u003c :my_numeric\n    config.purpose = :example # optional, default is nil\n    config.convert_method_for_numeric_attribute = :to_f # optional, default is to_i\n    config.default_value_if_numeric_attribute_blank =  0.0 # optional, default is 0\n  end\n\n  validates :my_numeric, numericality: { greater_than: 0 }\n  validates :my_string, presence: true\nend\n```\n\nYou can check encrypted value from rails console with raw query\n\n```ruby\n# Rails 5.2 console\npost = Post.new =\u003e #\u003cPost id: nil, my_string: nil, my_numeric: nil, created_at: nil, updated_at: nil\u003e\npost.valid? # =\u003e false\npost.errors.full_messages # =\u003e [\"My numeric must be greater than 0\", \"My string can't be blank\"]\n\npost.my_string = \"aloha\" # =\u003e \"aloha\"\npost.my_numeric = 5 # =\u003e 5\npost.save # =\u003e true\n\nraw = ActiveRecord::Base.connection.exec_query 'SELECT my_string, my_numeric FROM posts limit 1'\n\nraw.rows.first[raw.columns.find_index('my_string')]\n# =\u003e \"TUVQcnRBck5oYzMvRlRZUWR3Mzlzdz09LS1tZ09xNGNYbnkzdFc2d1duMEIrdUdBPT0=--ffae1f04753ca5c636915746a4c6fccf81897138\"\n\nraw.rows.first[raw.columns.find_index('my_numeric')]\n# =\u003e \"TkdSbURRNzVMbUF6MjF0bjI2ZEtmQT09LS1rRHhqQ2xpWGhYaHBoRlhCRnVZSmh3PT0=--74e45e6c96df78258a1731994a71a74c5047d655\"\n\npost.reload\npost.my_string # =\u003e \"aloha\"\npost.my_numeric # =\u003e 5\n```\n\n## Usage For non-Active Record Model\nYou can use `Enkrip::Engine.encrypt` and `Enkrip::Engine.decrypt` to encrypt and decrypt a value.\n\n```ruby\nmy_string = 'hello world'\n\nencrypted_my_string = Enkrip::Engine.encrypt my_string\n# =\u003e \"MzZ1M0RDSWdQQ0VaRVJXT3NBYlVTWExWVnVSbXNBeXRMSC9wYWdoeW5Ddz0tLVNVT2l6NDJCd1ZxbW1lYnl2eC9PakE9PQ==--c7436c403595c18fef802a51be29f73d5bb73f19\"\n\nEnkrip::Engine.decrypt encrypted_my_string\n# =\u003e \"hello world\"\n```\n\nYou can pass `purpose` to the `Enkrip::Engine.decrypt` and `Enkrip::Engine.encrypt`.\n```ruby\n# you can pass purpose parameter, default purpose is nil.\nsecond_string = 'hello world 2'\nanother_encrypted_my_string = Enkrip::Engine.encrypt second_string, purpose: :example_purpose\n\nEnkrip::Engine.decrypt another_encrypted_my_string, purpose: :random_purpose # =\u003e nil\nEnkrip::Engine.decrypt another_encrypted_my_string # =\u003e nil\nEnkrip::Engine.decrypt another_encrypted_my_string, purpose: :example_purpose # =\u003e \"hello world 2\"\n\n\n```\n\n## License\n\nEnkrip is released under the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuntoaji%2Fenkrip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkuntoaji%2Fenkrip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuntoaji%2Fenkrip/lists"}