activecrypto 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/activecrypto.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_crypto/encryptor.rb +58 -0
- data/lib/active_crypto/version.rb +3 -0
- data/lib/active_crypto.rb +31 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1908d143a26a8d854dff58b7a413a26b0ea47df8
|
4
|
+
data.tar.gz: 3f7fe03666b86a5b0962b680cd941663cf54ff05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3514eb911a8dbfba0ce98a6acd256f3a80054667662532f529ea77592ea7a0b4789e9f3198e5f9bae5d5640aac89956dbedfb5d16b1359cb6e3949bb318efca1
|
7
|
+
data.tar.gz: 4a963fd1de6b256ca6b18453353e69d4a41fd0bbd7836c14af0573ba800633197c1d7e42c9204adff7ee9eec97d68756336701b28f86a930f1d2464a1b5e3e82
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Write your name
|
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,62 @@
|
|
1
|
+
# Active Crypto
|
2
|
+
|
3
|
+
Encryption and decryption of a model's properties (Rails 4.1+, OpenSSL)
|
4
|
+
|
5
|
+
NOTE: Default key will be used from the **secrets.yml** file
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'activecrypto'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install activecrypto
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# config/application.rb
|
27
|
+
require 'active_crypto'
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# app/models/user.rb
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
# basic encrypt (default AES-256-CBC)
|
34
|
+
encrypt :column_name
|
35
|
+
...
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Or with with custom cipher like AES-256-CBC
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
encrypt :column_name, { cipher: 'AES', block_mode: 'CBC', keylength: 256 }
|
44
|
+
...
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
If you need to choose an encryption algorithm. You can see the full list with:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ruby -r openssl -e 'puts OpenSSL::Cipher.ciphers'
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mtunjic/activecrypto.
|
57
|
+
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_crypto/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activecrypto"
|
8
|
+
spec.version = ActiveCrypto::VERSION
|
9
|
+
spec.authors = ["Marko Tunjic"]
|
10
|
+
spec.email = ["marko.tunjic@live.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Encryption and decryption of a model’s properties.}
|
13
|
+
spec.description = %q{Encryption and decryption of a model’s properties.}
|
14
|
+
spec.homepage = "https://github.com/mtunjic/activecrypto"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_crypto"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module ActiveCrypto
|
5
|
+
class Encryptor
|
6
|
+
|
7
|
+
attr_reader :options
|
8
|
+
|
9
|
+
def initialize(field, options = {})
|
10
|
+
|
11
|
+
@field = field
|
12
|
+
@options = {:cipher => 'AES',
|
13
|
+
:block_mode => 'CBC',
|
14
|
+
:keylength => 256}.merge(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_save(model)
|
18
|
+
unless model[@field].blank?
|
19
|
+
key = model.class.encryption_key
|
20
|
+
model[@field] = encrypt(model[@field], key, @options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def after_save(model)
|
25
|
+
unless model[@field].blank?
|
26
|
+
key = model.class.encryption_key
|
27
|
+
model[@field] = decrypt(model[@field], key, @options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def encrypt(text, key, opt)
|
32
|
+
cipher = OpenSSL::Cipher.new(algorithm(opt))
|
33
|
+
cipher.encrypt
|
34
|
+
cipher.key = key
|
35
|
+
iv = cipher.random_iv
|
36
|
+
encrypted = cipher.update(text) + cipher.final
|
37
|
+
encrypted.insert(0, iv)
|
38
|
+
Base64.encode64(encrypted)
|
39
|
+
end
|
40
|
+
|
41
|
+
def decrypt(text, key, opt)
|
42
|
+
decipher = OpenSSL::Cipher.new(algorithm(opt))
|
43
|
+
decipher.decrypt
|
44
|
+
decoded_text = Base64.decode64(text)
|
45
|
+
decipher.key = key
|
46
|
+
decipher.iv = decoded_text.slice!(0..15)
|
47
|
+
decrypted = decipher.update(decoded_text)
|
48
|
+
decrypted << decipher.final
|
49
|
+
end
|
50
|
+
|
51
|
+
def algorithm(opts)
|
52
|
+
"#{opts[:cipher]}-#{opts[:keylength]}-#{opts[:block_mode]}"
|
53
|
+
end
|
54
|
+
|
55
|
+
alias_method :after_find, :after_save
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#-------------------------------
|
2
|
+
# Copyright © 2016 Marko Tunjic.
|
3
|
+
#-------------------------------
|
4
|
+
require 'active_crypto/version'
|
5
|
+
require 'active_support/concern'
|
6
|
+
require 'active_crypto/encryptor'
|
7
|
+
|
8
|
+
module ActiveCrypto
|
9
|
+
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def encrypt(attribute, options = {})
|
14
|
+
# setup
|
15
|
+
encryptor = ActiveCrypto::Encryptor.new(attribute, options)
|
16
|
+
# hooks
|
17
|
+
before_save(encryptor)
|
18
|
+
after_save(encryptor)
|
19
|
+
after_find(encryptor)
|
20
|
+
define_method(:after_find) { }
|
21
|
+
end
|
22
|
+
|
23
|
+
def encryption_key
|
24
|
+
Rails.application.secrets.secret_key_base
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
ActiveRecord::Base.send(:include, ActiveCrypto)
|
30
|
+
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activecrypto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marko Tunjic
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-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: '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
|
+
description: Encryption and decryption of a model’s properties.
|
56
|
+
email:
|
57
|
+
- marko.tunjic@live.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- activecrypto.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/active_crypto.rb
|
73
|
+
- lib/active_crypto/encryptor.rb
|
74
|
+
- lib/active_crypto/version.rb
|
75
|
+
homepage: https://github.com/mtunjic/activecrypto
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata:
|
79
|
+
allowed_push_host: https://rubygems.org
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.4
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Encryption and decryption of a model’s properties.
|
100
|
+
test_files: []
|