aes_keeper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e21e5cca9b69f4f7c2b8b4293c9dccb94b5424b6
4
- data.tar.gz: 5dbbb5ffb17a53f95911c39033ba98418b66cbbd
3
+ metadata.gz: fe002651b1e55b1ed52eead2ed8d8988b2fadf45
4
+ data.tar.gz: f4ccfcb450f6584c44e203bb92b2ddb672c1b890
5
5
  SHA512:
6
- metadata.gz: b5966c1289e8340d51ff1931a34190804f0066427c6b583d1d502488924c41d150412bc52cff7e60148fd694307ea1636b8546c29a25ff1c80e3542893ef5757
7
- data.tar.gz: fbd85365c8ae110a1c85c2eee8120dc9a093bec804558217548fa2cec67644777d05c545c3bec8f4479609daeb47eca431f9e2dbfa0a773d7bd6e9bdc8404f2d
6
+ metadata.gz: fb09a61e37d0d8f499e1f645210000b7e2b9f7235d79adb8fa10fdb48408ed8f3d1ec9d3ca72cb9573243eefe6467d58dca80bfc8cd5a6ae30bc9ecf0e20b89e
7
+ data.tar.gz: 59b0e16ce24f89ef24cf2646a826c47b99682673ed0c402a00cf0353bfcc746dd1fd3e4a949822ff492653a136cc91e0c0afa7e2a1f3599be787de23e2e33459
data/README.md CHANGED
@@ -26,6 +26,12 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ #####AESKeeper::AESKeeperPure
30
+ - Pure String implementation. Backwards compatible and crossplatform compatible.
31
+
32
+ #####AESKeeper::AESKeeperMarshal && AesKeeper
33
+ - Marshalling Object implementation
34
+
29
35
  With encryption and iv result separate:
30
36
  ```ruby
31
37
  cipher = AesKeeper.new(key: 'some really good 32+ character long key for encryption! ^_^')
data/lib/aes_keeper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "aes_keeper/version"
2
2
  require 'armor'
3
3
  require 'base64'
4
+ require 'aes_keeper/aes_keeper_common.rb'
4
5
  require 'aes_keeper/aes_keeper_pure.rb'
5
6
  require 'aes_keeper/aes_keeper_marshal.rb'
6
7
 
@@ -0,0 +1,57 @@
1
+ module AESKeeper
2
+ class AESKeeperCommon
3
+ VERSION = VERSION
4
+ def initialize(options = {})
5
+ raise ":key required!" unless options.has_key? :key
6
+ raise ":key needs to be at-least 32 characters!" unless options[:key].length >= 32
7
+ @key = key(options)
8
+ @cipher = OpenSSL::Cipher::AES256.new(:CBC)
9
+ end
10
+
11
+ private
12
+ def key(options = {})
13
+ return nil unless options.has_key? :key
14
+ if options.has_key?(:salt) && options.fetch(:armor) {true}
15
+ Armor.digest(options[:key], options[:salt])
16
+ else
17
+ options[:key]
18
+ end
19
+ end
20
+
21
+ def decrypt_validations bndng
22
+ options = bndng.local_variable_get :options
23
+ if options.is_a? String
24
+ text = options.dup
25
+ options = {}
26
+ if text.scan(/:/).one?
27
+ options[:encrypted], options[:iv] = text.split(':')
28
+ else
29
+ raise 'Invalid String input!'
30
+ end
31
+ end
32
+ raise ':encrypted required!' unless options.has_key?(:encrypted)
33
+ raise ':iv required!' unless options.has_key?(:iv)
34
+ raise ':encrypted invalid type!' unless options[:encrypted].is_a? String
35
+ raise ':iv invalid type!' unless options[:iv].is_a? String
36
+ return '' if options[:encrypted].strip.empty? and options[:iv].strip.empty?
37
+ bndng.local_variable_set :options, options
38
+ end
39
+
40
+ def stringable obj
41
+ obj.tap { |i|
42
+ i.define_singleton_method(:to_s) { [self[:encrypted], self[:iv]].join(":") }
43
+ i.define_singleton_method(:to_string) { [self[:encrypted], self[:iv]].join(":") }
44
+ }
45
+ end
46
+
47
+ def nothing obj
48
+ obj.tap {|i|
49
+ i.define_singleton_method(:to_s){''}
50
+ i.define_singleton_method(:blank?){true}
51
+ i.define_singleton_method(:empty?){true}
52
+ i.define_singleton_method(:presence){''}
53
+ i.define_singleton_method(:present?){false}
54
+ }
55
+ end
56
+ end
57
+ end
@@ -1,13 +1,5 @@
1
1
  module AESKeeper
2
- class AESKeeperMarshal
3
- VERSION = VERSION
4
- def initialize(options = {})
5
- raise ":key required!" unless options.has_key? :key
6
- raise ":key needs to be at-least 32 characters!" unless options[:key].length >= 32
7
- @key = key(options)
8
- @cipher = OpenSSL::Cipher::AES256.new(:CBC)
9
- end
10
-
2
+ class AESKeeperMarshal < AESKeeperCommon
11
3
  def encrypt(content, options = {})
12
4
  if (content != nil) and (content != "")
13
5
  @cipher.encrypt
@@ -16,38 +8,16 @@ module AESKeeper
16
8
  @cipher.iv = iv
17
9
  encrypted = @cipher.update Marshal.dump(content)
18
10
  encrypted << @cipher.final
19
- {
11
+ stringable({
20
12
  encrypted: Base64.encode64(encrypted).encode('utf-8'), iv: Base64.encode64(iv).encode('utf-8')
21
- }.tap { |i|
22
- i.define_singleton_method(:to_s) { [self[:encrypted], self[:iv]].join(":") }
23
- i.define_singleton_method(:to_string) { [self[:encrypted], self[:iv]].join(":") }
24
- }
13
+ })
25
14
  else
26
- { encrypted: '', iv: ''}.tap {|i|
27
- i.define_singleton_method(:to_s){''}
28
- i.define_singleton_method(:blank?){true}
29
- i.define_singleton_method(:empty?){true}
30
- i.define_singleton_method(:presence){''}
31
- i.define_singleton_method(:present?){false}
32
- }
15
+ nothing({ encrypted: '', iv: ''})
33
16
  end
34
17
  end
35
18
 
36
19
  def decrypt(options = {})
37
- if options.is_a? String
38
- text = options.dup
39
- options = {}
40
- if text.scan(/:/).one?
41
- options[:encrypted], options[:iv] = text.split(':')
42
- else
43
- raise 'Invalid String input!'
44
- end
45
- end
46
- raise ':encrypted required!' unless options.has_key?(:encrypted)
47
- raise ':iv required!' unless options.has_key?(:iv)
48
- raise ':encrypted invalid type!' unless options[:encrypted].is_a? String
49
- raise ':iv invalid type!' unless options[:iv].is_a? String
50
- return '' if options[:encrypted].strip.empty? and options[:iv].strip.empty?
20
+ decrypt_validations binding
51
21
  @cipher.decrypt
52
22
  @cipher.key = key(options) || @key
53
23
  @cipher.iv = Base64.decode64(options[:iv].encode('ascii-8bit'))
@@ -59,10 +29,5 @@ module AESKeeper
59
29
  raise 'Decryption failed.'
60
30
  end
61
31
  end
62
-
63
- def key(options = {})
64
- return nil unless options.has_key? :key
65
- options.has_key?(:salt) ? Armor.digest(options[:key], options[:salt]) : options[:key]
66
- end
67
32
  end
68
33
  end
@@ -1,12 +1,5 @@
1
1
  module AESKeeper
2
- class AESKeeperPure
3
- def initialize(options = {})
4
- raise ":key required!" unless options.has_key? :key
5
- raise ":key needs to be at-least 32 characters!" unless options[:key].length >= 32
6
- @key = key(options)
7
- @cipher = OpenSSL::Cipher::AES256.new(:CBC)
8
- end
9
-
2
+ class AESKeeperPure < AESKeeperCommon
10
3
  def encrypt(content, options = {})
11
4
  if (content != nil) and (content != "")
12
5
  @cipher.encrypt
@@ -15,38 +8,16 @@ module AESKeeper
15
8
  @cipher.iv = @iv
16
9
  encrypted = @cipher.update content
17
10
  encrypted << @cipher.final
18
- {
11
+ stringable({
19
12
  encrypted: Base64.encode64(encrypted).encode('utf-8'), iv: Base64.encode64(@iv).encode('utf-8')
20
- }.tap { |i|
21
- i.define_singleton_method(:to_s) { [self[:encrypted], self[:iv]].join(":") }
22
- i.define_singleton_method(:to_string) { [self[:encrypted], self[:iv]].join(":") }
23
- }
24
- else
25
- { encrypted: '', iv: ''}.tap {|i|
26
- i.define_singleton_method(:to_s){''}
27
- i.define_singleton_method(:blank?){true}
28
- i.define_singleton_method(:empty?){true}
29
- i.define_singleton_method(:presence){''}
30
- i.define_singleton_method(:present?){false}
31
- }
13
+ })
14
+ else
15
+ nothing({ encrypted: '', iv: ''})
32
16
  end
33
17
  end
34
18
 
35
19
  def decrypt(options = {})
36
- if options.is_a? String
37
- text = options.dup
38
- options = {}
39
- if text.scan(/:/).one?
40
- options[:encrypted], options[:iv] = text.split(':')
41
- else
42
- raise 'Invalid String input!'
43
- end
44
- end
45
- raise ':encrypted required!' unless options.has_key?(:encrypted)
46
- raise ':iv required!' unless options.has_key?(:iv)
47
- raise ':encrypted invalid type!' unless options[:encrypted].is_a? String
48
- raise ':iv invalid type!' unless options[:iv].is_a? String
49
- return '' if options[:encrypted].strip.empty? and options[:iv].strip.empty?
20
+ decrypt_validations binding
50
21
  @cipher.decrypt
51
22
  @cipher.key = key(options) || @key
52
23
  @cipher.iv = Base64.decode64(options[:iv].encode('ascii-8bit'))
@@ -58,10 +29,5 @@ module AESKeeper
58
29
  raise 'Decryption failed.'
59
30
  end
60
31
  end
61
-
62
- def key(options = {})
63
- return nil unless options.has_key? :key
64
- options.has_key?(:salt) ? Armor.digest(options[:key], options[:salt]) : options[:key]
65
- end
66
32
  end
67
33
  end
@@ -1,3 +1,3 @@
1
1
  module AESKeeper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -6,6 +6,24 @@ describe AESKeeper::AESKeeperPure do
6
6
  let(:encrypt2){AESKeeper::AESKeeperPure.new key: "asdfghjklqwertyuiopzxcvbnm1234567890", salt: "shaker"}
7
7
  let(:encrypt3){AESKeeper::AESKeeperPure.new key: uuid, salt: "Example.com"}
8
8
 
9
+ it "can be reproducible and optional PBKDF2" do
10
+ enc = AESKeeper::AESKeeperPure.new key: "asdfghjklqwertyuiopzxcvbnm1234567890", salt: "shaker", armor: false
11
+ enc.instance_exec {@iv="F5DC1E5A25F87C2201FE9B8D682D22CE"}
12
+ a = enc.encrypt("asdf@moo.com").to_s
13
+
14
+ enc = AESKeeper::AESKeeperPure.new key: "asdfghjklqwertyuiopzxcvbnm1234567890", salt: "shaker"
15
+ enc.instance_exec {@iv="F5DC1E5A25F87C2201FE9B8D682D22CE"}
16
+ b = enc.encrypt("asdf@moo.com").to_s
17
+
18
+ enc = AESKeeper::AESKeeperPure.new key: "asdfghjklqwertyuiopzxcvbnm1234567890", salt: "shaker", armor: false
19
+ enc.instance_exec {@iv="F5DC1E5A25F87C2201FE9B8D682D22CE"}
20
+ c = enc.encrypt("asdf@moo.com").to_s
21
+
22
+ _(a).must_equal c
23
+ _(a).wont_equal b
24
+ _(b).wont_equal c
25
+ end
26
+
9
27
  it "produces a hash of size 2" do
10
28
  hsh = encrypt1.encrypt("moo")
11
29
  _(hsh).must_be_kind_of Hash
@@ -53,3 +71,10 @@ describe AESKeeper::AESKeeperPure do
53
71
  end
54
72
  end
55
73
 
74
+
75
+ # ~> LoadError
76
+ # ~> cannot load such file -- minitest_helper
77
+ # ~>
78
+ # ~> /home/danielpclark/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
79
+ # ~> /home/danielpclark/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
80
+ # ~> /tmp/seeing_is_believing_temp_dir20160202-24131-1bft3zn/program.rb:1:in `<main>'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aes_keeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,7 @@ files:
108
108
  - Rakefile
109
109
  - aes_keeper.gemspec
110
110
  - lib/aes_keeper.rb
111
+ - lib/aes_keeper/aes_keeper_common.rb
111
112
  - lib/aes_keeper/aes_keeper_marshal.rb
112
113
  - lib/aes_keeper/aes_keeper_pure.rb
113
114
  - lib/aes_keeper/version.rb