acts_as_decryptable 0.1.0
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/LICENSE +19 -0
- data/README +1 -0
- data/Rakefile +12 -0
- data/init.rb +3 -0
- data/lib/active_record/acts/decryptable.rb +66 -0
- data/test/decryptable_test.rb +59 -0
- metadata +72 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Hwan-Joon Choi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Acts as decryptable
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc 'Default: run acts_as_decryptable unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
desc 'Test the acts_as_ordered_tree plugin.'
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.pattern = 'test/**/*_test.rb'
|
11
|
+
t.verbose = true
|
12
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module Decryptable
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
# class SomeModel < ActiveRecord::Base
|
9
|
+
# acts_as_decryptable :encrypt => [:name, :email], :key => "SOME_KEY"
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# some_model_object.name = "name"
|
13
|
+
# some_model_object.save
|
14
|
+
# some_model_object.name # => "encrypted name"
|
15
|
+
# some_model_object.decrypted_name # => "name"
|
16
|
+
module ClassMethods
|
17
|
+
def acts_as_decryptable(options)
|
18
|
+
cattr_accessor :encrypted_columns
|
19
|
+
configuration = { :encrypt => [] }
|
20
|
+
configuration.update(options) if options.is_a?(Hash)
|
21
|
+
raise "Valid key must be specified" unless configuration[:key]
|
22
|
+
|
23
|
+
configuration[:encrypt].each do |field_name|
|
24
|
+
class_eval <<-EOV
|
25
|
+
def #{field_name}=(value)
|
26
|
+
self[:#{field_name}] = SymmetricCrypto.encrypt(value, #{configuration[:key].to_json})
|
27
|
+
end
|
28
|
+
|
29
|
+
def decrypt_#{field_name}
|
30
|
+
SymmetricCrypto.decrypt(self[:#{field_name}], #{configuration[:key].to_json})
|
31
|
+
end
|
32
|
+
|
33
|
+
EOV
|
34
|
+
end
|
35
|
+
|
36
|
+
class_eval <<-EOV
|
37
|
+
include ActiveRecord::Acts::Decryptable::InstanceMethods
|
38
|
+
EOV
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module InstanceMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
class SymmetricCrypto
|
46
|
+
def self.encrypt(text, key)
|
47
|
+
aes(:encrypt, text, key)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.decrypt(crypted, key)
|
51
|
+
aes(:decrypt, crypted, key)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def self.aes(direction, message, key)
|
56
|
+
cipher = OpenSSL::Cipher.new('AES256')
|
57
|
+
direction == :encrypt ? cipher.encrypt : cipher.decrypt
|
58
|
+
cipher.key = key
|
59
|
+
cipher.update message
|
60
|
+
cipher.final
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'activerecord', '>= 1.15.4.7794'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
require "#{File.dirname(__FILE__)}/../init"
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
10
|
+
|
11
|
+
def setup_db
|
12
|
+
|
13
|
+
ActiveRecord::Schema.define(:version => 1) do
|
14
|
+
create_table :mixins do |t|
|
15
|
+
t.column :plaintext, :string
|
16
|
+
t.column :encrypted, :text
|
17
|
+
t.column :created_at, :datetime
|
18
|
+
t.column :updated_at, :datetime
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown_db
|
24
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
25
|
+
ActiveRecord::Base.connection.drop_table(table)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Mixin < ActiveRecord::Base
|
30
|
+
end
|
31
|
+
|
32
|
+
class DecryptableMixin < Mixin
|
33
|
+
acts_as_decryptable :encrypt => [ :encrypted ], :key => 'a' * 32
|
34
|
+
|
35
|
+
def self.table_name() "mixins" end
|
36
|
+
end
|
37
|
+
|
38
|
+
class DecryptTest < Test::Unit::TestCase
|
39
|
+
|
40
|
+
def setup
|
41
|
+
setup_db
|
42
|
+
DecryptableMixin.create!
|
43
|
+
end
|
44
|
+
|
45
|
+
def teardown
|
46
|
+
teardown_db
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_encrypt
|
50
|
+
decryptable = DecryptableMixin.first
|
51
|
+
decryptable.plaintext = "text"
|
52
|
+
decryptable.encrypted = "text"
|
53
|
+
decryptable.save!
|
54
|
+
|
55
|
+
assert_equal "text", decryptable.plaintext
|
56
|
+
assert_equal "text", decryptable.decrypt_encrypted
|
57
|
+
assert_not_equal "text", decryptable.encrypted
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_decryptable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- hc5duke
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-07 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Allow some rows to be encrypted/decrypted using symmetric encryption.
|
23
|
+
email: hc5duke@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- LICENSE
|
32
|
+
- README
|
33
|
+
- Rakefile
|
34
|
+
- init.rb
|
35
|
+
- lib/active_record/acts/decryptable.rb
|
36
|
+
- test/decryptable_test.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: https://www.github.com/hc5duke/acts_as_decryptable
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Allow some rows to be encrypted/decrypted using symmetric encryption
|
71
|
+
test_files: []
|
72
|
+
|