aes 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -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.
@@ -0,0 +1,41 @@
1
+ = aes
2
+
3
+ An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building native extensions and supports Base64 encoded input and output.
4
+
5
+ Usage:
6
+
7
+ require 'aes'
8
+
9
+ # Generate a random key
10
+ key = AES.key(32)
11
+ => "290c3c5d812a4ba7ce33adf09598a462"
12
+
13
+ # Encrypt something, default output is base_64 encoded. init_vector and cipher_text are joined with "$"
14
+ b64 = AES.encrypt(key, "A super secret message")
15
+ => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="
16
+
17
+ # Same as above but minus the base64 encoding, initialization vector and cipher_text are shoved into an array
18
+ plain = AES.encrypt(key, "A super secret message", {:format => :plain}) #
19
+ => [";\202\222\306\376<\206\343\023\245\312\225\214KAm", "C\343\023\323U~W>\023y\217\341\201\371\352\334\311^\307\352{\020 H(DVw\3224N\223"]
20
+
21
+ AES.decrypt(key, b64)
22
+ => "A super secret message"
23
+
24
+ AES.decrypt(key, plain, {:format => :plain})
25
+ => "A super secret message"
26
+
27
+ == Contributing to aes
28
+
29
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
30
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
31
+ * Fork the project
32
+ * Start a feature/bugfix branch
33
+ * Commit and push until you are happy with your contribution
34
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
35
+ * 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.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2010 Carl Hicks. See LICENSE.txt for
40
+ further details.
41
+
@@ -0,0 +1,54 @@
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
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "aes #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,64 @@
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 = %q{aes}
8
+ s.version = "0.3.0"
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 = %q{2010-12-05}
13
+ s.description = %q{An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building
14
+ native extensions and supports Base64 encoded input and output.}
15
+ s.email = %q{carl.hicks@gmail.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "aes.gemspec",
28
+ "lib/aes.rb",
29
+ "test/helper.rb",
30
+ "test/test_aes.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/chicks/aes}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{AES#encrypt(key, data), AES#decrypt(key, data). Capiche?}
37
+ s.test_files = [
38
+ "test/helper.rb",
39
+ "test/test_aes.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,130 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ class AES
5
+ class << self
6
+ # Encrypts the plain_text with the provided key
7
+ def encrypt(key, plain_text, opts={})
8
+ AES.new(key,opts).encrypt(plain_text)
9
+ end
10
+ # Decrypts the cipher_text with the provided key
11
+ def decrypt(key, cipher_text, opts={})
12
+ AES.new(key,opts).decrypt(cipher_text)
13
+ end
14
+ # Generates a random key of the specified length in bits
15
+ # Default output is
16
+ def key(length=256,format=:plain)
17
+ key = AES.new("").random_key(256)
18
+ case format
19
+ when :base_64
20
+ Base64.encode64(key)
21
+ else
22
+ key
23
+ end
24
+ end
25
+ end
26
+
27
+ attr :options
28
+ attr :key
29
+ attr :cipher
30
+ attr :cipher_text
31
+ attr :plain_text
32
+
33
+ def initialize(key, opts={})
34
+ merge_options opts
35
+ @key = key
36
+ self
37
+ end
38
+
39
+ # Encrypts
40
+ def encrypt(plain_text)
41
+ @plain_text = plain_text
42
+ _setup(:encrypt)
43
+ case @options[:format]
44
+ when :base_64
45
+ @cipher_text = b64_e(_iv) << "$" << b64_e(_encrypt)
46
+ else
47
+ @cipher_text = [_iv, _encrypt]
48
+ end
49
+ @cipher_text
50
+ end
51
+
52
+ # Decrypts
53
+ def decrypt(cipher_text)
54
+ @cipher_text = cipher_text
55
+ _setup(:decrypt)
56
+ case @options[:format]
57
+ when :base_64
58
+ ctext = b64_d(@cipher_text)
59
+ else
60
+ ctext = @cipher_text
61
+ end
62
+ @cipher.iv = ctext[0]
63
+ @plain_text = @cipher.update(ctext[1]) + @cipher.final
64
+ end
65
+
66
+ # Generate a random initialization vector
67
+ def random_iv
68
+ _setup(:encrypt)
69
+ _iv
70
+ end
71
+
72
+ # Generate a random key
73
+ def random_key(length=256)
74
+ _random_seed.unpack('H*')[0][0..((length/8)-1)]
75
+ #Digest::SHA256.digest(_random_seed)[0..(length / 8)]
76
+ end
77
+
78
+ private
79
+
80
+ # Generates a random seed value
81
+ def _random_seed(size=32)
82
+ if defined? OpenSSL::Random
83
+ return OpenSSL::Random.random_bytes(size)
84
+ else
85
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
86
+ (1..size).collect{|a| chars[rand(chars.size)] }.join
87
+ end
88
+ end
89
+
90
+ # Un-Base64's the IV and CipherText
91
+ # Returns an array containing the IV, and CipherText
92
+ def b64_d(data)
93
+ iv_ctext = []
94
+ data.split('$').each do |part|
95
+ iv_ctext << Base64.decode64(part)
96
+ end
97
+ iv_ctext
98
+ end
99
+
100
+ # Base64 Encodes a string
101
+ def b64_e(data)
102
+ Base64.encode64(data).chomp
103
+ end
104
+
105
+ # Generates and returns a random initialization vector
106
+ def _iv
107
+ @cipher.random_iv
108
+ end
109
+
110
+ # Encrypts @plain_text
111
+ def _encrypt
112
+ @cipher.update(@plain_text) + @cipher.final
113
+ end
114
+
115
+ # Merge init options with defaults
116
+ def merge_options(opts)
117
+ @options = {
118
+ :format => :base_64,
119
+ :cipher => "AES-256-CBC"
120
+ }.merge! opts
121
+ end
122
+
123
+ # Create a new cipher using the cipher type specified
124
+ def _setup(action)
125
+ @cipher = OpenSSL::Cipher::Cipher.new(@options[:cipher])
126
+ # Toggles encryption mode
127
+ @cipher.send(action)
128
+ @cipher.key = @key.unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
129
+ end
130
+ end
@@ -0,0 +1,18 @@
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 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'aes'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ class TestAES < Test::Unit::TestCase
4
+
5
+ should "encrypt and decrypt a string" do
6
+ key = "01234567890123456789012345678901"
7
+ msg = "This is a message that nobody should ever see"
8
+ enc = AES.encrypt(key, msg)
9
+ assert_equal msg, AES.decrypt(key, enc)
10
+ enc = AES.encrypt(key, msg, {:format => :plain})
11
+ assert_equal msg, AES.decrypt(key, enc, {:format => :plain})
12
+ end
13
+
14
+ should "generate a new key when AES#key" do
15
+ assert_equal 32, AES.key.length
16
+ assert_equal 45, AES.key(256, :base_64).length
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Carl Hicks
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-05 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ prerelease: false
24
+ name: shoulda
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ prerelease: false
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 1
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 1
65
+ version: 1.5.1
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirement: *id004
81
+ description: |-
82
+ An AES encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAES, but it doesn't require building
83
+ native extensions and supports Base64 encoded input and output.
84
+ email: carl.hicks@gmail.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ files:
93
+ - .document
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - aes.gemspec
100
+ - lib/aes.rb
101
+ - test/helper.rb
102
+ - test/test_aes.rb
103
+ has_rdoc: true
104
+ homepage: http://github.com/chicks/aes
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.3.7
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: AES#encrypt(key, data), AES#decrypt(key, data). Capiche?
137
+ test_files:
138
+ - test/helper.rb
139
+ - test/test_aes.rb