acts_as_encrypted 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2008-07-28
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ rails/init.rb
6
+ rails/install.rb
7
+ lib/acts_as_encrypted.rb
8
+ uninstall.rb
9
+ bin/acts_as_encrypted
10
+
data/README.txt ADDED
@@ -0,0 +1,66 @@
1
+ = ActsAsEncrypted
2
+
3
+ * actsasencrypted.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ A quick and easy encryption wrapper. using the built in openssl functions.
8
+
9
+ Usable in String, Text, or Binary fields. (Encodes in Base 64 before saving)
10
+
11
+ == SYNOPSIS:
12
+
13
+ SecretAgent << ActiveRecord::Base
14
+ acts_as_encrypted
15
+ encrypt_fields :firstname, :lastname, :secret_agent_number
16
+ end
17
+
18
+ SuperWeapon << ActiveRecord::Base
19
+ acts_as_encrypted
20
+ encrypt_field :obvious_weakness
21
+ end
22
+
23
+ == REQUIREMENTS:
24
+
25
+ * Ruby Compiled --with-openssl
26
+
27
+ == INSTALL:
28
+
29
+ * sudo gem install acts_as_encrypted
30
+
31
+ In your application's config/environment:
32
+
33
+ config.gem "acts_as_encrypted"
34
+
35
+ Then, if you want to change the encryption password or type (and you should!):
36
+
37
+ config.after_initialize do
38
+ ActsAsEncrypted::DBCipherKey = 'Sha1 Some password here'
39
+ ActsAsEncrypted::DBCipherType = "One of the Ciphers from OpenSSL::Cipher.ciphers"
40
+ # The default cipher is aes-256-cbc
41
+ end
42
+
43
+ == LICENSE:
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2008 Alexander Bartlow
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/acts_as_encrypted.rb'
6
+
7
+ Hoe.new('acts_as_encrypted', ActsAsEncrypted::VERSION) do |p|
8
+ p.rubyforge_name = 'acts_as_encrypted'
9
+ p.developer 'Alexander Bartlow', 'bartlow.3@osu.edu'
10
+
11
+ end
12
+
13
+ # vim: syntax=Ruby
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ gem unpack acts_as_encrypted --target ./vendor/plugins/acts_as_encrypted
3
+ echo "acts_as_encrypted installed, be sure to define a password and a cipher
4
+ in vendor/plugins/acts_as_encrypted/config.rb"
@@ -0,0 +1,139 @@
1
+ require 'openssl'
2
+ require 'ostruct'
3
+ # ActsAsEncrypted
4
+ module ActsAsEncrypted
5
+ VERSION = '1.0.0'
6
+ DBCipherType='aes-256-cbc'
7
+ DBCipherKey='965f79fc27052324879b138b46df31e6f66c4d00'
8
+ module Acts
9
+ module Encrypted
10
+ # included is called from the ActiveRecord::Base
11
+ # when you inject this module
12
+
13
+ def self.included(base)
14
+ base.extend AddActsAsMethod
15
+ end
16
+
17
+ # this module stores the main function and the two modules for
18
+ # the instance and class functions
19
+ module AddActsAsMethod
20
+ def acts_as_encrypted(options = {})
21
+ # Here you can put additional association for the
22
+ # target class.
23
+ # belongs_to :role
24
+ # add class and instance methods
25
+ class_eval "include ActsAsEncrypted::Acts::Encrypted::InstanceMethods"
26
+ end
27
+ end
28
+
29
+ # Istance methods
30
+ module InstanceMethods
31
+ # doing this our target class
32
+ # acquires all the methods inside ClassMethods module
33
+ # as class methods.
34
+
35
+ def self.included(aClass)
36
+ aClass.extend ClassMethods
37
+ end
38
+
39
+ def field_is_encrypted?(field)
40
+ @encryption_initializer ||= OpenStruct.new
41
+ @encryption_initializer.send(field).nil?
42
+ end
43
+
44
+ def mark_as_encrypted(field)
45
+ @encryption_initializer.send(field + '=', nil)
46
+ end
47
+
48
+ def mark_as_decrypted(field)
49
+ @encryption_initializer.send(field + '=', true)
50
+ end
51
+
52
+ def decrypt_field(field)
53
+ mark_as_decrypted(field)
54
+ EncryptionWrapper.new(field).decrypt_field(self)
55
+ end
56
+
57
+ module ClassMethods
58
+ # Class methods
59
+ # Our encrypt_field function - when called sets up the callback hooks and the lazy decryption
60
+ def encrypt_fields(*args)
61
+ args.each do |fn|
62
+ before_save(EncryptionWrapper.new(fn))
63
+ after_save(EncryptionWrapper.new(fn))
64
+ fn = fn.to_s
65
+ # if the initializer says 'nil', then it's encrypted - since that's the default value
66
+ class_eval <<-HERE
67
+ # This is a custom accessor that
68
+ def #{fn}
69
+ if field_is_encrypted?(\"#{fn}\")
70
+ decrypt_field(\"#{fn}\")
71
+ else
72
+ #{fn}_safe
73
+ end
74
+ end
75
+ HERE
76
+
77
+ class_eval <<-THERE
78
+ def #{fn}_safe
79
+ read_attribute \"#{fn}\"
80
+ end
81
+ THERE
82
+ end
83
+ end # encrypt_fields
84
+
85
+ alias_method :encrypt_field, :encrypt_fields
86
+ end
87
+
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ # == Encryption Wrapper
94
+ # Use +EncryptionWrapper+ as a callback in a model to encrypt database fields.
95
+ # It is recomended that you don't use it directly, but instead use:
96
+ # acts_as_encrypted
97
+ # encrypt_field :field_one, :field_two, :field_three
98
+ class EncryptionWrapper
99
+ def initialize(attribute)
100
+ @attribute = attribute.to_sym
101
+ @attribute_safe = (attribute.to_s + '_safe').to_sym
102
+ @attr_setter = (attribute.to_s + '=').to_sym
103
+ end
104
+
105
+ def encrypt_field(record)
106
+ record.send(@attr_setter, encrypt(record.send(@attribute_safe)))
107
+ end
108
+
109
+ def decrypt_field(record)
110
+ record.send(@attr_setter, decrypt(record.send(@attribute_safe)))
111
+ end
112
+
113
+ alias_method :before_save, :encrypt_field
114
+ alias_method :after_save, :decrypt_field
115
+
116
+ private
117
+
118
+ def encrypt(value)
119
+ unless value.nil?
120
+ c = OpenSSL::Cipher.new(ActsAsEncrypted::DBCipherType).encrypt
121
+ c.pkcs5_keyivgen ActsAsEncrypted::DBCipherKey
122
+ c.update value
123
+ return ActiveSupport::Base64.encode64(c.final)
124
+ else
125
+ return nil
126
+ end
127
+ end
128
+
129
+ def decrypt(value)
130
+ unless value.nil?
131
+ c = OpenSSL::Cipher.new(ActsAsEncrypted::DBCipherType).decrypt
132
+ c.pkcs5_keyivgen ActsAsEncrypted::DBCipherKey
133
+ c.update(ActiveSupport::Base64.decode64(value))
134
+ return c.final
135
+ else
136
+ return nil
137
+ end
138
+ end
139
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'acts_as_encrypted'
2
+ ActiveRecord::Base.send(:include, ActsAsEncrypted::Acts::Encrypted)
data/rails/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'README'))
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_encrypted
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Bartlow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-29 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: A quick and easy encryption wrapper. using the built in openssl functions. Usable in String, Text, or Binary fields. (Encodes in Base 64 before saving)
26
+ email:
27
+ - bartlow.3@osu.edu
28
+ executables:
29
+ - acts_as_encrypted
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - rails/init.rb
42
+ - rails/install.rb
43
+ - lib/acts_as_encrypted.rb
44
+ - uninstall.rb
45
+ - bin/acts_as_encrypted
46
+ has_rdoc: true
47
+ homepage: actsasencrypted.rubyforge.org
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: acts_as_encrypted
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: A quick and easy encryption wrapper
73
+ test_files: []
74
+