encryption_accessor 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/Manifest +5 -3
- data/{README → README.rdoc} +0 -0
- data/Rakefile +1 -1
- data/encryption_accessor.gemspec +4 -4
- data/lib/encryption_accessor.rb +4 -57
- data/lib/encryption_accessor/accessor_methods.rb +55 -0
- data/lib/encryption_accessor/aes_crypt.rb +53 -0
- metadata +12 -10
- data/lib/aes_crypt.rb +0 -51
data/Manifest
CHANGED
data/{README → README.rdoc}
RENAMED
File without changes
|
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require 'echoe'
|
|
3
3
|
|
4
4
|
# PACKAGING ============================================================
|
5
5
|
|
6
|
-
Echoe.new('encryption_accessor', '0.0.
|
6
|
+
Echoe.new('encryption_accessor', '0.0.2') do |p|
|
7
7
|
p.description = "Accessor's to encrypt/decrypt fields when data is stored encrypted on DB"
|
8
8
|
p.url = 'https://github.com/marklazz/encryption_accessor'
|
9
9
|
p.author = 'Marcelo Giorgi'
|
data/encryption_accessor.gemspec
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{encryption_accessor}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Marcelo Giorgi"]
|
9
9
|
s.date = %q{2011-02-18}
|
10
10
|
s.description = %q{Accessor's to encrypt/decrypt fields when data is stored encrypted on DB}
|
11
11
|
s.email = %q{marklazz.uy@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["README", "lib/
|
13
|
-
s.files = ["README", "Rakefile", "
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/encryption_accessor.rb", "lib/encryption_accessor/accessor_methods.rb", "lib/encryption_accessor/aes_crypt.rb", "tasks/encryption_accessor.rake"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "encryption_accessor.gemspec", "lib/encryption_accessor.rb", "lib/encryption_accessor/accessor_methods.rb", "lib/encryption_accessor/aes_crypt.rb", "tasks/encryption_accessor.rake"]
|
14
14
|
s.homepage = %q{https://github.com/marklazz/encryption_accessor}
|
15
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Encryption_accessor", "--main", "README"]
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Encryption_accessor", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{encryption_accessor}
|
18
18
|
s.rubygems_version = %q{1.3.7}
|
data/lib/encryption_accessor.rb
CHANGED
@@ -1,61 +1,8 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname(__FILE__), 'encryption_accessor', 'accessor_methods')
|
3
|
+
require File.join(File.dirname(__FILE__), 'encryption_accessor', 'aes_crypt')
|
4
|
+
|
1
5
|
module EncryptionAccessor
|
2
6
|
|
3
7
|
VERSION = '0.0.1'
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
|
11
|
-
def encryption_accessor(*fields)
|
12
|
-
fields.each do |field|
|
13
|
-
force_implement_field_setter!(field)
|
14
|
-
|
15
|
-
define_method(:"#{field}_with_encryption=") do |value|
|
16
|
-
self.send(:"#{field}_without_encryption=", value)
|
17
|
-
write_attribute(:"#{field}", AESCrypt.encrypt(read_attribute(:"#{field}"))) unless value.nil?
|
18
|
-
end
|
19
|
-
|
20
|
-
self.send :alias_method_chain, :"#{field}=", :encryption
|
21
|
-
|
22
|
-
define_method(:"#{field}_decrypt") do
|
23
|
-
blob_value = self.read_attribute(:"#{field}")
|
24
|
-
return if blob_value.nil?
|
25
|
-
AESCrypt.decrypt(blob_value)
|
26
|
-
end
|
27
|
-
|
28
|
-
define_method(:"#{field}") do
|
29
|
-
self.send(:"#{field}_decrypt")
|
30
|
-
end
|
31
|
-
|
32
|
-
define_method(:"#{field}_encrypted") do
|
33
|
-
self.attributes["#{field}"]
|
34
|
-
end
|
35
|
-
define_method(:"#{field}") do
|
36
|
-
self.send(:"#{field}_decrypt")
|
37
|
-
end
|
38
|
-
define_method(:"#{field}_encrypt!") do
|
39
|
-
self.send(:"#{field}_with_encryption=", self.attributes["#{field}"])
|
40
|
-
end
|
41
|
-
define_method(:"#{field}_decrypt!") do
|
42
|
-
self.send(:"#{field}=", self.send(:"#{field}_decrypt"))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def force_implement_field_setter!(field)
|
50
|
-
new.send("#{field}=", nil)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
module ActiveRecord
|
56
|
-
class Base
|
57
|
-
class << self
|
58
|
-
include EncryptionAccessor
|
59
|
-
end
|
60
|
-
end
|
61
8
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module EncryptionAccessor
|
2
|
+
|
3
|
+
module AccessorMethods
|
4
|
+
|
5
|
+
def encryption_accessor(*fields)
|
6
|
+
fields.each do |field|
|
7
|
+
force_implement_field_setter!(field)
|
8
|
+
|
9
|
+
define_method(:"#{field}_with_encryption=") do |value|
|
10
|
+
self.send(:"#{field}_without_encryption=", value)
|
11
|
+
write_attribute(:"#{field}", AESCrypt.encrypt(read_attribute(:"#{field}"))) unless value.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
self.send :alias_method_chain, :"#{field}=", :encryption
|
15
|
+
|
16
|
+
define_method(:"#{field}_decrypt") do
|
17
|
+
blob_value = self.read_attribute(:"#{field}")
|
18
|
+
return if blob_value.nil?
|
19
|
+
AESCrypt.decrypt(blob_value)
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method(:"#{field}") do
|
23
|
+
self.send(:"#{field}_decrypt")
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{field}_encrypted") do
|
27
|
+
self.attributes["#{field}"]
|
28
|
+
end
|
29
|
+
define_method(:"#{field}") do
|
30
|
+
self.send(:"#{field}_decrypt")
|
31
|
+
end
|
32
|
+
define_method(:"#{field}_encrypt!") do
|
33
|
+
self.send(:"#{field}_with_encryption=", self.attributes["#{field}"])
|
34
|
+
end
|
35
|
+
define_method(:"#{field}_decrypt!") do
|
36
|
+
self.send(:"#{field}=", self.send(:"#{field}_decrypt"))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def force_implement_field_setter!(field)
|
44
|
+
new.send("#{field}=", nil)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module ActiveRecord
|
50
|
+
class Base
|
51
|
+
class << self
|
52
|
+
include EncryptionAccessor::AccessorMethods
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'openssl'
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module EncryptionAccessor
|
6
|
+
|
7
|
+
module AESCrypt
|
8
|
+
extend self
|
9
|
+
|
10
|
+
ALGORITHM = "AES-256-CBC"
|
11
|
+
SECRET = "add-your-way-eliasonmedia-02-10-2011"
|
12
|
+
ENCODE_DIRECTIVE = 'H*'
|
13
|
+
|
14
|
+
# ALGORITHM = "AES-256-ECB"
|
15
|
+
# Decrypts a block of data (encrypted_data) given an encryption key
|
16
|
+
# and an initialization vector (iv). Keys, iv's, and the data
|
17
|
+
# returned are all binary strings. Cipher_type should be
|
18
|
+
# "AES-256-CBC", "AES-256-ECB", or any of the cipher types
|
19
|
+
# supported by OpenSSL. Pass nil for the iv if the encryption type
|
20
|
+
# doesn't use iv's (like ECB).
|
21
|
+
#:return: => String
|
22
|
+
#:arg: encrypted_data => String
|
23
|
+
#:arg: key => String
|
24
|
+
#:arg: iv => String
|
25
|
+
#:arg: cipher_type => String
|
26
|
+
def decrypt(encrypted_data, key = SECRET, iv = nil, cipher_type = ALGORITHM)
|
27
|
+
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
|
28
|
+
aes.decrypt
|
29
|
+
aes.key = key
|
30
|
+
aes.iv = iv if iv != nil
|
31
|
+
aes.update([encrypted_data].pack(ENCODE_DIRECTIVE)) + aes.final
|
32
|
+
end
|
33
|
+
|
34
|
+
# Encrypts a block of data given an encryption key and an
|
35
|
+
# initialization vector (iv). Keys, iv's, and the data returned
|
36
|
+
# are all binary strings. Cipher_type should be "AES-256-CBC",
|
37
|
+
# "AES-256-ECB", or any of the cipher types supported by OpenSSL.
|
38
|
+
# Pass nil for the iv if the encryption type doesn't use iv's (like
|
39
|
+
# ECB).
|
40
|
+
#:return: => String
|
41
|
+
#:arg: data => String
|
42
|
+
#:arg: key => String
|
43
|
+
#:arg: iv => String
|
44
|
+
#:arg: cipher_type => String
|
45
|
+
def encrypt(data, key = SECRET, iv = nil, cipher_type = ALGORITHM)
|
46
|
+
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
|
47
|
+
aes.encrypt
|
48
|
+
aes.key = key
|
49
|
+
aes.iv = iv if iv != nil
|
50
|
+
(aes.update(data) + aes.final).unpack(ENCODE_DIRECTIVE)[0]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encryption_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marcelo Giorgi
|
@@ -39,18 +39,20 @@ executables: []
|
|
39
39
|
extensions: []
|
40
40
|
|
41
41
|
extra_rdoc_files:
|
42
|
-
- README
|
43
|
-
- lib/aes_crypt.rb
|
42
|
+
- README.rdoc
|
44
43
|
- lib/encryption_accessor.rb
|
44
|
+
- lib/encryption_accessor/accessor_methods.rb
|
45
|
+
- lib/encryption_accessor/aes_crypt.rb
|
45
46
|
- tasks/encryption_accessor.rake
|
46
47
|
files:
|
47
|
-
-
|
48
|
+
- Manifest
|
49
|
+
- README.rdoc
|
48
50
|
- Rakefile
|
49
|
-
-
|
51
|
+
- encryption_accessor.gemspec
|
50
52
|
- lib/encryption_accessor.rb
|
53
|
+
- lib/encryption_accessor/accessor_methods.rb
|
54
|
+
- lib/encryption_accessor/aes_crypt.rb
|
51
55
|
- tasks/encryption_accessor.rake
|
52
|
-
- Manifest
|
53
|
-
- encryption_accessor.gemspec
|
54
56
|
has_rdoc: true
|
55
57
|
homepage: https://github.com/marklazz/encryption_accessor
|
56
58
|
licenses: []
|
@@ -62,7 +64,7 @@ rdoc_options:
|
|
62
64
|
- --title
|
63
65
|
- Encryption_accessor
|
64
66
|
- --main
|
65
|
-
- README
|
67
|
+
- README.rdoc
|
66
68
|
require_paths:
|
67
69
|
- lib
|
68
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/lib/aes_crypt.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'openssl'
|
4
|
-
require 'digest/sha1'
|
5
|
-
|
6
|
-
module AESCrypt
|
7
|
-
extend self
|
8
|
-
|
9
|
-
ALGORITHM = "AES-256-CBC"
|
10
|
-
SECRET = "add-your-way-eliasonmedia-02-10-2011"
|
11
|
-
ENCODE_DIRECTIVE = 'H*'
|
12
|
-
|
13
|
-
# ALGORITHM = "AES-256-ECB"
|
14
|
-
# Decrypts a block of data (encrypted_data) given an encryption key
|
15
|
-
# and an initialization vector (iv). Keys, iv's, and the data
|
16
|
-
# returned are all binary strings. Cipher_type should be
|
17
|
-
# "AES-256-CBC", "AES-256-ECB", or any of the cipher types
|
18
|
-
# supported by OpenSSL. Pass nil for the iv if the encryption type
|
19
|
-
# doesn't use iv's (like ECB).
|
20
|
-
#:return: => String
|
21
|
-
#:arg: encrypted_data => String
|
22
|
-
#:arg: key => String
|
23
|
-
#:arg: iv => String
|
24
|
-
#:arg: cipher_type => String
|
25
|
-
def decrypt(encrypted_data, key = SECRET, iv = nil, cipher_type = ALGORITHM)
|
26
|
-
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
|
27
|
-
aes.decrypt
|
28
|
-
aes.key = key
|
29
|
-
aes.iv = iv if iv != nil
|
30
|
-
aes.update([encrypted_data].pack(ENCODE_DIRECTIVE)) + aes.final
|
31
|
-
end
|
32
|
-
|
33
|
-
# Encrypts a block of data given an encryption key and an
|
34
|
-
# initialization vector (iv). Keys, iv's, and the data returned
|
35
|
-
# are all binary strings. Cipher_type should be "AES-256-CBC",
|
36
|
-
# "AES-256-ECB", or any of the cipher types supported by OpenSSL.
|
37
|
-
# Pass nil for the iv if the encryption type doesn't use iv's (like
|
38
|
-
# ECB).
|
39
|
-
#:return: => String
|
40
|
-
#:arg: data => String
|
41
|
-
#:arg: key => String
|
42
|
-
#:arg: iv => String
|
43
|
-
#:arg: cipher_type => String
|
44
|
-
def encrypt(data, key = SECRET, iv = nil, cipher_type = ALGORITHM)
|
45
|
-
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
|
46
|
-
aes.encrypt
|
47
|
-
aes.key = key
|
48
|
-
aes.iv = iv if iv != nil
|
49
|
-
(aes.update(data) + aes.final).unpack(ENCODE_DIRECTIVE)[0]
|
50
|
-
end
|
51
|
-
end
|