the_encryptor 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/encryptor.gemspec +21 -0
- data/lib/encryptor/version.rb +3 -0
- data/lib/encryptor.rb +75 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Pham
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Encryptor
|
2
|
+
|
3
|
+
Encrypt sensitive data on your ActiveRecord models or plain ol' Ruby objects using AES.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'encryptor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install encryptor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Add this line within the class definition to any object, ActiveRecord model or not, you want to encrypt sensitive data:
|
22
|
+
|
23
|
+
$ include Encryptor
|
24
|
+
|
25
|
+
For any attribute you want to encrypt, add the line:
|
26
|
+
|
27
|
+
$ encrypted :attribute_name
|
28
|
+
|
29
|
+
This assumes you have two other attributes named encrypted_#{attribute_name} and #{attribute_name}_key.
|
30
|
+
|
31
|
+
It will then create #{attribute_name} and #{attribute_name}= methods that will transparently decrypt and encrypt the attribute.
|
32
|
+
|
33
|
+
Optionally, you can have two arguments that override the default name of the encrypted attribute and attribute key, like so:
|
34
|
+
|
35
|
+
$ encrypted :attribute_name, :optional_encrypted_attribute_name, :optional_attribute_key
|
36
|
+
|
37
|
+
## TODO
|
38
|
+
|
39
|
+
* Tests, tests, tests
|
40
|
+
* Refactor out usage of AES into a strategy
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/encryptor.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'encryptor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'the_encryptor'
|
8
|
+
gem.version = Encryptor::VERSION
|
9
|
+
gem.authors = ['David Pham']
|
10
|
+
gem.email = ['hello@khoi.co']
|
11
|
+
gem.summary = %q{Declaratively encrypt sensitive data}
|
12
|
+
gem.description = %q{Encrypt sensitive data on your ActiveRecord models or plain ol' Ruby objects}
|
13
|
+
gem.homepage = 'http://khoi.co'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency 'aes', '~> 0.5.0'
|
21
|
+
end
|
data/lib/encryptor.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'encryptor/version'
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'aes'
|
5
|
+
|
6
|
+
module Encryptor
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Preconditions
|
11
|
+
# => attribute_name - symbol, it is the name of the virtual attribute you want to create
|
12
|
+
# => (optional) encrypted_attribute_name - symbol, it is the name of the encrypted attribute
|
13
|
+
# => (optional) key_name - symbol, it is the name of the key
|
14
|
+
# Postconditions
|
15
|
+
# => #{attribute_name} instance method is created and handles decryption
|
16
|
+
# => #{attribute_name}= instance method is created and handles encryption
|
17
|
+
def encrypted(attribute_name, encrypted_attribute_name = nil, key_name = nil)
|
18
|
+
encrypted_attribute_name = encrypted_attribute_name.present? ? encrypted_attribute_name : "encrypted_#{attribute_name}"
|
19
|
+
key_name = key_name.present? ? key_name : "#{attribute_name}_key"
|
20
|
+
|
21
|
+
class_eval do
|
22
|
+
define_method :"#{attribute_name}" do
|
23
|
+
decrypt(self.send(encrypted_attribute_name), self.send(key_name))
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method :"#{attribute_name}=" do |value_to_encrypt|
|
27
|
+
encrypted_parts = encrypt(value_to_encrypt)
|
28
|
+
|
29
|
+
key = encrypted_parts[:key]
|
30
|
+
encrypted_attribute = encrypted_parts[:encrypted_attribute]
|
31
|
+
|
32
|
+
self.send("#{key_name}=", key)
|
33
|
+
|
34
|
+
self.send("#{encrypted_attribute_name}=", encrypted_attribute)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Preconditions
|
41
|
+
# => encrypted_attribute - string, it is the encrypted value
|
42
|
+
# => key, string, it is the key
|
43
|
+
# Postconditions
|
44
|
+
# => If both encrypted_attribute and key are present
|
45
|
+
# * Returns decrypted value - string
|
46
|
+
# => Else
|
47
|
+
# * Returns nil
|
48
|
+
def decrypt(encrypted_attribute, key)
|
49
|
+
if encrypted_attribute.present? and key.present?
|
50
|
+
AES.decrypt(encrypted_attribute, key)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Preconditions
|
55
|
+
# => attribute - string, it is the unencrypted value
|
56
|
+
# Postconditions
|
57
|
+
# => If attribute is present
|
58
|
+
# * Returns a hash with the following keys:
|
59
|
+
# - key - string
|
60
|
+
# - encrypted_attribute - string
|
61
|
+
# => Else
|
62
|
+
# * Returns a hash with the same keys as above but with nil values
|
63
|
+
def encrypt(attribute)
|
64
|
+
if attribute.present?
|
65
|
+
key = AES.key
|
66
|
+
|
67
|
+
encrypted_attribute = AES.encrypt(attribute, key)
|
68
|
+
end
|
69
|
+
|
70
|
+
{
|
71
|
+
key: key,
|
72
|
+
encrypted_attribute: encrypted_attribute
|
73
|
+
}
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: the_encryptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Pham
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: aes
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.5.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.5.0
|
30
|
+
description: Encrypt sensitive data on your ActiveRecord models or plain ol' Ruby
|
31
|
+
objects
|
32
|
+
email:
|
33
|
+
- hello@khoi.co
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- encryptor.gemspec
|
44
|
+
- lib/encryptor.rb
|
45
|
+
- lib/encryptor/version.rb
|
46
|
+
homepage: http://khoi.co
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Declaratively encrypt sensitive data
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|