secure_attribute 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 773123854481b3622f58a393706848701bf1c039
4
+ data.tar.gz: a71411f98c30243778348a2a5d9426eeab7015ec
5
+ SHA512:
6
+ metadata.gz: eca1061427d684176ea764c898ea740c1cd7ad231a1b4f7e1446567e80daa72e4098a2ee27efaac5042a7f3d56c99ddd9d860a22a9d2a2298ddcfd2bc86b9865
7
+ data.tar.gz: 9111e5f0544e859c926ca48849a57feaa13d284914d84e7063fe0d5b1e8903e641749bf5a318bf604126fb84b23bede70b7d6d2c9c9226228d3bf2fbdd11aa40
@@ -0,0 +1,3 @@
1
+ module SecureAttribute
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,67 @@
1
+ require "base64"
2
+ require "openssl"
3
+
4
+ module SecureAttribute
5
+ def self.included(model)
6
+ model.extend(ClassMethods)
7
+ end
8
+
9
+ def self.encipher(algorithm, data, key)
10
+ encrypted, iv = SecureAttribute.encrypt(algorithm, data, key)
11
+ SecureAttribute.pack(algorithm, iv, encrypted)
12
+ end
13
+
14
+ def self.decipher(data, key)
15
+ algorithm, iv, data = SecureAttribute.unpack(data)
16
+ SecureAttribute.decrypt(algorithm, data, key, iv)
17
+ end
18
+
19
+ def self.encrypt(algorithm, data, key)
20
+ cipher = OpenSSL::Cipher.new(algorithm).encrypt
21
+ cipher.key = key
22
+ iv = cipher.random_iv
23
+ [cipher.update(data) + cipher.final, iv]
24
+ end
25
+
26
+ def self.decrypt(algorithm, data, key, iv)
27
+ cipher = OpenSSL::Cipher.new(algorithm).decrypt
28
+ cipher.key, cipher.iv = key, iv
29
+ decrypted = cipher.update(data)
30
+ decrypted << cipher.final
31
+ end
32
+
33
+ def self.pack(algorithm, iv, encrypted)
34
+ ["", algorithm, Base64.strict_encode64(iv), Base64.strict_encode64(encrypted)].join("$")
35
+ end
36
+
37
+ def self.unpack(string)
38
+ _, algorithm, iv, data = string.split("$")
39
+ [algorithm, Base64.decode64(iv), Base64.decode64(data)]
40
+ end
41
+
42
+ def self.export_random_key_base64(algorithm)
43
+ Base64.strict_encode64(OpenSSL::Cipher.new(algorithm).random_key)
44
+ end
45
+
46
+ module ClassMethods
47
+ def attr_secure(name, options = {})
48
+ force_defining_active_record_attribute_accessors
49
+ attr_writer(name) unless respond_to?("#{name}=")
50
+ attr_reader(name) unless respond_to?(name)
51
+ alias_method(attr_reader = "#{name}_without_secure_attribute", "#{name}")
52
+ alias_method(attr_writer = "#{name}_without_secure_attribute=", "#{name}=")
53
+
54
+ define_method("#{name}=") do |data|
55
+ send(attr_writer, SecureAttribute.encipher(options[:algorithm], data, options[:key]))
56
+ end
57
+
58
+ define_method(name) do
59
+ SecureAttribute.decipher(send(attr_reader), options[:key])
60
+ end
61
+ end
62
+
63
+ def force_defining_active_record_attribute_accessors
64
+ define_attribute_methods if defined?(ActiveRecord::Base) && self < ActiveRecord::Base
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secure_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Bernard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Encrypt attributes of any Ruby object or ActiveRecord model.
14
+ email:
15
+ - alexis@bernard.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/secure_attribute.rb
21
+ - lib/secure_attribute/version.rb
22
+ homepage: https://github.com/BaseSecrete/secure_attribute
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Encrypt attributes of any Ruby object or ActiveRecord model.
46
+ test_files: []
47
+ has_rdoc: