hashed_attr 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: 6ff55ce0fc59516e780c39b16505ebebeab4da98
4
+ data.tar.gz: 4a9ddd481f3f841b2e906ce3d3a1f8d9a32362c9
5
+ SHA512:
6
+ metadata.gz: ec628ca808b4e7743d90756d2f1b621a9efa73225b3c1fdfc06083d53d5a87356d03e17a716f52caeb862bf5138be9e75f9ec088b15eb840db8c70926ed32855
7
+ data.tar.gz: 7e96ccc49c9b9ca4ec47d75635f80e7fcf2921c72af0a8e3744f2bf2f49be79b39faac2f4afdf22f3cc4755dd64fc67d45349136d44f9010b8df32a7d42ec1f6
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/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - '2.0'
6
+ - '2.1'
7
+ - '2.2'
8
+ addons:
9
+ code_climate:
10
+ repo_token:
11
+ secure: "4vr2TYLMkquEL3/7mnnmE9diuQ98ug3iNw3eXB69JCC5OIW8DGRlL20/44+dh6xEImkkvp6bKQQrikvYTMpqrDi4MeAYFHzAWw2p7Oc+A5bGERamByTakM0wyl9ypnwHGzpleBoHShk7TP/Dpl6w25ME1QPb67qF8uPeeIQJQzE="
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## v0.1.0
4
+
5
+ - Initial release.
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nando Vieira
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,146 @@
1
+ # EncryptAttr
2
+
3
+ [![Build Status](https://travis-ci.org/fnando/encrypt_attr.svg)](https://travis-ci.org/fnando/encrypt_attr)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/encrypt_attr/badges/gpa.svg)](https://codeclimate.com/github/fnando/encrypt_attr)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/encrypt_attr/badges/coverage.svg)](https://codeclimate.com/github/fnando/encrypt_attr)
6
+
7
+ Encrypt attributes using AES-256-CBC (or your custom encryption strategy). Works with and without ActiveRecord.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'encrypt_attr'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install encrypt_attr
24
+
25
+ ## Usage
26
+
27
+ The most basic usage is including the `EncryptAttr` module.
28
+
29
+ ```ruby
30
+ class User
31
+ include EncryptAttr
32
+ attr_accessor :encrypted_api_key
33
+ encrypt_attr :api_key
34
+ end
35
+ ```
36
+
37
+ The `encrypt_attr` method has some aliases, so you can also use any of these:
38
+
39
+ - `attr_encrypt`
40
+ - `attr_encrypted`
41
+ - `attr_vault`
42
+ - `encrypt_attr`
43
+ - `encrypt_attribute`
44
+ - `encrypted_attr`
45
+ - `encrypted_attribute`
46
+
47
+ This assumes that you have a `encrypted_api_key` attribute. By default, the value is encrypted using a global secret token. You can set a custom token by setting `EncryptAttr.secret_token`; you have to use 100 characters or more (e.g. `$ openssl rand -hex 50`).
48
+
49
+ ```ruby
50
+ EncryptAttr.secret_token = 'abc123'
51
+ ```
52
+
53
+ You can also set the secret token per attribute basis.
54
+
55
+ ```ruby
56
+ class User
57
+ include EncryptAttr
58
+ attr_accessor :encrypted_api_key
59
+ encrypt_attr :api_key, secret_token: USER_SECRET_TOKEN
60
+ end
61
+ ```
62
+
63
+ To access the decrypted value, just use the method with the same name.
64
+
65
+ ```ruby
66
+ user = User.new
67
+ user.api_key = 'abc123'
68
+ user.api_key #=> abc123
69
+ user.encrypted_api_key #=> UcnhbnAl1Rmvt1mkG0m1FA...
70
+
71
+ user.api_key = 'newsecret'
72
+ user.api_key #=> newsecret
73
+ user.encrypted_api_key #=> JgH5dFGl8HnJNEloXZ6qSg...
74
+ ```
75
+
76
+ You encrypt multiple attributes at once.
77
+
78
+ ```ruby
79
+ class User
80
+ include EncryptAttr
81
+ attr_accessor :encrypted_api_key
82
+ encrypt_attr :api_key, :api_client_id
83
+ end
84
+ ```
85
+
86
+ ### ActiveRecord integration
87
+
88
+ You can also use encrypted attributes with ActiveRecord. If ActiveRecord is available, it's included automatically. You can also manually include `EncryptAttr::Base` or require `encrypt_attr/activerecord`.
89
+
90
+ ```ruby
91
+ class User < ActiveRecord::Base
92
+ encrypt_attr :api_key
93
+ end
94
+ ```
95
+
96
+ The usage is pretty much the same, and you can set a secret for each attribute. The example above will require a column name `encrypted_api_key`.
97
+
98
+ ```ruby
99
+ class AddEncryptedApiKeyToUsers < ActiveRecord::Base
100
+ def change
101
+ add_column :users, :encrypted_api_key, :text, null: false
102
+ end
103
+ end
104
+ ```
105
+
106
+ ### Using a custom encryption
107
+
108
+ You can define your encryption engine by defining an object that responds to `encrypt(secret_token, value)` and `decrypt(secret_token, value)`. Here's an example:
109
+
110
+ ```ruby
111
+ module ReverseEncryptor
112
+ def self.encrypt(secret_token, value)
113
+ value.to_s.reverse
114
+ end
115
+
116
+ def self.decrypt(secret_token, value)
117
+ value.to_s.reverse
118
+ end
119
+ end
120
+
121
+ EncryptAttr.encryptor = ReverseEncryptor
122
+
123
+ class User
124
+ include EncryptAttr
125
+ attr_accessor :encrypted_api_key
126
+ attr_encrypted :api_key
127
+ end
128
+
129
+ user = User.new
130
+ user.api_key = 'API_KEY'
131
+ user.encrypted_api_key #=> 'YEK_IPA'
132
+ ```
133
+
134
+ ## Development
135
+
136
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
137
+
138
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
139
+
140
+ ## Contributing
141
+
142
+ 1. Fork it ( https://github.com/fnando/encrypt_attr/fork )
143
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
144
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
145
+ 4. Push to the branch (`git push origin my-new-feature`)
146
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :spec
11
+
data/hash_attr.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ require './lib/hash_attr/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'hashed_attr'
5
+ spec.version = HashAttr::VERSION
6
+ spec.authors = ['Dann Luciano']
7
+ spec.email = ['dannluciano@gmail.com']
8
+ spec.summary = 'Hash attributes using SHA512 (or your custom hash strategy). Works with and without ActiveRecord.'
9
+ spec.description = spec.summary
10
+ spec.homepage = 'http://rubygems.org/gems/hashed_attr'
11
+ spec.license = 'MIT'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.bindir = 'exe'
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_development_dependency 'bundler'
19
+ spec.add_development_dependency 'rake'
20
+ spec.add_development_dependency 'minitest'
21
+ spec.add_development_dependency 'minitest-reporters'
22
+ spec.add_development_dependency 'sqlite3'
23
+ spec.add_development_dependency 'activerecord'
24
+ spec.add_development_dependency 'codeclimate-test-reporter'
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'hash_attr'
2
+ ActiveRecord::Base.send :include, HashAttr::Base
@@ -0,0 +1,39 @@
1
+ module HashAttr
2
+ module Base
3
+ def self.included(target)
4
+ target.extend(ClassMethods)
5
+ end
6
+
7
+ class << self
8
+ # Define the object that will encrypt/decrypt values.
9
+ # By default, it's HashAttr::Encryptor
10
+ attr_accessor :encryptor
11
+ end
12
+
13
+ # Set initial encryptor engine.
14
+ self.encryptor = Encryptor
15
+
16
+ module ClassMethods
17
+ def hash_attr(*args)
18
+
19
+ args.each do |attribute|
20
+ define_encrypted_attribute(attribute)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def define_encrypted_attribute(attribute)
27
+ define_method attribute do
28
+ instance_variable_get("@#{attribute}")
29
+ end
30
+
31
+ define_method "#{attribute}=" do |value|
32
+ instance_variable_set("@#{attribute}", value)
33
+ send("hashed_#{attribute}=", nil)
34
+ send("hashed_#{attribute}=", HashAttr.encryptor.encrypt(value)) if value
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'digest/sha2'
2
+ require 'base64'
3
+
4
+ module HashAttr
5
+ class Encryptor
6
+ def self.encrypt(value)
7
+ new().encrypt(value)
8
+ end
9
+
10
+ def self.decrypt(value)
11
+ new().decrypt(value)
12
+ end
13
+
14
+ def encrypt(value)
15
+ Digest::SHA512.base64digest value
16
+ end
17
+
18
+ def decrypt(value)
19
+ value
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module HashAttr
2
+ VERSION = '0.1.0'
3
+ end
data/lib/hash_attr.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'forwardable'
2
+
3
+ module HashAttr
4
+ require 'hash_attr/version'
5
+ require 'hash_attr/encryptor'
6
+ require 'hash_attr/base'
7
+ require 'hash_attr/active_record' if defined?(ActiveRecord)
8
+
9
+ class << self
10
+ extend Forwardable
11
+ def_delegators Base, :encryptor, :encryptor=
12
+ end
13
+
14
+ def self.included(target)
15
+ target.send :include, Base
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashed_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dann Luciano
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-24 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
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: sqlite3
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: activerecord
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: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Hash attributes using SHA512 (or your custom hash strategy). Works with
112
+ and without ActiveRecord.
113
+ email:
114
+ - dannluciano@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - CHANGELOG.md
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - hash_attr.gemspec
128
+ - lib/hash_attr.rb
129
+ - lib/hash_attr/active_record.rb
130
+ - lib/hash_attr/base.rb
131
+ - lib/hash_attr/encryptor.rb
132
+ - lib/hash_attr/version.rb
133
+ homepage: http://rubygems.org/gems/hashed_attr
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Hash attributes using SHA512 (or your custom hash strategy). Works with and
157
+ without ActiveRecord.
158
+ test_files: []