moneta-encryptor 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: 0191a34149d04893c0e3e4f8338130565eec915d
4
+ data.tar.gz: bd7fb7102d3848dd05f44c7a321dea8e4493d583
5
+ SHA512:
6
+ metadata.gz: 10cff6794043bf85401c8330e8b5268298bb61eddb25ef200f968769097dbe5fe002f887b2d0f8eaecc495acb6c0684a5a8a7b5cdac9fb668754afbf6b92d444
7
+ data.tar.gz: 959427380fe07a3beb5f5f710a49fc704444ddb97ae53b53e2af23235da4e2bab34c6ad28e934b973505a239cdc4e395742478ad3f462bc697e667f52028882e
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 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ Metrics/LineLength:
2
+ Exclude:
3
+ - '*.gemspec'
4
+
5
+ Metrics/LineLength:
6
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in moneta-encryptor.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem 'pry'
10
+ gem 'rspec'
11
+ gem 'rubocop'
12
+ gem 'simplecov'
13
+ end
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Moneta::Encryptor
2
+
3
+ The gem provides a simple symmetric encryption layer to the Moneta chain.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'moneta-encryptor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install moneta-encryptor
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ require 'moneta/encryptor'
25
+
26
+ Moneta.build do
27
+ use :Encryptor, encryption_key: 'SECRET' * 6
28
+ adapter :Memory
29
+ end
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anakinj/moneta-encryptor.
35
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
@@ -0,0 +1,82 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ require 'moneta'
5
+
6
+ module Moneta
7
+ # :nodoc:
8
+ class Encryptor < Moneta::Proxy
9
+ ENCRYPTION_KEY_KEY = :encryption_key
10
+
11
+ def initialize(adapter, options = {})
12
+ @default_key = options[ENCRYPTION_KEY_KEY]
13
+ @algorithm = options[:encryption_algorithm] || 'aes-256-cbc'
14
+ validate_key_length!
15
+ super
16
+ end
17
+
18
+ def load(key, options = {})
19
+ value = super
20
+ decrypt(value, encryption_key(options))
21
+ end
22
+
23
+ def store(key, value, options = {})
24
+ super(key,
25
+ encrypt(value, encryption_key(options)),
26
+ Moneta::Utils.without(options, :encryption_key))
27
+ value
28
+ end
29
+
30
+ private
31
+
32
+ def validate_key_length!
33
+ return if @default_key.nil?
34
+ OpenSSL::Cipher.new(@algorithm).key = @default_key
35
+ end
36
+
37
+ def decrypt(value, key)
38
+ return value if value.nil? || key.nil?
39
+ return value unless value.is_a?(String) && value.include?('.')
40
+
41
+ cipher = OpenSSL::Cipher.new(@algorithm)
42
+ cipher.decrypt
43
+ cipher.key = key
44
+ cipher.iv, encrypted_value = extract_iv_and_value(value)
45
+
46
+ decrypted_value = cipher.update(encrypted_value)
47
+ decrypted_value << cipher.final
48
+
49
+ decrypted_value
50
+ end
51
+
52
+ def encrypt(value, key)
53
+ return value if key.nil? || !value.is_a?(String)
54
+ cipher = OpenSSL::Cipher.new(@algorithm)
55
+ cipher.encrypt
56
+ cipher.key = key
57
+
58
+ iv = cipher.random_iv
59
+
60
+ encrypted_data = cipher.update(value)
61
+ encrypted_data << cipher.final
62
+
63
+ tuck_iv_and_value(iv, encrypted_data)
64
+ end
65
+
66
+ def encryption_key(options)
67
+ options.fetch(ENCRYPTION_KEY_KEY, @default_key)
68
+ end
69
+
70
+ def tuck_iv_and_value(iv, encrypted_value)
71
+ encoded_values = [iv, encrypted_value].map do |value|
72
+ Base64.strict_encode64(value)
73
+ end
74
+
75
+ encoded_values.join('.')
76
+ end
77
+
78
+ def extract_iv_and_value(raw_value)
79
+ raw_value.split('.').map { |value| Base64.strict_decode64(value) }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'moneta-encryptor'
6
+ spec.version = File.read(File.expand_path('../version', __FILE__)).chomp
7
+ spec.authors = ['Joakim Antman']
8
+ spec.email = ['antmanj@gmail.com']
9
+
10
+ spec.summary = 'Moneta transformer that encrypts the values before persisted in the store'
11
+ spec.homepage = 'https://github.com/anakinj/moneta-encryptor'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'moneta'
21
+ spec.add_development_dependency 'bundler', '~> 1.15'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
data/version ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moneta-encryptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Antman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: moneta
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - antmanj@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - lib/moneta/encryptor.rb
69
+ - moneta-encryptor.gemspec
70
+ - version
71
+ homepage: https://github.com/anakinj/moneta-encryptor
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.7
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Moneta transformer that encrypts the values before persisted in the store
94
+ test_files: []