Giter VIP home page Giter VIP logo

mongoid-encrypted-fields's Introduction

mongoid-encrypted-fields

Build Status Code Climate

New Maintainer Needed

We are actively seeking a new maintainer for this gem! As we no longer use MongoDB as part of our platform, we aren't using the gem for ourselves. As MongoDB and Mongoid continually change, we want to make sure our gem keeps up.

If you're interested, please contact us! Thanks

Description

A library for storing encrypted data in Mongo using Mongoid. We looked at a few alternatives, but wanted something that stored the values securely and unobtrusively.

Mongoid 3 supports custom types that need to only provide a simple interface - allowing us to extend core Ruby types to secure any type while providing a clean interface for developers.

Queries encrypt data before searching the database, so equality matches work automatically.

Prerequisites

  • Mongoid 5+
  • Rails 4+
  • Ruby 2.0+
  • "Bring your own" encryption, see below

Mongoid 3, Mongoid 4 and Rails 3.2 are supported in version 1.x of this gem.

Install

```ruby
gem 'mongoid-encrypted-fields'
```

Searchable vs. Unsearchable

  • Default encrypted fields use a global salt so the same value produces the same encrypted output. Queries work by first encrypting the search term, then searching for the encrypted value.
  • Unsearchable encrypted fields use a unique salt each time a value is encrypted. Encrypting the same value multiple times will generate unique encrypted outputs each time. Queries on unsearchable encrypted fields are not possible.

Usage

  • Configure the ciphers to be used for encrypting field values:

    GibberishCipher can be found in examples - uses the Gibberish gem:

    Mongoid::EncryptedFields.cipher = GibberishCipher.new(ENV['MY_PASSWORD'], ENV['MY_SALT'])
    Mongoid::EncryptedFields.unsearchable_cipher = GibberishCipher.new(ENV['MY_PASSWORD'])
  • Use encrypted types for fields in your models:

    class Person
        include Mongoid::Document
    
        field :ssn, type: Mongoid::EncryptedString              #can search for Person with ssn
        field :name, type: Mongoid::UnsearchableEncryptedString #don't need to search based on name
    end
  • The field getter returns the unencrypted value:

    person = Person.new(ssn: '123456789')
    person.ssn # => '123456789'
  • The encrypted value is accessible with the "encrypted" attribute

    person.ssn.encrypted # => <encrypted string>
    
    # It can also be accessed using the hash syntax supported by Mongoid
    person[:ssn] # => <encrypted string>
  • Finding a model by an encrypted field works automatically (equality only):

    Person.where(ssn: '123456789').count() # ssn is encrypted before querying the database
    Person.where(name: 'John Doe').count() # does not work!  uses a new salt each time the value is encrypted

Known Limitations

  • Currently can encrypt these Mongoid types
    • Date
    • DateTime
    • Hash
    • String
    • Time
  • The uniqueness validator for encrypted fields should always be set to case-sensitive. Encrypted fields cannot support a case-insensitive match.
  • Queries for unsearchable encrypted fields do not work.

Related Articles

Copyright

(c) 2012 Koan Health. See LICENSE.txt for further details.

mongoid-encrypted-fields's People

Contributors

jerryclinesmith avatar biggernoise avatar johnnyshields avatar lacour avatar gkorban avatar joe1chen avatar maxjacobson avatar michaeltrimm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.