encrypt_column 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: 181ee10189a3bafd2eff6e34027ed0e9d306188e
4
+ data.tar.gz: af2b55e36335468a8fcb822c6eab00b0d50b8fd1
5
+ SHA512:
6
+ metadata.gz: d7c4639ddc4fdd40cc6e8b6aa6d7a91a71854aef372ba108e5ff4a32d5c85ed54cf8f567338d28193e0336c9fb79e3d70e9c152f9c46467de9ae68b9c02b061d
7
+ data.tar.gz: d95a83fae55a881a65645b8a40674f28a989ea4cc7488fe5b81d670a85c961298e55e637335760d92ff2777edb7705d3656016557e993c363dcbb4851083e835
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,49 @@
1
+ # Run rubocop --auto-gen-config to generate a to-do file with a list of issues
2
+ #
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.2
6
+ Include:
7
+ - Rakefile
8
+ - config.ru
9
+ - lib/**/*
10
+ Exclude:
11
+ - db/schema.rb
12
+ - db/**/*
13
+ - files/**/*
14
+ - test/**/*
15
+ - spec/**/*
16
+ - tmp/**/*
17
+ - bin/**/*
18
+ - !ruby/regexp /old_and_unused\.rb$/
19
+ - lib/templates/erb/scaffold/_form.html.erb
20
+ - scripts/interactive/**/*
21
+ # By default, the rails cops are not run. Override in project or home
22
+ # directory .rubocop.yml files, or by giving the -R/--rails option.
23
+ # RunRailsCops: true
24
+
25
+ # Disable documentation checking until a class needs to be documented
26
+ Documentation:
27
+ Enabled: false
28
+
29
+ Style/IndentationWidth:
30
+ Exclude:
31
+ - 'spec/**/*'
32
+
33
+ Metrics/AbcSize:
34
+ Max: 25
35
+
36
+ Metrics/CyclomaticComplexity:
37
+ Max: 10
38
+
39
+ Metrics/LineLength:
40
+ Max: 100
41
+
42
+ # Offense count: 1
43
+ # Configuration parameters: CountComments.
44
+ Metrics/MethodLength:
45
+ Max: 18
46
+
47
+ # Offense count: 1
48
+ Metrics/PerceivedComplexity:
49
+ Max: 9
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.12.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in encrypt_column.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dan Herman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # EncryptColumn
2
+
3
+ Encrypt any column with an optional hash (using searchable: true) or conditionally (if: Proc)
4
+ also has a failsafe (failsafe: true) feature to write to different db column in
5
+ the database, i.e. `<name>_ciphertext`. This prevents users from accidentally
6
+ commenting out the encrypt declaration and writing plaintext to the database.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'encrypt_column'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install encrypt_column
23
+
24
+ ## Usage
25
+
26
+ Add the following to the top of your model file
27
+ ```ruby
28
+ include EncryptColumn
29
+ ```
30
+
31
+ The gem uses the ENCRYPTION_KEY environment variable for encryption setup:
32
+ ```
33
+ ENV['ENCRYPTION_KEY'] = 'your_encryption_key_goes_here'
34
+ ```
35
+ and optionally a HASH_SALT if the searchable option is used.
36
+ ```
37
+ ENV['HASH_SALT'] = 'some_salt'
38
+ ```
39
+
40
+ Then specify the column to be encrypted as so (i.e. encrypt ssn column):
41
+ ```ruby
42
+ encrypt :ssn
43
+ ```
44
+
45
+ To add a `<Model>.with_<field_name>(<field_value>)` search method (using a hash column named `<column_name>_hash` or `ssn_hash`)
46
+ ```ruby
47
+ encrypt :ssn, searchable: true
48
+
49
+ Usage like so:
50
+
51
+ SecureTable.with_ssn('123456789')
52
+ ```
53
+
54
+ To use a failsafe column name to prevent accidental removal of encryption specify "failsafe: true". This will store the data in a column name `<column_name>_ciphertext` (i.e. `ssn_ciphertext`) but allow for read/write access by the original column name.
55
+ ```ruby
56
+ encrypt :ssn, failsafe: true
57
+ ```
58
+
59
+ To conditionally encrypt a column you can specify an if statement like so:
60
+ ```ruby
61
+ encrypt :card_number, if: -> (x) { x.card_type == 'credit' }
62
+ ```
63
+
64
+ Use all the options combined, like so:
65
+ ```ruby
66
+ encrypt :card_number, searchable: true, failsafe: true, if -> (x) { x.card_type == 'credit' }
67
+ ```
68
+
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/danlherman/encrypt_column.
77
+
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
82
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'encrypt_column'
5
+ require 'pry'
6
+
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'encrypt_column/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "encrypt_column"
8
+ spec.version = EncryptColumn::VERSION
9
+ spec.authors = ["Dan Herman"]
10
+ spec.email = ["dherman@intratechs.com"]
11
+
12
+ spec.summary = %q{Easily encrypt columns in your app conditionally and with hashed values for searching}
13
+ spec.homepage = "https://github.com/danlherman/encrypt_column"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rubocop"
27
+
28
+ spec.add_dependency "bcrypt", "~> 3.1"
29
+ spec.add_dependency "activerecord"
30
+ spec.add_dependency "activesupport"
31
+ end
@@ -0,0 +1,8 @@
1
+ class Decrypt
2
+ def self.cipher(ciphertext)
3
+ raise 'Encryption Key Config Missing' unless ENV['encryption_key'].present?
4
+ ActiveSupport::MessageEncryptor.new(ENV['encryption_key']).decrypt_and_verify(ciphertext)
5
+ rescue ActiveSupport::MessageVerifier::InvalidSignature
6
+ return ciphertext
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ class Encrypt
2
+ def self.text(plaintext)
3
+ return raise 'Missing Encryption Key Config' if ENV['encryption_key'].nil?
4
+ ActiveSupport::MessageEncryptor.new(ENV['encryption_key']).encrypt_and_sign(plaintext)
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module ClassMethods
2
+ # Encrypt any column with a hash (searchable: true) or conditionally(if: Proc)
3
+ # also has a failsafe (failsafe: true) feature to write to a different db column
4
+ # in the database, i.e. <name>_ciphertext. This prevents users from accidentally
5
+ # commenting out the encrypt declaration and reading/writing plaintext to the
6
+ # database.
7
+ def encrypt(name, options = {})
8
+ searchable = options[:searchable] || false
9
+ encrypt_cond = options[:if] || proc { true }
10
+ failsafe = options[:failsafe] || false
11
+ column = name
12
+ column = "#{name}_ciphertext" if failsafe
13
+ hash_column = "#{name}_hash"
14
+
15
+ # getter
16
+ define_method(name) do
17
+ return read_attribute(column) unless instance_eval(&encrypt_cond)
18
+ Decrypt.cipher(read_attribute(column))
19
+ end
20
+
21
+ # setter
22
+ define_method("#{name}=") do |value|
23
+ return write_attribute(column, value) unless instance_eval(&encrypt_cond)
24
+ write_attribute(column, Encrypt.text(value))
25
+ write_attribute(hash_column, Hashed.val(value)) if searchable
26
+ end
27
+
28
+ # search method when searchable specified
29
+ define_singleton_method("with_#{name}") do |value|
30
+ where(hash_column.to_sym => Hashed.val(value))
31
+ end if searchable
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require 'digest'
2
+
3
+ class Hashed
4
+ def self.val(plaintext)
5
+ return nil if plaintext.nil?
6
+ return raise 'Missing Hash Salt Config' if ENV['hash_salt'].nil?
7
+ Digest::SHA2.hexdigest(ENV['hash_salt'] + plaintext.to_s)
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EncryptColumn
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,7 @@
1
+ module EncryptColumn
2
+ require 'encrypt_column/version'
3
+ require 'encrypt_column/encrypt_column'
4
+ require 'encrypt_column/encrypt'
5
+ require 'encrypt_column/decrypt'
6
+ require 'encrypt_column/hashed'
7
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encrypt_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Herman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bcrypt
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - dherman@intratechs.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - encrypt_column.gemspec
157
+ - lib/encrypt_column.rb
158
+ - lib/encrypt_column/decrypt.rb
159
+ - lib/encrypt_column/encrypt.rb
160
+ - lib/encrypt_column/encrypt_column.rb
161
+ - lib/encrypt_column/hashed.rb
162
+ - lib/encrypt_column/version.rb
163
+ homepage: https://github.com/danlherman/encrypt_column
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.6.6
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Easily encrypt columns in your app conditionally and with hashed values for
187
+ searching
188
+ test_files: []