aes-new 0

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
+ SHA256:
3
+ metadata.gz: f9dc45539edc533dac1b381db8f4f1903a7f447646ed1396343a4d1612c0f366
4
+ data.tar.gz: b3410c2bfc427cb1e1845004aeafa0c31cf4fecb52bd466f9064379ce12bad03
5
+ SHA512:
6
+ metadata.gz: 45604087919b409f311158502679f4f839a63a9e158d01b9bda21c72bae630c907549546be03b67a989e1a8da56c4b472b1a23d76c92d7366e7bac03dc82f85f
7
+ data.tar.gz: 89bee0f3d86947290a67c2b5ba16c39a93708164723c618394c0d9736b99f98faaf93ad8bd39972c452ae79bc386ced37e60c47e4370511db33500ffdc7fcf6d
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 AesNew encrypt/decrypt gem built on top of OpenSSL. Not as quick as FastAesNew 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 = AesNew.key
11
+ => "290c3c5d812a4ba7ce33adf09598a462"
12
+
13
+ # Encrypt a string. Default output is base_64 encoded, init_vector and cipher_text are joined with "$"
14
+ b64 = AesNew.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 = AesNew.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 = AesNew.iv(:base_64)
24
+ => "IJjbgbv/OvPIAf4R5qAWyg=="
25
+
26
+ # Encrypt a string, with a provided key and init_vector.
27
+ b64_iv = AesNew.encrypt("A super secret message", key, {:iv => iv})
28
+ => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="
29
+
30
+ AesNew.decrypt(b64, key)
31
+ => "A super secret message"
32
+
33
+ AesNew.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 = AesNew.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
+ AesNew.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,40 @@
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-new"
16
+ gem.homepage = "https://github.com/enotikalt/aes-new"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{AesNew#encrypt(key, data), AesNew#decrypt(key, data). Capiche?}
19
+ gem.description = %Q{An AesNew encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAesNew, 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
+ task :default => :test
31
+
32
+ require 'rdoc/task'
33
+ Rake::RDocTask.new do |rdoc|
34
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
35
+
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = "aes #{version}"
38
+ rdoc.rdoc_files.include('README*')
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6
data/aes_new.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-new"
8
+ s.version = "0.6"
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 = "2020-09-30"
13
+ s.description = "An AesNew encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAesNew, 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_new.gemspec",
27
+ "lib/aes_new.rb",
28
+ "lib/aes/aes_new.rb",
29
+ ]
30
+ s.homepage = "https://github.com/enotikalt/aes-new"
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = "1.8.12"
34
+ s.summary = "AesNew#encrypt(key, data), AesNew#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
+
@@ -0,0 +1,158 @@
1
+ module AesNew
2
+ class << self
3
+ # Encrypts the plain_text with the provided key
4
+ def encrypt(plain_text, key, opts={})
5
+ ::AesNew::AesNew.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
+ ::AesNew::AesNew.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 = ::AesNew::AesNew.new("").random_key(length)
15
+ case format
16
+ when :base_64
17
+ Base64.urlsafe_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 = ::AesNew::AesNew.new("").random_iv
26
+ case format
27
+ when :base_64
28
+ Base64.urlsafe_encode64(iv).chomp
29
+ else
30
+ iv
31
+ end
32
+ end
33
+ end
34
+
35
+ class AesNew
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.urlsafe_decode64(part)
108
+ end
109
+ iv_and_ctext
110
+ end
111
+
112
+ # Base64 Encodes a string
113
+ def b64_e(data)
114
+ Base64.urlsafe_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 => "AesNew-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.urlsafe_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.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
data/lib/aes_new.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'aes/aes'
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aes-new
3
+ version: !ruby/object:Gem::Version
4
+ version: '0'
5
+ platform: ruby
6
+ authors:
7
+ - Carl Hicks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-05 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 AesNew encrypt/decrypt gem built ontop of OpenSSL. Not as quick as FastAesNew, but it doesn't require building
71
+ native extensions and supports Base64 encoded input and output.
72
+ email: carl.hicks@gmail.com
73
+ executables: []
74
+ extensions: []
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_new.gemspec
86
+ - lib/aes/aes_new.rb
87
+ - lib/aes_new.rb
88
+ homepage: https://github.com/enotikalt/aes-new
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.2.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: AesNew#encrypt(key, data), AesNew#decrypt(key, data). Capiche?
111
+ test_files: []