data_encryptor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +60 -0
- data/Rakefile +12 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/data_encryptor.gemspec +26 -0
- data/lib/data_encryptor.rb +46 -0
- data/lib/data_encryptor/version.rb +5 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ad6683e091c9d993542162c7513fbee14bdde98fd51835b3aa7879a6830e58e0
|
4
|
+
data.tar.gz: 192b078c1258259ae4bcb5c0f1783a9363341b004a719285e7624343ce2d603e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d18aea403e5e1d078464829b8cc4cf1487bd8984a10cae7c36b091023dd47e4de82f435fa65267bd18d14e6f1db2ba5a29cbd8c9f0727c2d51e22ae4dbdedc1
|
7
|
+
data.tar.gz: 242fb94efe276e5bf0d165b19a7a443d5e2dc0f29958807ae4ca0a356dc149632aa62d183af30d239854d0f624929d73bce12abe032a8783eabed9b073aff7c5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
data_encryptor (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.14.0)
|
10
|
+
rake (12.3.3)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
data_encryptor!
|
17
|
+
minitest (~> 5.0)
|
18
|
+
rake (~> 12.0)
|
19
|
+
|
20
|
+
BUNDLED WITH
|
21
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 David Piegza
|
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,60 @@
|
|
1
|
+
# DataEncryptor
|
2
|
+
|
3
|
+
DataEncryptor is a simple wrapper for OpenSSL::Cipher to encrypt and decrypt objects. It uses `JSON` to serialize objects
|
4
|
+
which limits encryption for basic types like String, Integer, Array, Hash and so on. This gem uses the stdlib only.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'data_encryptor'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install data_encryptor
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
You need to specify a key first. In Rails a key can be generated with `rake secret` and set in an initializer file, e.g. `data_encryptor.rb`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
DataEncryptor.setup! key: '[key]'
|
28
|
+
```
|
29
|
+
|
30
|
+
The default algorithm is `AES-256-CBC`, this can be changed with:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
DataEncryptor.setup! key: '[key]', algorithm: 'AES-128-CBC'
|
34
|
+
```
|
35
|
+
|
36
|
+
To encrypt data:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
encrypted_data = DataEncryptor.encrypt('secret credit card information')
|
40
|
+
```
|
41
|
+
|
42
|
+
To decrypt data:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
DataEncryptor.decrypt(encrypted_data)
|
46
|
+
```
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
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).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davidpiegza/data_encryptor.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'data_encryptor'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/data_encryptor/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'data_encryptor'
|
7
|
+
spec.version = DataEncryptor::VERSION
|
8
|
+
spec.authors = ['David Piegza']
|
9
|
+
|
10
|
+
spec.summary = %q{DataEncryptor is a simple wrapper for OpenSSL::Cipher to encrypt and decrypt objects.}
|
11
|
+
spec.homepage = 'https://github.com/davidpiegza/data_encryptor'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
|
+
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['source_code_uri'] = 'https://github.com/davidpiegza/data_encryptor'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'base64'
|
4
|
+
require 'json'
|
5
|
+
require 'openssl'
|
6
|
+
|
7
|
+
require 'data_encryptor/version'
|
8
|
+
|
9
|
+
module DataEncryptor
|
10
|
+
def self.setup!(key:, algorithm: 'AES-256-CBC')
|
11
|
+
@options = { key: key, algorithm: algorithm }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.encrypt(data)
|
15
|
+
raise ArgumentError, 'must specify a key' if @options[:key].to_s.empty?
|
16
|
+
|
17
|
+
cipher = new_cipher
|
18
|
+
iv = cipher.random_iv
|
19
|
+
|
20
|
+
decrypted = cipher.update(JSON.dump(data))
|
21
|
+
decrypted << cipher.final
|
22
|
+
"#{::Base64.strict_encode64(decrypted)}--#{::Base64.strict_encode64(iv)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.decrypt(encrypted_data)
|
26
|
+
raise ArgumentError, 'must specify a key' if @options[:key].to_s.empty?
|
27
|
+
|
28
|
+
encrypted_data, iv = encrypted_data.split('--').map { |v| ::Base64.strict_decode64(v) }
|
29
|
+
|
30
|
+
raise ArgumentError, 'encrypted data has invalid format' unless encrypted_data && iv
|
31
|
+
|
32
|
+
cipher = new_cipher(method: :decrypt)
|
33
|
+
cipher.iv = iv
|
34
|
+
|
35
|
+
decrypted_data = cipher.update(encrypted_data)
|
36
|
+
decrypted_data << cipher.final
|
37
|
+
|
38
|
+
JSON.parse(decrypted_data)
|
39
|
+
end
|
40
|
+
|
41
|
+
private_class_method def self.new_cipher(method: :encrypt)
|
42
|
+
cipher = OpenSSL::Cipher.new(@options.fetch(:algorithm)).public_send(method)
|
43
|
+
cipher.key = Digest::SHA256.digest @options.fetch(:key)
|
44
|
+
cipher
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_encryptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Piegza
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- Gemfile
|
21
|
+
- Gemfile.lock
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- bin/console
|
26
|
+
- bin/setup
|
27
|
+
- data_encryptor.gemspec
|
28
|
+
- lib/data_encryptor.rb
|
29
|
+
- lib/data_encryptor/version.rb
|
30
|
+
homepage: https://github.com/davidpiegza/data_encryptor
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
metadata:
|
34
|
+
homepage_uri: https://github.com/davidpiegza/data_encryptor
|
35
|
+
source_code_uri: https://github.com/davidpiegza/data_encryptor
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 2.3.0
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubygems_version: 3.0.3
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: DataEncryptor is a simple wrapper for OpenSSL::Cipher to encrypt and decrypt
|
55
|
+
objects.
|
56
|
+
test_files: []
|