aes_keeper 0.0.1

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: dd63ef7d34c5f8f6cb4896b069bdd0edd149ec0a
4
+ data.tar.gz: 8bdef6588a29ca0648c67a252eef7ebeb202afb1
5
+ SHA512:
6
+ metadata.gz: 023da16fb9fb354624724c0262400587194d1839286940862cfa6dd320198ba1d1bd622db7250ffc3eee9e8de3eb6fc58f8b2dbfcc6aa88cbb4ebde61eed9f29
7
+ data.tar.gz: 3ed04c398b32c92e3e04872cfbe68c735c605b78fa75ee56d5a7e112dadf8b06ef8ce3877a51452e39dc3b57eef31321f475e56f0250c682bbad47d32ff68cae
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ *.gem
16
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aes_keeper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel P. Clark
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # AesKeeper
2
+
3
+ A database safe encryption using AES 256 CBC. Key required, Salt optional. Returns encrypted String and IV,
4
+ or you can call **.to_s** to receive the encrypted data as one String. Decrypt will accept either form of the
5
+ { encrypted: '', iv: '' } pair or the String result.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'aes_keeper'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install aes_keeper
22
+
23
+ ## Usage
24
+
25
+ With encryption and iv result separate:
26
+ ```ruby
27
+ cipher = AesKeeper.new(key: 'some really good 32 character long key for encryption! ^_^')
28
+ result = cipher.encrypt("apples")
29
+ # => {:encrypted=>"K1E/a75/3zxNKCTXJFJZaVe8jjeHFQG+Cv1Lxntz3oM=\n", :iv=>"uQozeVXYhaeK7Rvh1na6kA==\n"}
30
+ cipher.decrypt(result)
31
+ # => "apples"
32
+ ```
33
+
34
+ With encryption and iv result combined:
35
+ ```ruby
36
+ cipher = AesKeeper.new(key: 'some really good 32 character long key for encryption! ^_^')
37
+ result = cipher.encrypt("apples").to_s
38
+ # => "xPbb+kVIROlA5YHvSaKsYcCobgBz/TQHpJ5wP9lAuBQ=\n:WIaGiOhjFBRBVJReiA2aTA==\n"
39
+ cipher.decrypt(result)
40
+ # => "apples"
41
+ ```
42
+
43
+ You can specify a Salt as well:
44
+ ```ruby
45
+ AesKeeper.new(
46
+ key: 'some really good 32 character long key for encryption! ^_^',
47
+ salt: 'asdfqwerty'
48
+ )
49
+ ```
50
+
51
+ ## Contributing
52
+
53
+ This code can be refactored and tested. Otherwise I consider this project feature complete. It's designed
54
+ with assumptions for its implementation and it's met those requirements.
55
+
56
+ 1. Fork it ( https://github.com/[my-github-username]/aes_keeper/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
61
+
62
+ ## License
63
+
64
+ Copyright (c) 2015 Daniel P. Clark
65
+
66
+ MIT License
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ "Software"), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
82
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
83
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
84
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
85
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aes_keeper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aes_keeper"
8
+ spec.version = AesKeeper::VERSION
9
+ spec.authors = ["Daniel P. Clark"]
10
+ spec.email = ["6ftdan@gmail.com"]
11
+ spec.summary = %q{Encrypt data via AES to database worthy strings.}
12
+ spec.description = %q{Encrypt data to strings via AES 256bit encryption. Database string safe.}
13
+ spec.homepage = "https://github.com/danielpclark/aes_keeper"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = '~> 2.0'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_runtime_dependency "armor", "~> 0.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ class AesKeeper
2
+ VERSION = "0.0.1"
3
+ end
data/lib/aes_keeper.rb ADDED
@@ -0,0 +1,69 @@
1
+ require "aes_keeper/version"
2
+ require 'armor'
3
+ require 'base64'
4
+
5
+ class AesKeeper
6
+ def initialize(options = {})
7
+ raise ":key required!" unless options.has_key? :key
8
+ raise ":key needs to be at-least 32 characters!" unless options[:key].length >= 32
9
+ @key = key(options)
10
+ @cipher = OpenSSL::Cipher::AES256.new(:CBC)
11
+ end
12
+
13
+ def encrypt(content, options = {})
14
+ if (content != nil) and (content != "")
15
+ @cipher.encrypt
16
+ @cipher.key = key(options) || @key
17
+ iv = @cipher.random_iv
18
+ @cipher.iv = iv
19
+ encrypted = @cipher.update Marshal.dump(content)
20
+ encrypted << @cipher.final
21
+ {
22
+ encrypted: Base64.encode64(encrypted).encode('utf-8'), iv: Base64.encode64(iv).encode('utf-8')
23
+ }.tap { |i|
24
+ i.define_singleton_method(:to_s) { [self[:encrypted], self[:iv]].join(":") }
25
+ i.define_singleton_method(:to_string) { [self[:encrypted], self[:iv]].join(":") }
26
+ }
27
+ else
28
+ { encrypted: '', iv: ''}.tap {|i|
29
+ i.define_singleton_method(:to_s){''}
30
+ i.define_singleton_method(:blank?){true}
31
+ i.define_singleton_method(:empty?){true}
32
+ i.define_singleton_method(:presence){''}
33
+ i.define_singleton_method(:present?){false}
34
+ }
35
+ end
36
+ end
37
+
38
+ def decrypt(options = {})
39
+ if options.is_a? String
40
+ text = options.dup
41
+ options = {}
42
+ if text.scan(/:/).one?
43
+ options[:encrypted], options[:iv] = text.split(':')
44
+ else
45
+ raise 'Invalid String input!'
46
+ end
47
+ end
48
+ raise ':encrypted required!' unless options.has_key?(:encrypted)
49
+ raise ':iv required!' unless options.has_key?(:iv)
50
+ raise ':encrypted invalid type!' unless options[:encrypted].is_a? String
51
+ raise ':iv invalid type!' unless options[:iv].is_a? String
52
+ return '' if options[:encrypted].strip.empty? and options[:iv].strip.empty?
53
+ @cipher.decrypt
54
+ @cipher.key = key(options) || @key
55
+ @cipher.iv = Base64.decode64(options[:iv].encode('ascii-8bit'))
56
+ decrypted = @cipher.update Base64.decode64(options[:encrypted].encode('ascii-8bit'))
57
+ begin
58
+ decrypted << @cipher.final
59
+ Marshal.load(decrypted)
60
+ rescue
61
+ raise 'Decryption failed.'
62
+ end
63
+ end
64
+
65
+ def key(options = {})
66
+ return nil unless options.has_key? :key
67
+ options.has_key?(:salt) ? Armor.digest(options[:key], options[:salt]) : options[:key]
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aes_keeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel P. Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-09 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: armor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.0'
55
+ description: Encrypt data to strings via AES 256bit encryption. Database string safe.
56
+ email:
57
+ - 6ftdan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - aes_keeper.gemspec
68
+ - lib/aes_keeper.rb
69
+ - lib/aes_keeper/version.rb
70
+ homepage: https://github.com/danielpclark/aes_keeper
71
+ licenses:
72
+ - MIT
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: '2.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.4.5
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Encrypt data via AES to database worthy strings.
94
+ test_files: []