GithubHelp home page GithubHelp logo

kapost / mongoid-encryptor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juno/mongoid-encryptor

0.0 29.0 1.0 116 KB

mongoid-encryptor encrypts and decrypts one or more fields in a Mongoid model.

License: MIT License

Ruby 100.00%

mongoid-encryptor's Introduction

mongoid-encryptor

Build Status

mongoid-encryptor encrypts and decrypts one or more fields in a Mongoid model.

Prerequisites

  • ruby 1.9.2 or 1.9.3
  • mongoid 2.0 or later
  • activesupport 3.0 or later

Install

Put this line in your Gemfile:

gem 'mongoid-encryptor', :require => 'mongoid/encryptor'

Then bundle:

$ bundle

Quick Start

Set up SHA encrypted field in models like this:

class Credential
  include Mongoid::Document
  include Mongoid::Encryptor
  field :password
  encrypts :password
end

>> c = Credential.new
=> #<Credential _id: 4d6383aeb2de3cbea1000001, password: nil>
>> c.password = 'this is a secret'
=> "this is a secret"
>> c.password.encrypted?
=> false
>> c.save
=> true

>> c.password
=> "70a202166f0a78defe464d810f30b50b767cb89a"
>> c.password.encrypted?
=> true
>> c.password.cipher.salt
=> "salt"
>> c.password == 'this is a secret'
=> true

Symmetric encryption

Set up Symmetric encrypted field in models like this:

class Credential
  include Mongoid::Document
  include Mongoid::Encryptor
  field :password
  encrypts :password, :mode => :symmetric, :password => 'key'
end

>> c = Credential.new
=> #<Credential _id: 4d638b6db2de3cc2ca000001, password: nil>
>> c.password = 'this is a secret'
=> "this is a secret"
>> c.password.encrypted?
=> false
>> c.save
=> true

>> c.password
=> "y3HnNrU0HviAl3aw2sWH1KttBLsCLYP1\n"
>> c.password.encrypted?
=> true
>> c.password.cipher
=> #<EncryptedStrings::SymmetricCipher:0x000001016b1c08 @algorithm="DES-EDE3-CBC", @password="key">
>> c.password.cipher.password
=> "key"
>> c.password == 'this is a secret'
=> true

Asymmetric encryption

Set up Asymmetric encrypted field in models like this:

class Credential
  include Mongoid::Document
  include Mongoid::Encryptor
  field :password
  encrypts :password, :mode => :asymmetric,
    :private_key_file => '/path/to/private_key',
    :public_key_file => '/path/to/public_key'
end

>> c = Credential.new
=> #<Credential _id: 4d638ceab2de3cc3c1000001, password: nil>
>> c.password = 'this is a secret'
=> "this is a secret"
>> c.password.encrypted?
=> false
>> c.save
=> true

>> c.password
=> "ha/2EacZTmvAIHOSjFEshM+9UHUItB/wGKJ5ftClQDllA9SOBJJazTlsMS9m\nPd5W3goZbY9V2dDdNo4NgQ0e8VsG0dpcvOIrua/ye+jX3e+0ocevcnOH9PL9\n8C5P8caOD/sKlKLTI0Dr1v/6d/f0Q4UuPQyTh3d4aEWyagypWyQ=\n"
>> c.password.encrypted?
=> true
>> c.password == 'this is a secret'
=> true

You can generate keypair like this:

$ openssl genrsa -des3 -out private 1024  # generate private key
$ openssl rsa -in private -pubout -out public  # generate public key
$ mv private private.bak
$ openssl rsa -in private.bak -out private  # remove passphrase from private key
$ ls -l
-rw-r--r--   1 juno  staff   887  2 22 19:20 private
-rw-r--r--   1 juno  staff   963  2 22 19:19 private.bak
-rw-r--r--   1 juno  staff   272  2 22 19:19 public

Questions, Feedback

github/juno or @junya.

References

Copyright

(c) 2011 Junya Ogura. See LICENSE.txt for further details.

mongoid-encryptor's People

Contributors

juno avatar iamnader avatar icebreaker avatar

Watchers

 avatar  avatar Matt Huggins avatar Ian Smith avatar Cory Leistikow avatar Chris Willmore avatar Eric Pauley avatar James Cloos avatar  avatar Justin Hundley avatar Jace Harmer avatar Derek Lee avatar alex avatar Mark Peck avatar Josh Higgins avatar Teresa Tarn avatar Andrew Meier avatar Erik Mingo avatar Dave avatar Eric Brohman avatar  avatar Baxter Grace avatar Ian (kapost) avatar  avatar Eric Carlson avatar  avatar  avatar Charlie PS Corrigan avatar  avatar

Forkers

gdiphil

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.