Title: Encrypt your data with MessageEncryptor · Issue #10 · jollygoodcode/jollygoodcode.github.io · GitHub
Open Graph Title: Encrypt your data with MessageEncryptor · Issue #10 · jollygoodcode/jollygoodcode.github.io
X Title: Encrypt your data with MessageEncryptor · Issue #10 · jollygoodcode/jollygoodcode.github.io
Description: Rails has a built-in class MessageEncryptor, which uses OpenSSL::Cipher to perform encryption. Read How does MessageEncryptor works or the source code if you are interested in its inner workings. If you have been working with Ruby for a ...
Open Graph Description: Rails has a built-in class MessageEncryptor, which uses OpenSSL::Cipher to perform encryption. Read How does MessageEncryptor works or the source code if you are interested in its inner workings. I...
X Description: Rails has a built-in class MessageEncryptor, which uses OpenSSL::Cipher to perform encryption. Read How does MessageEncryptor works or the source code if you are interested in its inner workings. I...
Opengraph URL: https://github.com/jollygoodcode/jollygoodcode.github.io/issues/10
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Encrypt your data with MessageEncryptor","articleBody":"Rails has a built-in class [MessageEncryptor](http://api.rubyonrails.org/classes/ActiveSupport/MessageEncryptor.html), which uses [OpenSSL::Cipher](http://ruby-doc.org/stdlib-2.2.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html) to perform encryption. Read [How does MessageEncryptor works](http://www.monkeyandcrow.com/blog/reading_rails_how_does_message_encryptor_work) or [the source code](https://github.com/rails/rails/blob/4f51433/activesupport/lib/active_support/message_encryptor.rb) if you are interested in its inner workings.\n\nIf you have been working with Ruby for a while, you would probably be familiar with `attr_accessor`, `attr_reader` and `attr_writer` which provide you with getter and/or setter. These methods are mostly convenient, but if you are required to store sensitive information in the database (i.e. OAuth token), then you will need your own custom getter and setter to protect the sensitive data.\n\nYou can make use of the `#encrypt_and_sign` and `#decrypt_and_verify` methods available in Rails to encrypt and decrypt data in your custom getter and setter, and here's how you can do it:\n\n``` ruby\ndef token=(value)\n encrypted_token = cryptor.encrypt_and_sign(value)\n self[:token] = encrypted_token\nend\n\ndef token\n encrypted_token = self[:token]\n\n if encrypted_token.present?\n cryptor.decrypt_and_verify(encrypted_token)\n end\nend\n\nprivate\n\n def cryptor\n ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)\n end\n```\n\nFeeling paranoid? You can also pass in additional cipher:\n\n``` ruby\nActiveSupport::MessageEncryptor.new(\n Rails.application.secrets.secret_key_base,\n cipher: \"aes-256-ecb\"\n)\n```\n\nPro-tip: You can get a list of available ciphers with `$ openssl list-cipher-commands`.\n\nTesting it is also very simple (with RSpec):\n\n``` ruby\ndescribe \"#token=\" do\n it \"saves encrypted token in database\" do\n user = build(:user)\n user.token = \"oauth token\"\n\n user.save\n\n expect(user[\"token\"]).not_to eq(\"oauth token\")\n end\nend\n\ndescribe \"#token\" do\n it \"returns decrypted token upon retrieval\" do\n user = build(:user)\n user.token = \"oauth token\"\n\n user.save\n\n expect(user.reload.token).to eq(\"oauth token\")\n end\nend\n```\n\nNote that I have used [FactoryGirl](https://github.com/thoughtbot/factory_girl) to perform a `build(:user)`. \n\nAlternatively, if you need a lot more features for your encrypted fields, you can also check out [attr_encrypted gem](https://github.com/attr-encrypted/attr_encrypted).\n\nRemember to secure sensitive information you store in your database! :heart:\n## \n\nThanks for reading! \n\n@JuanitoFatas :pencil2: [Jolly Good Code](http://www.jollygoodcode.com)\n### About Jolly Good Code\n\n[](http://www.jollygoodcode.com)\n\nWe specialise in Agile practices and Ruby, and we love contributing to open source. \n[Speak to us](http://www.jollygoodcode.com/#get-in-touch) about your next big idea, or [check out our projects](http://www.jollygoodcode.com/open-source). \n","author":{"url":"https://github.com/JuanitoFatas","@type":"Person","name":"JuanitoFatas"},"datePublished":"2015-11-17T07:47:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/10/jollygoodcode.github.io/issues/10"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:10c6fd15-a7be-9a77-0f0b-fc0e33583567 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB58:3CD7D9:9ADC1AE:D28A984:6A5EDE20 |
| html-safe-nonce | 918df6ffa3e873de53fed9e5271ac7e96526bdf2224244b3e88cc2160b363b4d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjU4OjNDRDdEOTo5QURDMUFFOkQyOEE5ODQ6NkE1RURFMjAiLCJ2aXNpdG9yX2lkIjoiNTQxNDUzNTE2ODcwMjQ3MTcxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 79f2d8de282c1ac73154a67e4d0f9f483ea9c69084568bd4413f9cfca180a08b |
| hovercard-subject-tag | issue:117302220 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/jollygoodcode/jollygoodcode.github.io/10/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c92443ba9ca8169c63bb1bc65dede17e6a1a9514a2e803ccbaf3e5b36a1dab52/jollygoodcode/jollygoodcode.github.io/issues/10 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c92443ba9ca8169c63bb1bc65dede17e6a1a9514a2e803ccbaf3e5b36a1dab52/jollygoodcode/jollygoodcode.github.io/issues/10 |
| og:image:alt | Rails has a built-in class MessageEncryptor, which uses OpenSSL::Cipher to perform encryption. Read How does MessageEncryptor works or the source code if you are interested in its inner workings. I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JuanitoFatas |
| hostname | github.com |
| expected-hostname | github.com |
| None | 82d0004a35927bdb00c652a57f456c55aa0eda4ade1bc7956d7510fdea6e454b |
| turbo-cache-control | no-preview |
| go-import | github.com/jollygoodcode/jollygoodcode.github.io git https://github.com/jollygoodcode/jollygoodcode.github.io.git |
| octolytics-dimension-user_id | 5326832 |
| octolytics-dimension-user_login | jollygoodcode |
| octolytics-dimension-repository_id | 39484578 |
| octolytics-dimension-repository_nwo | jollygoodcode/jollygoodcode.github.io |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 39484578 |
| octolytics-dimension-repository_network_root_nwo | jollygoodcode/jollygoodcode.github.io |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d2d913dd727d2d7ac189fb4d05b10bc34ecd03ee |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width