active_record_encryption 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9369affd95f719bd5c13f2f1fbc5dd41a6a3c0f04f0b33da74d6834d6954f243
4
+ data.tar.gz: 1afd356f1754db7c05ced1749705bf9d56632597ee3380c1b34f96b455f631d4
5
+ SHA512:
6
+ metadata.gz: d25b721fa158761b2f43270d343386800c585e69c5b42db29b047daebefc0228d121ef7b0b8dc059049cc4a918f974b9bed8aec391e8039f3c42c9e75fc0bcef
7
+ data.tar.gz: 9182542eb22718dd4379afd6b757b37d994e99ccdc1c216d2c6a757991c76060bb51338164db351c5a56e1806d955761142c631fdfab54ae5ca4239ddbacaf13
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ /gemfiles/*.lock
16
+ /gemfiles/**/config
17
+ /gemfiles/.bundle
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,77 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/*'
4
+ - 'db/**/*'
5
+ - 'vendor/**/*'
6
+ - '**/Rakefile'
7
+ - '**/config.ru'
8
+ - 'node_modules/**/*'
9
+ TargetRubyVersion: 2.5
10
+ DisplayCopNames: true
11
+
12
+ Performance:
13
+ Enabled: false
14
+
15
+ Bundler:
16
+ Enabled: false
17
+
18
+ Naming:
19
+ Enabled: false
20
+
21
+ Metrics/BlockNesting:
22
+ Enabled: false
23
+
24
+ Metrics/ClassLength:
25
+ Enabled: false
26
+
27
+ Metrics/LineLength:
28
+ Enabled: false
29
+
30
+ Metrics/MethodLength:
31
+ Enabled: false
32
+
33
+ Metrics/BlockLength:
34
+ Enabled: false
35
+
36
+ Metrics/ModuleLength:
37
+ Enabled: false
38
+
39
+ Style/AsciiComments:
40
+ Enabled: false
41
+
42
+ Style/BlockDelimiters:
43
+ Exclude:
44
+ - 'spec/**/*'
45
+
46
+ Style/Documentation:
47
+ Enabled: false
48
+
49
+ Style/BlockDelimiters:
50
+ Enabled: false
51
+
52
+ Style/DoubleNegation:
53
+ Enabled: false
54
+
55
+ Style/GuardClause:
56
+ Enabled: false
57
+
58
+ Style/ClassAndModuleChildren:
59
+ Enabled: false
60
+
61
+ Style/SpecialGlobalVars:
62
+ Enabled: false
63
+
64
+ Style/NumericPredicate:
65
+ Enabled: false
66
+
67
+ Style/Lambda:
68
+ Enabled: false
69
+
70
+ Layout/AlignParameters:
71
+ EnforcedStyle: with_fixed_indentation
72
+
73
+ Layout/MultilineMethodCallIndentation:
74
+ EnforcedStyle: indented
75
+
76
+ Lint/AmbiguousRegexpLiteral:
77
+ Enabled: false
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.6
5
+ - 2.4.3
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.1
8
+ gemfile:
9
+ - gemfiles/5.0_stable.gemfile
10
+ - gemfiles/5.1_stable.gemfile
11
+ - gemfiles/5.2_stable.gemfile
12
+ script: bundle exec rspec
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise '5.0-stable' do
4
+ gem 'activerecord', '~> 5.0.0'
5
+ end
6
+
7
+ appraise '5.1-stable' do
8
+ gem 'activerecord', '~> 5.1.0'
9
+ end
10
+
11
+ appraise '5.2-stable' do
12
+ gem 'activerecord', git: 'https://github.com/rails/rails', branch: '5-2-stable'
13
+ end
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alpaca-tc@alpaca.tc. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in activerecord_encryption.gemspec
8
+ gemspec
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # Feel free to open issues for suggestions and improvements
8
+
9
+ # RSpec files
10
+ rspec = dsl.rspec
11
+ watch(rspec.spec_helper) { rspec.spec_dir }
12
+ watch(rspec.spec_support) { rspec.spec_dir }
13
+ watch(rspec.spec_files)
14
+
15
+ # Ruby files
16
+ ruby = dsl.ruby
17
+ dsl.watch_spec_files_for(ruby.lib_files)
18
+
19
+ # Rails files
20
+ rails = dsl.rails(view_extensions: %w[erb haml slim])
21
+ dsl.watch_spec_files_for(rails.app_files)
22
+ dsl.watch_spec_files_for(rails.views)
23
+
24
+ watch(rails.controllers) do |m|
25
+ [
26
+ rspec.spec.call("routing/#{m[1]}_routing"),
27
+ rspec.spec.call("controllers/#{m[1]}_controller"),
28
+ rspec.spec.call("acceptance/#{m[1]}")
29
+ ]
30
+ end
31
+
32
+ # Rails config changes
33
+ watch(rails.spec_helper) { rspec.spec_dir }
34
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
35
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
36
+
37
+ # Capybara features specs
38
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
39
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
40
+
41
+ # Turnip features and steps
42
+ watch(%r{^spec/acceptance/(.+)\.feature$})
43
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
44
+ Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
45
+ end
46
+ end
47
+
48
+ guard :rubocop, all_on_start: false, cli: ['--auto-correct'] do
49
+ watch(/.+\.rb$/)
50
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
51
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 alpaca-tc
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.
@@ -0,0 +1,95 @@
1
+ [![Build Status](https://travis-ci.org/alpaca-tc/active_record_encryption.png)](https://travis-ci.org/alpaca-tc/active_record_encryption)
2
+
3
+ # ActiveRecordEncryption
4
+
5
+ Decorate encrypted binary column with attribute that transparently encrypt and decrypt sensitive data.
6
+ It uses the ActiveRecord's Attribute API, and it is a simpler implementation than other gems.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'active_record_encryption'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Define serialized type in application
19
+
20
+ Here is an example of passing a type of object you want:
21
+
22
+ ```ruby
23
+ class Post < ActiveRecord::Base
24
+ include ActiveRecordEncryption::EncryptedAttribute
25
+
26
+ encrypted_attribute(:name, :string)
27
+ encrypted_attribute(:published_on, :date)
28
+ end
29
+
30
+ ---
31
+
32
+ $ post = Post.new(name: 'Author name', published_on: Date.current)
33
+ #=> #<Post:0x00007f92169ac158 id: nil, name: "Author name", published_on: Thu, 29 Mar 2018>
34
+ > post.save
35
+ DEBUG -- : (0.1ms) begin transaction
36
+ DEBUG -- : Post Create (0.1ms) INSERT INTO "posts" ("name", "published_on") VALUES (?, ?) [["name", "N\xFA\xDD\xC2\xB0&\xAE\x9A..."], ["published_on", "N\xFA\xDD\xX2\xB0&\xAE\x9A..."]]
37
+ DEBUG -- : (0.0ms) commit transaction
38
+ ```
39
+
40
+ ### `#encrypted_attribute` options
41
+
42
+ You can pass the same arguments as [ActiveRecord::Attributes.attribute](https://apidock.com/rails/ActiveRecord/Attributes/ClassMethods/attribute)
43
+
44
+ ```
45
+ class PointLog < ActiveRecord::Base
46
+ encrypted_attribute(:date, :date)
47
+ encrypted_attribute(:point, :integer, default: -> { Current.user.current_point })
48
+ encrypted_attribute(:price, Money.new)
49
+ end
50
+ ```
51
+
52
+ ### Migration
53
+
54
+ Create or modify the table that your model uses to add a column like the following:
55
+
56
+ **NOTE: Default limit of binary is too long. Please set limit of encrypted columns**
57
+
58
+ ```ruby
59
+ -ActiveRecord::Schema.define do
60
+ create_table(:posts, force: true) do |t|
61
+ t.binary :name, limit: 256
62
+ t.binary :point, limit: 256
63
+ t.binary :price, limit: 256
64
+ end
65
+ end
66
+ ```
67
+
68
+ *Tips* How to calculate limit of encrypted column? - It depends on algorithm of cipher.
69
+
70
+ ## Development
71
+
72
+ 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.
73
+
74
+ 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).
75
+
76
+ ### Run spec
77
+
78
+ ```
79
+ bundle exec appraisal install
80
+ bundle exec appraisal 5.0-stable rspec
81
+ bundle exec appraisal 5.1-stable rspec
82
+ bundle exec appraisal 5.2-stable rspec
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/active_record_encryption. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the ActiveRecord::Encryption project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alpaca-tc/active_record_encryption/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,37 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'active_record_encryption/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'active_record_encryption'
10
+ spec.version = ActiveRecordEncryption::VERSION
11
+ spec.authors = ['alpaca-tc']
12
+ spec.email = ['alpaca-tc@alpaca.tc']
13
+
14
+ spec.summary = 'Write a short summary, because RubyGems requires one.'
15
+ spec.description = 'Write a longer description or delete this line.'
16
+ spec.homepage = 'https://github.com/alpaca-tc/active_record_encryption'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activerecord', '>= 5.0'
26
+
27
+ spec.add_development_dependency 'appraisal'
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'guard-rspec'
30
+ spec.add_development_dependency 'guard-rubocop'
31
+ spec.add_development_dependency 'mysql2', '< 0.5.0'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'rake'
34
+ spec.add_development_dependency 'rspec'
35
+ spec.add_development_dependency 'rubocop'
36
+ spec.add_development_dependency 'sqlite3'
37
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ gem 'activerecord', '~> 5.0.0'
8
+
9
+ gemspec path: '../'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ gem 'activerecord', '~> 5.1.0'
8
+
9
+ gemspec path: '../'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ gem 'activerecord', git: 'https://github.com/rails/rails', branch: '5-2-stable'
8
+
9
+ gemspec path: '../'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
5
+
6
+ module ActiveRecordEncryption
7
+ require 'active_record_encryption/version'
8
+ require 'active_record_encryption/serializer_with_cast'
9
+ require 'active_record_encryption/type'
10
+ require 'active_record_encryption/encryptor'
11
+ require 'active_record_encryption/encryptor/cipher'
12
+ require 'active_record_encryption/encrypted_attribute'
13
+ require 'active_record_encryption/quoter'
14
+ require 'active_record_encryption/exceptions'
15
+
16
+ thread_mattr_accessor(:cipher)
17
+
18
+ def self.with_cipher(new_cipher)
19
+ previous = cipher
20
+ self.cipher = new_cipher
21
+ yield
22
+ ensure
23
+ self.cipher = previous
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ module EncryptedAttribute
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def encrypted_attribute(name, subtype, **options)
9
+ name = name.to_s
10
+
11
+ attribute(name, subtype, **options)
12
+ decorate_encrypted_attribute(name)
13
+ end
14
+
15
+ private
16
+
17
+ def decorate_encrypted_attribute(name)
18
+ decorate_attribute_type(name, :encrypted) do |subtype|
19
+ ActiveRecordEncryption::Type.new(name, subtype)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ module Encryptor
5
+ class << self
6
+ def encrypt(value, cipher: ActiveRecordEncryption.cipher)
7
+ raise_missing_cipher_error unless cipher
8
+
9
+ string = value_to_string(value)
10
+ cipher.encrypt(string)
11
+ end
12
+
13
+ def decrypt(value, cipher: ActiveRecordEncryption.cipher)
14
+ raise_missing_cipher_error unless cipher
15
+ cipher.decrypt(value)
16
+ end
17
+
18
+ private
19
+
20
+ def value_to_string(value)
21
+ ActiveRecordEncryption::Quoter.instance.type_cast(value)
22
+ end
23
+
24
+ def raise_missing_cipher_error
25
+ raise(ActiveRecordEncryption::MissingCipherError, 'missing cipher')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ module Encryptor
5
+ class Cipher
6
+ def encrypt(value)
7
+ value
8
+ end
9
+
10
+ def decrypt(value)
11
+ value
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ class Error < StandardError; end
5
+ class MissingCipherError < Error; end
6
+ class InvalidMessage < Error; end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module ActiveRecordEncryption
6
+ # Serialize ruby object to string for encryption.
7
+ class Quoter
8
+ include ActiveRecord::ConnectionAdapters::Quoting
9
+ include Singleton
10
+
11
+ # minimum length of string for encryption
12
+ def unquoted_true
13
+ 't'
14
+ end
15
+
16
+ # minimum length of string for encryption
17
+ def unquoted_false
18
+ 'f'
19
+ end
20
+
21
+ private
22
+
23
+ # Cast value to string
24
+ def _type_cast(value)
25
+ return value.to_s if value.is_a?(Numeric)
26
+ super(value)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ module SerializerWithCast
5
+ # Unfortunately, current Rails doesn't serialize value from user input with `#cast`.
6
+ # IMO, it is expected that serialized value from user input should be equal same result.
7
+ #
8
+ # Example:
9
+ # # Current
10
+ # User.attribute(:id, :boolean)
11
+ # User.where(id: 'string').to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = 'string'
12
+ # User.where(id: true).to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
13
+ #
14
+ # # Expected
15
+ # User.attribute(:id, :boolean)
16
+ # User.where(id: 'string').to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
17
+ # User.where(id: true).to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
18
+ refine ActiveModel::Type::Decimal do
19
+ def serialize(value)
20
+ cast(value)
21
+ end
22
+ end
23
+
24
+ refine ActiveModel::Type::Boolean do
25
+ def serialize(value)
26
+ cast(value)
27
+ end
28
+ end
29
+
30
+ # Backport Rails5.2
31
+ refine ActiveModel::Type::DateTime do
32
+ def serialize(value)
33
+ super(cast(value))
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module ActiveRecordEncryption
6
+ module Testing
7
+ class TestCipher < ActiveRecordEncryption::Encryptor::Cipher
8
+ attr_reader :key
9
+
10
+ def initialize(key: SecureRandom.hex)
11
+ @key = key
12
+ end
13
+
14
+ def ==(other)
15
+ other.is_a?(self.class) && key == other.key
16
+ end
17
+
18
+ def encrypt(value)
19
+ super("#{value}#{key}")
20
+ end
21
+
22
+ def decrypt(value)
23
+ value = value.to_s
24
+
25
+ if value.match(/#{key}$/)
26
+ super(value.sub(/#{key}$/, ''))
27
+ else
28
+ raise InvalidMessage, 'invalid value given'
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ class Type < ActiveRecord::Type::Value
5
+ using(ActiveRecordEncryption::SerializerWithCast)
6
+
7
+ delegate :type, :cast, to: :subtype
8
+
9
+ def initialize(name, subtype)
10
+ @name = name
11
+ @subtype = subtype
12
+ @binary = ActiveRecord::Type.lookup(:binary)
13
+ end
14
+
15
+ def deserialize(value)
16
+ deserialized = binary.deserialize(value)
17
+ subtype.deserialize(encryptor.decrypt(deserialized)) unless deserialized.nil?
18
+ end
19
+
20
+ def serialize(value)
21
+ serialized = subtype.serialize(value)
22
+ binary.serialize(encryptor.encrypt(serialized)) unless serialized.nil?
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :name, :subtype, :binary
28
+
29
+ def encryptor
30
+ ActiveRecordEncryption::Encryptor
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordEncryption
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_encryption
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alpaca-tc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
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: guard-rubocop
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: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "<"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.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.5.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
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
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
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: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sqlite3
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Write a longer description or delete this line.
168
+ email:
169
+ - alpaca-tc@alpaca.tc
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - Appraisals
179
+ - CODE_OF_CONDUCT.md
180
+ - Gemfile
181
+ - Guardfile
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - active_record_encryption.gemspec
186
+ - gemfiles/5.0_stable.gemfile
187
+ - gemfiles/5.1_stable.gemfile
188
+ - gemfiles/5.2_stable.gemfile
189
+ - lib/active_record_encryption.rb
190
+ - lib/active_record_encryption/encrypted_attribute.rb
191
+ - lib/active_record_encryption/encryptor.rb
192
+ - lib/active_record_encryption/encryptor/cipher.rb
193
+ - lib/active_record_encryption/exceptions.rb
194
+ - lib/active_record_encryption/quoter.rb
195
+ - lib/active_record_encryption/serializer_with_cast.rb
196
+ - lib/active_record_encryption/testing/test_cipher.rb
197
+ - lib/active_record_encryption/type.rb
198
+ - lib/active_record_encryption/version.rb
199
+ homepage: https://github.com/alpaca-tc/active_record_encryption
200
+ licenses:
201
+ - MIT
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 2.7.3
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: Write a short summary, because RubyGems requires one.
223
+ test_files: []