attr_encryptor 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/.rvmrc +47 -0
- data/MIT-LICENSE +20 -0
- data/README.md +291 -0
- data/Rakefile +22 -0
- data/attr_encryptor.gemspec +36 -0
- data/lib/attr_encryptor.rb +326 -0
- data/lib/attr_encryptor/adapters/active_record.rb +25 -0
- data/lib/attr_encryptor/adapters/data_mapper.rb +21 -0
- data/lib/attr_encryptor/adapters/sequel.rb +14 -0
- data/lib/attr_encryptor/version.rb +17 -0
- data/test/active_record_test.rb +98 -0
- data/test/attr_encrypted_test.rb +290 -0
- data/test/data_mapper_test.rb +52 -0
- data/test/debug_order.rb +41 -0
- data/test/sequel_test.rb +50 -0
- data/test/test_helper.rb +14 -0
- metadata +149 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
4
|
+
|
5
|
+
class Client
|
6
|
+
include DataMapper::Resource
|
7
|
+
|
8
|
+
property :id, Serial
|
9
|
+
property :encrypted_email, String
|
10
|
+
property :encrypted_credentials, Text
|
11
|
+
property :salt, String
|
12
|
+
|
13
|
+
attr_encrypted :email, :key => 'a secret key'
|
14
|
+
attr_encrypted :credentials, :key => Proc.new { |client| Encryptor.encrypt(:value => client.salt, :key => 'some private key') }, :marshal => true
|
15
|
+
|
16
|
+
def initialize(attrs = {})
|
17
|
+
super attrs
|
18
|
+
self.salt ||= Digest::SHA1.hexdigest((Time.now.to_i * rand(5)).to_s)
|
19
|
+
self.credentials ||= { :username => 'example', :password => 'test' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
DataMapper.auto_migrate!
|
24
|
+
|
25
|
+
class DataMapperTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def setup
|
28
|
+
Client.all.each(&:destroy)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_encrypt_email
|
32
|
+
@client = Client.new :email => 'test@example.com'
|
33
|
+
assert @client.save
|
34
|
+
assert_not_nil @client.encrypted_email
|
35
|
+
assert_not_equal @client.email, @client.encrypted_email
|
36
|
+
assert_equal @client.email, Client.first.email
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_marshal_and_encrypt_credentials
|
40
|
+
@client = Client.new
|
41
|
+
assert @client.save
|
42
|
+
assert_not_nil @client.encrypted_credentials
|
43
|
+
assert_not_equal @client.credentials, @client.encrypted_credentials
|
44
|
+
assert_equal @client.credentials, Client.first.credentials
|
45
|
+
assert Client.first.credentials.is_a?(Hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_encode_by_default
|
49
|
+
assert Client.attr_encrypted_options[:encode]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/test/debug_order.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'digest/sha2'
|
2
|
+
require 'rubygems'
|
3
|
+
gem 'activerecord', ENV['ACTIVE_RECORD_VERSION'] if ENV['ACTIVE_RECORD_VERSION']
|
4
|
+
require 'active_record'
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$:.unshift(File.dirname(__FILE__))
|
7
|
+
require 'attr_encrypted'
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
10
|
+
|
11
|
+
def create_tables
|
12
|
+
silence_stream(STDOUT) do
|
13
|
+
ActiveRecord::Schema.define(:version => 1) do
|
14
|
+
create_table :accounts do |t|
|
15
|
+
t.string :encrypted_password
|
16
|
+
t.string :encrypted_password_iv
|
17
|
+
t.string :encrypted_password_salt
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# The table needs to exist before defining the class
|
24
|
+
create_tables
|
25
|
+
require 'ruby-debug'
|
26
|
+
require 'pry'
|
27
|
+
class Account < ActiveRecord::Base
|
28
|
+
attr_accessor :key
|
29
|
+
attr_encrypted :password, :key => Proc.new {|account| binding.pry}
|
30
|
+
end
|
31
|
+
|
32
|
+
def setup
|
33
|
+
ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
|
34
|
+
create_tables
|
35
|
+
end
|
36
|
+
setup
|
37
|
+
require 'ruby-debug'
|
38
|
+
binding.pry
|
39
|
+
|
40
|
+
Account.create!(:password => "password" , :key => "secret")
|
41
|
+
|
data/test/sequel_test.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
DB = Sequel.sqlite
|
4
|
+
|
5
|
+
DB.create_table :humans do
|
6
|
+
primary_key :id
|
7
|
+
column :encrypted_email, :string
|
8
|
+
column :password, :string
|
9
|
+
column :encrypted_credentials, :string
|
10
|
+
column :salt, :string
|
11
|
+
end
|
12
|
+
|
13
|
+
class Human < Sequel::Model(:humans)
|
14
|
+
attr_encrypted :email, :key => 'a secret key'
|
15
|
+
attr_encrypted :credentials, :key => Proc.new { |human| Encryptor.encrypt(:value => human.salt, :key => 'some private key') }, :marshal => true
|
16
|
+
|
17
|
+
def after_initialize(attrs = {})
|
18
|
+
self.salt ||= Digest::SHA1.hexdigest((Time.now.to_i * rand(5)).to_s)
|
19
|
+
self.credentials ||= { :username => 'example', :password => 'test' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class SequelTest < Test::Unit::TestCase
|
24
|
+
|
25
|
+
def setup
|
26
|
+
Human.all.each(&:destroy)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_encrypt_email
|
30
|
+
@human = Human.new :email => 'test@example.com'
|
31
|
+
assert @human.save
|
32
|
+
assert_not_nil @human.encrypted_email
|
33
|
+
assert_not_equal @human.email, @human.encrypted_email
|
34
|
+
assert_equal @human.email, Human.first.email
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_marshal_and_encrypt_credentials
|
38
|
+
@human = Human.new
|
39
|
+
assert @human.save
|
40
|
+
assert_not_nil @human.encrypted_credentials
|
41
|
+
assert_not_equal @human.credentials, @human.encrypted_credentials
|
42
|
+
assert_equal @human.credentials, Human.first.credentials
|
43
|
+
assert Human.first.credentials.is_a?(Hash)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_encode_by_default
|
47
|
+
assert Human.attr_encrypted_options[:encode]
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'digest/sha2'
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'activerecord', ENV['ACTIVE_RECORD_VERSION'] if ENV['ACTIVE_RECORD_VERSION']
|
5
|
+
require 'active_record'
|
6
|
+
require 'data_mapper'
|
7
|
+
require 'sequel'
|
8
|
+
require 'mocha'
|
9
|
+
|
10
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
+
$:.unshift(File.dirname(__FILE__))
|
12
|
+
require 'attr_encryptor'
|
13
|
+
|
14
|
+
puts "\nTesting with ActiveRecord #{ActiveRecord::VERSION::STRING rescue ENV['ACTIVE_RECORD_VERSION']}"
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_encryptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Palacio
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: encryptor2
|
16
|
+
requirement: &70172209395920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70172209395920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70172209395400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70172209395400
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: datamapper
|
38
|
+
requirement: &70172209395020 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70172209395020
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mocha
|
49
|
+
requirement: &70172203559020 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70172203559020
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: sequel
|
60
|
+
requirement: &70172203558600 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70172203558600
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dm-sqlite-adapter
|
71
|
+
requirement: &70172203558180 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70172203558180
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sqlite3
|
82
|
+
requirement: &70172203557760 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70172203557760
|
91
|
+
description: Generates attr_accessors that encrypt and decrypt attributes transparently
|
92
|
+
email: danpal@gmail.com
|
93
|
+
executables: []
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files: []
|
96
|
+
files:
|
97
|
+
- .gitignore
|
98
|
+
- .rvmrc
|
99
|
+
- MIT-LICENSE
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- attr_encryptor.gemspec
|
103
|
+
- lib/attr_encryptor.rb
|
104
|
+
- lib/attr_encryptor/adapters/active_record.rb
|
105
|
+
- lib/attr_encryptor/adapters/data_mapper.rb
|
106
|
+
- lib/attr_encryptor/adapters/sequel.rb
|
107
|
+
- lib/attr_encryptor/version.rb
|
108
|
+
- test/active_record_test.rb
|
109
|
+
- test/attr_encrypted_test.rb
|
110
|
+
- test/data_mapper_test.rb
|
111
|
+
- test/debug_order.rb
|
112
|
+
- test/sequel_test.rb
|
113
|
+
- test/test_helper.rb
|
114
|
+
homepage: http://github.com/danpal/attr_encrypted
|
115
|
+
licenses: []
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options:
|
118
|
+
- --line-numbers
|
119
|
+
- --inline-source
|
120
|
+
- --main
|
121
|
+
- README.rdoc
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ! '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 1.8.6
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Encrypt and decrypt attributes
|
142
|
+
test_files:
|
143
|
+
- test/active_record_test.rb
|
144
|
+
- test/attr_encrypted_test.rb
|
145
|
+
- test/data_mapper_test.rb
|
146
|
+
- test/debug_order.rb
|
147
|
+
- test/sequel_test.rb
|
148
|
+
- test/test_helper.rb
|
149
|
+
has_rdoc: false
|