aes-g 1.0.1 → 1.0.2

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: 9a31e3a0de84def8f494dfc80295c13011a7fbd2
4
- data.tar.gz: ccedbb152be001bc1fc381a705dba3a8eb283a6c
3
+ metadata.gz: 688f02b87b2d06431d8224df1fce72b305fb0c43
4
+ data.tar.gz: e6fbcc3985a1bdae4e7beabbea378e0dec3579c4
5
5
  SHA512:
6
- metadata.gz: 1091c75d92a9094164a7034d8713a9ef4d369367d6e41010d146152c355713d1ea532a001f7c3854249fcc0f5a997f81901a833d9cc9575c111398e4c6c4af1e
7
- data.tar.gz: 361291e6d11d45782207d1e3c87b85af8ac38a0be6305450b9870d03d9aad53d93f438500cacea8945e9261d759bb8b13388f5d76045cd6dcb2982461f7d9842
6
+ metadata.gz: 166950d7c374997c9870cd2f63bed268c09c38a624a0ffd649d9c1778abe445b9b7da415da75294ba07633eec8e786f6b81b5c4300fb9777763a83cbb7cceb64
7
+ data.tar.gz: 0f842088a5450167ee3d6d78d1c92b3e2472c9571422f4c05b9675e7b4b6c96b688fd5d26cccbefae7919f962c542185614fc9b035a3abfe5ce5f87723709370
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda"
10
+ gem "bundler"
11
+ gem "jeweler"
12
+ gem "simplecov"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Carl Hicks
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,65 @@
1
+ = aes
2
+
3
+ An AES encrypt/decrypt gem built on top of OpenSSL. Not as quick as FastAES but doesn't require building native extensions - also supports Base64 encoded input and output.
4
+
5
+ Usage:
6
+
7
+ require 'aes'
8
+
9
+ # Generate a random key
10
+ key = AES.key
11
+ => "290c3c5d812a4ba7ce33adf09598a462"
12
+
13
+ # Encrypt a string. Default output is base_64 encoded, init_vector and cipher_text are joined with "$"
14
+ b64 = AES.encrypt("A super secret message", key)
15
+ => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="
16
+
17
+ # Same as above but minus the base64 encoding, init_vector and cipher_text are shoved into an array
18
+ plain = AES.encrypt("A super secret message", key, {:format => :plain}) #
19
+ => [";\202\222\306\376<\206\343\023\245\312\225\214KAm",
20
+ "C\343\023\323U~W>\023y\217\341\201\371\352\334\311^\307\352{\020 H(DVw\3224N\223"]
21
+
22
+ # Generate a random initialization vector
23
+ iv = AES.iv(:base_64)
24
+ => "IJjbgbv/OvPIAf4R5qAWyg=="
25
+
26
+ # Encrypt a string, with a provided key and init_vector.
27
+ b64_iv = AES.encrypt("A super secret message", key, {:iv => iv})
28
+ => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="
29
+
30
+ AES.decrypt(b64, key)
31
+ => "A super secret message"
32
+
33
+ AES.decrypt(plain, key, {:format => :plain})
34
+ => "A super secret message"
35
+
36
+ # By default data is padded to the nearest 16 bytes block. To turn
37
+ # this off, you may use the :padding => false (or nil) option.
38
+ #
39
+ # In this mode however, the caller is required to pad the data. In
40
+ # the following example the message is exactly 16 bytes long, so no
41
+ # error aries.
42
+ msg = AES.encrypt("A secret message", key, {:padding => false})
43
+ => "SnD+WIfEfjZRrl+WAM/9pw==$89sGGZsu973j8Gl6aXC8Uw=="
44
+
45
+ # Be sure to pass the same padding option when decrypting the
46
+ # message, as it will fail if you try to decrypt unpadded data and
47
+ # didn't specify :padding => false.
48
+ AES.decrypt(msg, key, {:padding => false})
49
+ => "A secret message"
50
+
51
+ == Contributing to aes
52
+
53
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
54
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
55
+ * Fork the project
56
+ * Start a feature/bugfix branch
57
+ * Commit and push until you are happy with your contribution
58
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
59
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
60
+
61
+ == Copyright
62
+
63
+ Copyright (c) 2010 Carl Hicks. See LICENSE.txt for
64
+ further details.
65
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "aes"
16
+ gem.homepage = "http://github.com/chicks/aes"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{AES#encrypt(key, data), AES#decrypt(key, data). Capiche?}
19
+ gem.description = %Q{An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building
20
+ native extensions and supports Base64 encoded input and output.}
21
+ gem.email = "carl.hicks@gmail.com"
22
+ gem.authors = ["Carl Hicks"]
23
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
24
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
25
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
26
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ task :default => :test
38
+
39
+ require 'rdoc/task'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "aes #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
data/aes.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "aes-g"
8
+ s.version = "1.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Carl Hicks"]
12
+ s.date = "2012-05-01"
13
+ s.description = "An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building\n native extensions and supports Base64 encoded input and output."
14
+ s.email = "carl.hicks@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "aes.gemspec",
27
+ "lib/aes-g.rb",
28
+ "lib/aes/aes.rb",
29
+ ]
30
+ s.homepage = "http://github.com/chicks/aes"
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = "1.8.12"
34
+ s.summary = "AES#encrypt(key, data), AES#decrypt(key, data). Capiche?"
35
+
36
+ if s.respond_to? :specification_version then
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
41
+ s.add_development_dependency(%q<bundler>, [">= 0"])
42
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
43
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
44
+ else
45
+ s.add_dependency(%q<shoulda>, [">= 0"])
46
+ s.add_dependency(%q<bundler>, [">= 0"])
47
+ s.add_dependency(%q<jeweler>, [">= 0"])
48
+ s.add_dependency(%q<simplecov>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<shoulda>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<simplecov>, [">= 0"])
55
+ end
56
+ end
57
+
data/lib/aes-g.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'aes/aes'
data/lib/aes/aes.rb ADDED
@@ -0,0 +1,158 @@
1
+ module AES
2
+ class << self
3
+ # Encrypts the plain_text with the provided key
4
+ def encrypt(plain_text, key, opts={})
5
+ ::AES::AES.new(key, opts).encrypt(plain_text)
6
+ end
7
+ # Decrypts the cipher_text with the provided key
8
+ def decrypt(cipher_text, key, opts={})
9
+ ::AES::AES.new(key, opts).decrypt(cipher_text)
10
+ end
11
+ # Generates a random key of the specified length in bits
12
+ # Default format is :plain
13
+ def key(length=256,format=:plain)
14
+ key = ::AES::AES.new("").random_key(length)
15
+ case format
16
+ when :base_64
17
+ Base64.encode64(key).chomp
18
+ else
19
+ key
20
+ end
21
+ end
22
+ # Generates a random iv
23
+ # Default format is :plain
24
+ def iv(format=:plain)
25
+ iv = ::AES::AES.new("").random_iv
26
+ case format
27
+ when :base_64
28
+ Base64.encode64(iv).chomp
29
+ else
30
+ iv
31
+ end
32
+ end
33
+ end
34
+
35
+ class AES
36
+ attr :options
37
+ attr :key
38
+ attr :iv
39
+ attr :cipher
40
+ attr :cipher_text
41
+ attr :plain_text
42
+
43
+ def initialize(key, opts={})
44
+ merge_options opts
45
+ @cipher = nil
46
+ @key = key
47
+ @iv ||= random_iv
48
+ self
49
+ end
50
+
51
+ # Encrypts
52
+ def encrypt(plain_text)
53
+ @plain_text = plain_text
54
+ _setup(:encrypt)
55
+ @cipher.iv = @iv
56
+ case @options[:format]
57
+ when :base_64
58
+ @cipher_text = b64_e(@iv) << "$" << b64_e(_encrypt)
59
+ else
60
+ @cipher_text = [@iv, _encrypt]
61
+ end
62
+ @cipher_text
63
+ end
64
+
65
+ # Decrypts
66
+ def decrypt(cipher_text)
67
+ @cipher_text = cipher_text
68
+ _setup(:decrypt)
69
+ case @options[:format]
70
+ when :base_64
71
+ ctext = b64_d(@cipher_text)
72
+ else
73
+ ctext = @cipher_text
74
+ end
75
+ @cipher.iv = ctext[0]
76
+ @plain_text = @cipher.update(ctext[1]) + @cipher.final
77
+ end
78
+
79
+ # Generate a random initialization vector
80
+ def random_iv
81
+ _setup(:encrypt)
82
+ @cipher.random_iv
83
+ end
84
+
85
+ # Generate a random key
86
+ def random_key(length=256)
87
+ _random_seed.unpack('H*')[0][0..((length/8)-1)]
88
+ end
89
+
90
+ private
91
+
92
+ # Generates a random seed value
93
+ def _random_seed(size=32)
94
+ if defined? OpenSSL::Random
95
+ return OpenSSL::Random.random_bytes(size)
96
+ else
97
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
98
+ (1..size).collect{|a| chars[rand(chars.size)] }.join
99
+ end
100
+ end
101
+
102
+ # Un-Base64's the IV and CipherText
103
+ # Returns an array containing the IV, and CipherText
104
+ def b64_d(data)
105
+ iv_and_ctext = []
106
+ data.split('$').each do |part|
107
+ iv_and_ctext << Base64.decode64(part)
108
+ end
109
+ iv_and_ctext
110
+ end
111
+
112
+ # Base64 Encodes a string
113
+ def b64_e(data)
114
+ Base64.encode64(data).chomp
115
+ end
116
+
117
+ # Encrypts @plain_text
118
+ def _encrypt
119
+ @cipher.update(@plain_text) + @cipher.final
120
+ end
121
+
122
+ # Merge init options with defaults
123
+ def merge_options(opts)
124
+ @options = {
125
+ :format => :base_64,
126
+ :cipher => "AES-256-CBC",
127
+ :iv => nil,
128
+ :padding => true, # use cipher padding by default
129
+ }.merge! opts
130
+ _handle_iv
131
+ _handle_padding
132
+ end
133
+
134
+ def _handle_iv
135
+ @iv = @options[:iv]
136
+ return if @iv.nil?
137
+
138
+ case @options[:format]
139
+ when :base_64
140
+ @iv = Base64.decode64(@options[:iv])
141
+ end
142
+ end
143
+
144
+ def _handle_padding
145
+ # convert value to what OpenSSL module format expects
146
+ @options[:padding] = @options[:padding] ? 1 : 0
147
+ end
148
+
149
+ # Create a new cipher using the cipher type specified
150
+ def _setup(action)
151
+ @cipher ||= OpenSSL::Cipher::Cipher.new(@options[:cipher])
152
+ # Toggles encryption mode
153
+ @cipher.send(action)
154
+ @cipher.padding = @options[:padding]
155
+ @cipher.key = @key.unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
156
+ end
157
+ end
158
+ end
metadata CHANGED
@@ -1,24 +1,93 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aes-g
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Gabriel Vian
7
+ - Carl Hicks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-27 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Edited from AES.
14
- email:
11
+ date: 2012-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jeweler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |-
70
+ An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building
71
+ native extensions and supports Base64 encoded input and output.
72
+ email: carl.hicks@gmail.com
15
73
  executables: []
16
74
  extensions: []
17
- extra_rdoc_files: []
18
- files: []
19
- homepage:
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.rdoc
78
+ files:
79
+ - ".document"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - aes.gemspec
86
+ - lib/aes-g.rb
87
+ - lib/aes/aes.rb
88
+ homepage: http://github.com/chicks/aes
20
89
  licenses:
21
- - Unlicense
90
+ - MIT
22
91
  metadata: {}
23
92
  post_install_message:
24
93
  rdoc_options: []
@@ -28,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
28
97
  requirements:
29
98
  - - ">="
30
99
  - !ruby/object:Gem::Version
31
- version: 1.8.1
100
+ version: '0'
32
101
  required_rubygems_version: !ruby/object:Gem::Requirement
33
102
  requirements:
34
103
  - - ">="
@@ -38,6 +107,6 @@ requirements: []
38
107
  rubyforge_project:
39
108
  rubygems_version: 2.6.11
40
109
  signing_key:
41
- specification_version: 4
42
- summary: AES encryption for Ruby.
110
+ specification_version: 3
111
+ summary: AES#encrypt(key, data), AES#decrypt(key, data). Capiche?
43
112
  test_files: []