wikk_aes_256 0.1.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
+ SHA1:
3
+ metadata.gz: c7070013a13581a329266c4d5bc9a89426a0660c
4
+ data.tar.gz: bac8c27c2069937c888ba76458dac3e82d984753
5
+ SHA512:
6
+ metadata.gz: 4518a7b028a35cab529dc4fed027c7e0528dfef0f0aceea19f7979025b5a653e97b327b59d6636a8a26ce159b505fbf05e63eedc6443510baa238be06557e4f1
7
+ data.tar.gz: 7231b2fb4a7469ca910159efaea9a13b43fc340dcfadb1a3f15c7cd563d25bb063996ca231ac78f9bf851650c60c325d2dae825ca4a0977a0bdfa7d52976eff8
data/History.txt ADDED
@@ -0,0 +1,2 @@
1
+ robertburrowes Mon Jun 20 17:58:30 2016 +1200
2
+ first commit
data/Manifest.txt ADDED
@@ -0,0 +1,5 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/wikk_aes_256.rb
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # wikk_aes
2
+
3
+ * http://wikarekare.github.com/wikk_aes/
4
+ * Source https://github.com/wikarekare/wikk_aes
5
+ * Gem https://rubygems.org/gems/wikk_aes
6
+
7
+ ## DESCRIPTION:
8
+
9
+ Class for AES 256 encryption of text.
10
+
11
+ ## FEATURES/PROBLEMS:
12
+
13
+ * encrypt takes strings or File (IO) objects
14
+ * calls available to base64 encode/pack encrypted output and unencode/unpack before decryption
15
+ * calls to base64 encode key and initial vector, and WIKK::AES256 accepts key_string and iv_string arguments.
16
+
17
+ ## SYNOPSIS:
18
+
19
+ ```
20
+ require "wikk_aes_256"
21
+ aes2 = WIKK::AES_256.new
22
+ File.open("testfile.txt",'r') do |fd|
23
+ @et = aes2.cipher_to_s(fd)
24
+ end
25
+ puts aes2.decrypt(@et, true)
26
+ ```
27
+
28
+ ## REQUIREMENTS:
29
+
30
+
31
+ ## INSTALL:
32
+
33
+ * sudo gem install wikk_aes_256
34
+
35
+ ## LICENSE:
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2016
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ Hoe.plugin :yard
6
+
7
+ Hoe.spec 'wikk_aes_256' do
8
+ self.readme_file = "README.md"
9
+ self.developer( "Rob Burrowes","r.burrowes@auckland.ac.nz")
10
+ remote_rdoc_dir = '' # Release to root
11
+
12
+ self.yard_title = 'wikk_aes_256'
13
+ self.yard_options = ['--markup', 'markdown', '--protected']
14
+ end
15
+
16
+
17
+ #Validate manfest.txt
18
+ #rake check_manifest
19
+
20
+ #Local checking. Creates pkg/
21
+ #rake gem
22
+
23
+ #create doc/
24
+ #rake docs
25
+
26
+ #Copy up to rubygem.org
27
+ #rake release VERSION=1.0.1
@@ -0,0 +1,176 @@
1
+ module WIKK
2
+ require "openssl"
3
+ require 'digest/sha2'
4
+ require 'base64'
5
+
6
+ #Provides AES 256 Encryption, as well as generation of keys and initial vectors, which could be used in other places.
7
+ # @attr_reader [String] plain_text the decrypted text
8
+ # @attr_reader [String] cipher_text the encrypted text
9
+ class AES_256
10
+ VERSION = "0.1.0"
11
+ AES_256_CBC = "AES-256-CBC"
12
+
13
+ attr_reader :plain_text, :cipher_text
14
+
15
+ #Initialize
16
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
17
+ # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
18
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
19
+ # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
20
+ def initialize(key_string = nil, iv_string = nil)
21
+ if(key_string == nil)
22
+ gen_key
23
+ gen_iv
24
+ else
25
+ str_to_key(key_string)
26
+ str_to_iv(iv_string)
27
+ end
28
+ end
29
+
30
+ #Generates a new key using Digest SHA256 in @key.
31
+ # @return [String] Binary string, @key
32
+ def gen_key
33
+ digest = Digest::SHA256.new
34
+ digest.update("symetric key")
35
+ return (@key = digest.digest)
36
+ end
37
+
38
+ # @return [String] base64 version of @key
39
+ def key_to_s
40
+ return [@key].pack('m')
41
+ end
42
+
43
+ # @param [String] turns base64 version of key into AES_256_CBC Symetric Key.
44
+ def str_to_key(base64_keystring)
45
+ return( @key = base64_keystring.unpack('m')[0] )
46
+ end
47
+
48
+ #Generate random AES_256_CBC initialization vector.
49
+ # @return [String] Binary initialization vector @iv
50
+ def gen_iv
51
+ return (@iv = OpenSSL::Cipher::Cipher.new(AES_256_CBC).random_iv)
52
+ end
53
+
54
+ # @return [String] return Base64 version of initialization vector @iv
55
+ def iv_to_s
56
+ return([@iv].pack('m'))
57
+ end
58
+
59
+ # @param [String] turns base64 version of iv into AES_256_CBC initialization vector.
60
+ # @return [Array] AES_256_CBC initialization vector @iv.
61
+ def str_to_iv(base64_iv_string)
62
+ return (@iv = base64_iv_string.unpack('m')[0])
63
+ end
64
+
65
+ # @return [String] base64 version of @key
66
+ # @return [String] return Base64 version of initialization vector @iv
67
+ def key_iv_to_s
68
+ return key_to_s, iv_to_s
69
+ end
70
+
71
+ #Encrypts source using AES 256 CBC, using @key and @iv
72
+ # @param unencrypted_source [String|File]
73
+ # @return [String] Binary string representing encrypted source
74
+ def encrypt(unencrypted_source)
75
+ unencrypted_source = StringIO.new(unencrypted_source) if(unencrypted_source.class == String)
76
+ aes = OpenSSL::Cipher::Cipher.new(AES_256_CBC)
77
+ aes.encrypt
78
+ aes.key = @key
79
+ aes.iv = @iv
80
+ @cipher_text = ""
81
+ while (s = unencrypted_source.read(4096)) != nil do @cipher_text << aes.update(s); end
82
+ @cipher_text << aes.final
83
+ end
84
+
85
+ #Converts encrypted source String, @cipher_text, into Base64 String
86
+ # @param unencrypted_source [String|File] If present, then this source is encrypted, otherwise assumes already encrypted.
87
+ # @return [String] Base64 string representing encrypted source
88
+ def cipher_to_s(unencrypted_source = nil)
89
+ encrypt(unencrypted_source) if(unencrypted_source != nil)
90
+ return [@cipher_text].pack('m')
91
+ end
92
+
93
+ #Decrypts source using AES 256 CBC, using @key and @iv
94
+ # @param encrypted_source [String|File]
95
+ # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
96
+ # @return [String] String representing the original unencypted source
97
+ def decrypt(encrypted_source, base64_source = false)
98
+ encrypted_source = StringIO.new(encrypted_source) if(encrypted_source.class == String)
99
+ read_count = base64_source ? 5464:4096
100
+ decode_cipher = OpenSSL::Cipher::Cipher.new(AES_256_CBC)
101
+ decode_cipher.decrypt
102
+ decode_cipher.key = @key
103
+ decode_cipher.iv = @iv
104
+ @plain_text = ""
105
+ while (et = encrypted_source.read(read_count)) != nil do
106
+ @plain_text << (base64_source ? decode_cipher.update(et.unpack('m')[0]) : decode_cipher.update(et))
107
+ end
108
+ @plain_text << decode_cipher.final
109
+ end
110
+
111
+ #Generates a new key using Digest SHA256 in @key.
112
+ # @return [String] Base64 encoded string, @key
113
+ def self.gen_key_to_s
114
+ aes = self.new
115
+ return aes.key_to_s
116
+ end
117
+
118
+ #Generate random AES_256_CBC initialization vector.
119
+ # @return [String] Base64 encoded initialization vector @iv
120
+ def self.gen_iv_to_s
121
+ aes = self.new
122
+ return aes.iv_to_s
123
+ end
124
+
125
+ #Generates a new key using Digest SHA256 in @key, and random AES_256_CBC initialization vector in @iv
126
+ # @return [String] Base64 encoded string, @key
127
+ # @return [String] Base64 encoded initialization vector @iv
128
+ def self.gen_key_iv_to_s
129
+ aes = self.new
130
+ return aes.key_to_s, aes.iv_to_s
131
+ end
132
+
133
+ #Encrypts source using AES 256 CBC, using @key and @iv
134
+ # @param unencrypted_source [String|File]
135
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
136
+ # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
137
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
138
+ # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
139
+ # @return [String] Binary string representing encrypted source
140
+ # @return [String] base64 key, @key, so later decryption can be done
141
+ # @return [String] base64 initial vector, @iv, so later decryption can be done
142
+ def self.encrypt(unencrypted_source, key_string = nil, iv_string = nil)
143
+ aes = self.new(key_string, iv_string)
144
+ return aes.encrypt(unencrypted_source), aes.key_to_s, aes.iv_to_s
145
+ end
146
+
147
+ #Converts encrypted source String, @cipher_text, into Base64 String
148
+ # @param unencrypted_source [String|File] which must be present, as AES_256 class is created here.
149
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
150
+ # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
151
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
152
+ # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
153
+ # @return [String] Base64 string representing encrypted source
154
+ # @return [String] base64 key, @key, so later decryption can be done
155
+ # @return [String] base64 initial vector, @iv, so later decryption can be done
156
+ def self.cipher_to_s(unencrypted_source, key_string = nil, iv_string = nil)
157
+ aes = self.new(key_string, iv_string)
158
+ return aes.cipher_to_s(unencrypted_source), aes.key_to_s, aes.iv_to_s
159
+ end
160
+
161
+ #Creates an AES class and then Decrypts source using AES 256 CBC, using @key and @iv
162
+ # @param encrypted_source [String|File]
163
+ # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
164
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
165
+ # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
166
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
167
+ # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
168
+ # @return [String] String representing the original unencypted source
169
+ def self.decrypt(encrypted_source, base64_source=false, key_string = nil, iv_string = nil)
170
+ aes = self.new(key_string, iv_string)
171
+ return aes.decrypt(encrypted_source, base64_source)
172
+ end
173
+ end
174
+ end
175
+
176
+
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wikk_aes_256
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Burrowes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hoe-yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.15'
41
+ description: Class for AES 256 encryption of text.
42
+ email:
43
+ - r.burrowes@auckland.ac.nz
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.md
50
+ files:
51
+ - History.txt
52
+ - Manifest.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/wikk_aes_256.rb
56
+ homepage: http://wikarekare.github.com/wikk_aes/
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options:
62
+ - "--markup"
63
+ - markdown
64
+ - "--protected"
65
+ - "--title"
66
+ - wikk_aes_256
67
+ - "--quiet"
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Class for AES 256 encryption of text.
86
+ test_files: []