encrypted-attributes 0.0.1 → 0.0.2
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/README.md +21 -1
- data/lib/encrypted_attributes/ar_23.rb +42 -0
- data/lib/encrypted_attributes/ar_32.rb +35 -0
- data/lib/encrypted_attributes/railtie.rb +11 -0
- metadata +4 -1
data/README.md
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
[](http://travis-ci.org/clowder/encrypted-attributes)
|
2
2
|
|
3
|
-
#
|
3
|
+
# EncryptedAttributes
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
### Rails 2.3.x
|
8
|
+
|
9
|
+
Create an initializer that will configure the EncryptedAttributes module with your Key and IV.
|
10
|
+
|
11
|
+
```
|
12
|
+
EncryptedAttributes.setup(:key => 'YOUR RANDOM KEY', :iv => 'YOUR RANDOM INITIALIZATION VECTOR')
|
13
|
+
```
|
14
|
+
|
15
|
+
Encrypt your columns like.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Deployment < ActiveRecord::Base
|
19
|
+
serialize :secret_config, Hash
|
20
|
+
encrypt :secret_config
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
4
24
|
|
5
25
|
## Contributing
|
6
26
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module EncryptedAttributes
|
2
|
+
module AR23
|
3
|
+
def encrypted_attributes
|
4
|
+
@encrypted_attributes
|
5
|
+
end
|
6
|
+
|
7
|
+
def encrypt(column_name)
|
8
|
+
@encrypted_attributes = [] unless defined? @encrypted_columns
|
9
|
+
@encrypted_attributes << column_name.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def define_attribute_methods(*args)
|
13
|
+
self.encrypted_attributes.each do |column|
|
14
|
+
define_read_method("read_encrypted_#{column}", column, columns_hash[column])
|
15
|
+
evaluate_attribute_method "write_encrypted_#{column}=", "def write_encrypted_#{column}=(new_value);write_attribute('#{column}', new_value);end"
|
16
|
+
|
17
|
+
define_method(column) do
|
18
|
+
encrypted_value = self.send("read_encrypted_#{column}")
|
19
|
+
|
20
|
+
if encrypted_value
|
21
|
+
object_from_yaml(EncryptedAttributes.decrypt(encrypted_value)).freeze
|
22
|
+
else
|
23
|
+
encrypted_value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method("#{column}=") do |new_value|
|
28
|
+
if new_value
|
29
|
+
if self.class.serialized_attributes.has_key?(column)
|
30
|
+
new_value = new_value.to_yaml
|
31
|
+
end
|
32
|
+
new_value = EncryptedAttributes.encrypt(new_value)
|
33
|
+
end
|
34
|
+
|
35
|
+
self.send("write_encrypted_#{column}=", new_value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module EncryptedAttributes
|
2
|
+
module AR32
|
3
|
+
def encrypted_attributes
|
4
|
+
@encrypted_attributes
|
5
|
+
end
|
6
|
+
|
7
|
+
def encrypt(column_name)
|
8
|
+
@encrypted_attributes = [] unless defined? @encrypted_columns
|
9
|
+
@encrypted_attributes << column_name.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def define_attribute_methods(*args)
|
13
|
+
self.encrypted_attributes.each do |column|
|
14
|
+
define_method(column) do
|
15
|
+
coder = self.class.serialized_attributes[column]
|
16
|
+
value = EncryptedAttributes.decrypt(read_attribute(column))
|
17
|
+
|
18
|
+
value = coder ? coder.load(value) : value
|
19
|
+
value.freeze
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method("#{ column }=".to_sym) do |value|
|
23
|
+
coder = self.class.serialized_attributes[column]
|
24
|
+
|
25
|
+
value = coder ? coder.dump(value) : value
|
26
|
+
encrypted_data = EncryptedAttributes.encrypt(value)
|
27
|
+
|
28
|
+
write_attribute(column, encrypted_data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module EncryptedAttributes
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
config.encrypted_attributes = ActiveSupport::OrderedOptions.new
|
4
|
+
config.encrypted_attributes.key = nil
|
5
|
+
config.encrypted_attributes.iv = nil
|
6
|
+
|
7
|
+
initializer :encrypted_attributes do |app|
|
8
|
+
EncryptedAttributes.setup(:key => app.config.encrypted_attributes.key, :iv => app.config.encrypted_attributes.iv)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encrypted-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -68,6 +68,9 @@ extensions: []
|
|
68
68
|
extra_rdoc_files: []
|
69
69
|
files:
|
70
70
|
- lib/encrypted-attributes.rb
|
71
|
+
- lib/encrypted_attributes/ar_23.rb
|
72
|
+
- lib/encrypted_attributes/ar_32.rb
|
73
|
+
- lib/encrypted_attributes/railtie.rb
|
71
74
|
- lib/encrypted_attributes.rb
|
72
75
|
- lib/simple_aes.rb
|
73
76
|
- spec/encrypted_attributes_spec.rb
|