wikk_aes_256 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +5 -5
  2. data/History.txt +34 -0
  3. data/README.md +4 -4
  4. data/Rakefile +13 -15
  5. data/lib/wikk_aes_256.rb +126 -106
  6. metadata +9 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d15a628c0fd990371636a7d2e066768dc71a3fdd
4
- data.tar.gz: 3becb7b50ffe9eb2c1aa1d29a424e67bb44de4d9
2
+ SHA256:
3
+ metadata.gz: 5dd425fdcb527392c69b96465b764aab84ef72a8f70420a98400ae7d6aea894b
4
+ data.tar.gz: 8e993f98f23cc6a309dbd5da231d7ef0cac18f6fac9941a5136ee4678e988f63
5
5
  SHA512:
6
- metadata.gz: e5c3b086b36a0124de016622518ad9d15ae20132bc27e7b9a04890d9d41350ff71618a92feae34fef8a956e53f6a0c8f98768237306c9da4473015e9b0071366
7
- data.tar.gz: 28cc7ba2ec932b8a2cbcab44fe00a8048a87bfc20202d8dd2b4218dcd89165f2730ed1af4eddd8266b64525b7db3b5ebf38c77c63b912be974ce4690c2a37210
6
+ metadata.gz: dcfe57a6287556cfe0b1414fa2c8e417544e0e2f00ed159d2f889c097ca6c73512c552f510b3da7022fae48efbbef2cb25d4bb7761331dd4c8914acf14777c1d
7
+ data.tar.gz: bc83fcf0f6f2fefe986f2a194a5e8582cb410de4c1d006ea61a3179127102e23a6773f323ff927474743bebb7707bab720cf057edf40c4fafb675da3af702d71
data/History.txt CHANGED
@@ -1,3 +1,37 @@
1
+ robertburrowes Sat Apr 15 17:33:00 2023 +1200
2
+ Use SecureRandom for generating the key, if none given.
3
+ robertburrowes Sat Apr 15 17:30:45 2023 +1200
4
+ show encrypted text too.
5
+ robertburrowes Sun Jun 5 18:46:28 2022 +1200
6
+ ensure we use bash
7
+ robertburrowes Sun Jun 5 18:21:48 2022 +1200
8
+ #{PROJECT} release 0.1.7
9
+ robertburrowes Sun Jun 5 18:18:15 2022 +1200
10
+ left out require stringio
11
+ robertburrowes Sun Oct 25 22:11:06 2020 +1300
12
+ Tidy up yard doc comments
13
+ robertburrowes Sun Oct 25 17:45:35 2020 +1300
14
+ Chdir to cd as Mac doesn't look to have chdir in sh
15
+ robertburrowes Sun Oct 25 13:51:16 2020 +1300
16
+ New hoe layout
17
+ robertburrowes Sun Oct 25 13:50:46 2020 +1300
18
+ formatting
19
+ robertburrowes Sun Oct 25 13:50:30 2020 +1300
20
+ now including this in repo
21
+ robertburrowes Sun Oct 25 13:50:02 2020 +1300
22
+ Move dev shell scripts into sbin
23
+ robertburrowes Tue Apr 14 21:21:06 2020 +1200
24
+ moved test data to its own dir
25
+ robertburrowes Tue Apr 14 14:32:08 2020 +1200
26
+ mv test data to test/data
27
+ robertburrowes Mon Apr 13 22:55:45 2020 +1200
28
+ Add __dir__ path to access testfile
29
+ robertburrowes Tue Apr 10 11:53:52 2018 +1200
30
+ Inc Version
31
+ robertburrowes Tue Apr 10 11:53:43 2018 +1200
32
+ Inc version
33
+ robertburrowes Tue Apr 10 11:53:33 2018 +1200
34
+ Inc version
1
35
  robertburrowes Tue Apr 10 11:50:22 2018 +1200
2
36
  OpenSSL::Cipher::Cipher.new deprecated in favour of OpenSSL::Cipher.new
3
37
  robertburrowes Fri Jun 24 19:56:31 2016 +1200
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # wikk_aes
1
+ # wikk_aes_256
2
2
 
3
- * http://wikarekare.github.com/wikk_aes/
4
- * Source https://github.com/wikarekare/wikk_aes
5
- * Gem https://rubygems.org/gems/wikk_aes
3
+ * Docs :: https://wikarekare.github.io/wikk_aes_256/
4
+ * Source :: https://github.com/wikarekare/wikk_aes_256
5
+ * Gem :: https://rubygems.org/gems/wikk_aes_256
6
6
 
7
7
  ## DESCRIPTION:
8
8
 
data/Rakefile CHANGED
@@ -1,27 +1,25 @@
1
1
  # -*- ruby -*-
2
-
3
2
  require 'rubygems'
4
3
  require 'hoe'
5
4
  Hoe.plugin :yard
6
5
 
7
- Hoe.spec 'wikk_aes_256' do
8
- self.readme_file = "README.md"
9
- self.developer( "Rob Burrowes","r.burrowes@auckland.ac.nz")
6
+ Hoe.spec 'wikk_aes_256' do
7
+ self.readme_file = 'README.md'
8
+ self.developer( 'Rob Burrowes', 'r.burrowes@auckland.ac.nz')
10
9
  remote_rdoc_dir = '' # Release to root
11
-
10
+
12
11
  self.yard_title = 'wikk_aes_256'
13
- self.yard_options = ['--markup', 'markdown', '--protected']
12
+ self.yard_options = [ '--markup', 'markdown', '--protected' ]
14
13
  end
15
14
 
15
+ # Validate manfest.txt
16
+ # rake check_manifest
16
17
 
17
- #Validate manfest.txt
18
- #rake check_manifest
19
-
20
- #Local checking. Creates pkg/
21
- #rake gem
18
+ # Local checking. Creates pkg/
19
+ # rake gem
22
20
 
23
- #create doc/
24
- #rake docs
21
+ # create doc/
22
+ # rake docs
25
23
 
26
- #Copy up to rubygem.org
27
- #rake release VERSION=1.0.1
24
+ # Copy up to rubygem.org
25
+ # rake release VERSION=1.0.1
data/lib/wikk_aes_256.rb CHANGED
@@ -1,179 +1,199 @@
1
- module WIKK
2
- require "openssl"
3
- require 'digest/sha2'
4
- require 'base64'
1
+ require 'openssl'
2
+ require 'securerandom'
3
+ require 'base64'
4
+ require 'stringio'
5
5
 
6
- #Provides AES 256 Encryption, as well as generation of keys and initial vectors, which could be used in other places.
6
+ # Stay in our own namespace
7
+ module WIKK
8
+ # Provides AES 256 Encryption, as well as generation of keys and initial vectors, which could be used in other places.
9
+ #
7
10
  # @attr_reader [String] plain_text the decrypted text
8
11
  # @attr_reader [String] cipher_text the encrypted text
9
12
  class AES_256
10
- VERSION = "0.1.6"
11
- AES_256_CBC = "AES-256-CBC"
12
-
13
+ VERSION = '0.1.8'
14
+ AES_256_CBC = 'AES-256-CBC'
15
+
13
16
  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
+
18
+ # Initialize
19
+ #
20
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
17
21
  # 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
22
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
19
23
  # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
20
24
  def initialize(key_string = nil, iv_string = nil)
21
- if key_string == nil
22
- gen_key
25
+ if key_string.nil?
26
+ gen_key(key_length: 32)
23
27
  else
24
28
  str_to_key(key_string)
25
29
  end
26
30
 
27
- if iv_string == nil
31
+ if iv_string.nil?
28
32
  gen_iv
29
33
  else
30
34
  str_to_iv(iv_string)
31
35
  end
32
36
  end
33
-
34
- #Generates a new key using Digest SHA256 in @key.
35
- # @return [String] Binary string, @key
36
- def gen_key
37
- digest = Digest::SHA256.new
38
- digest.update("symetric key")
39
- return (@key = digest.digest)
37
+
38
+ # Generates a new binary key in @key, using SecureRandom.
39
+ #
40
+ # @return [String] Binary string, @key
41
+ def gen_key(key_length: 32)
42
+ @key = SecureRandom.gen_random(key_length)
40
43
  end
41
-
42
- # @return [String] base64 version of @key
44
+
45
+ # Convert key to a base64 string
46
+ #
47
+ # @return [String] base64 version of @key
43
48
  def key_to_s
44
- return [@key].pack('m').chomp
49
+ return [ @key ].pack('m').chomp
45
50
  end
46
51
 
47
- # @param [String] turns base64 version of key into AES_256_CBC Symetric Key.
52
+ # Convert a base64 string into a key
53
+ #
54
+ # @param [String] converts base64 version of key into AES_256_CBC Symetric Key.
55
+ # @return [String] Binary string, @key
48
56
  def str_to_key(base64_keystring)
49
- return( @key = base64_keystring.unpack('m')[0] )
57
+ return( @key = base64_keystring.unpack1('m') )
50
58
  end
51
59
 
52
- #Generate random AES_256_CBC initialization vector.
53
- # @return [String] Binary initialization vector @iv
60
+ # Generate random AES_256_CBC initialization vector.
61
+ #
62
+ # @return [String] Binary initialization vector @iv
54
63
  def gen_iv
55
64
  return (@iv = OpenSSL::Cipher.new(AES_256_CBC).random_iv)
56
- end
65
+ end
57
66
 
58
- # @return [String] return Base64 version of initialization vector @iv
67
+ # Convert initialization vector to base64 string
68
+ #
69
+ # @return [String] return Base64 version of initialization vector @iv
59
70
  def iv_to_s
60
- return([@iv].pack('m')).chomp
71
+ return [ @iv ].pack('m').chomp
61
72
  end
62
-
63
- # @param [String] turns base64 version of iv into AES_256_CBC initialization vector.
64
- # @return [Array] AES_256_CBC initialization vector @iv.
73
+
74
+ # Convert base64 string into an initialization vector
75
+ #
76
+ # @param [String] turns base64 version of iv into AES_256_CBC initialization vector.
77
+ # @return [Array] AES_256_CBC initialization vector @iv.
65
78
  def str_to_iv(base64_iv_string)
66
- return (@iv = base64_iv_string.unpack('m')[0])
79
+ return (@iv = base64_iv_string.unpack1('m'))
67
80
  end
68
81
 
69
- # @return [String] base64 version of @key
70
- # @return [String] return Base64 version of initialization vector @iv
82
+ # Convert key and the initialization vector into base64 strings
83
+ #
84
+ # @return [String,String] base64 version of @key;
85
+ # Base64 version of initialization vector @iv
71
86
  def key_iv_to_s
72
87
  return key_to_s, iv_to_s
73
88
  end
74
-
75
- #Encrypts source using AES 256 CBC, using @key and @iv
76
- # @param unencrypted_source [String|File]
77
- # @return [String] Binary string representing encrypted source
89
+
90
+ # Encrypts source using AES 256 CBC, using @key and @iv
91
+ #
92
+ # @param unencrypted_source [String|File]
93
+ # @return [String] Binary string representing encrypted source
78
94
  def encrypt(unencrypted_source)
79
- unencrypted_source = StringIO.new(unencrypted_source) if(unencrypted_source.class == String)
95
+ unencrypted_source = StringIO.new(unencrypted_source) if unencrypted_source.instance_of?(String)
80
96
  aes = OpenSSL::Cipher.new(AES_256_CBC)
81
97
  aes.encrypt
82
98
  aes.key = @key
83
99
  aes.iv = @iv
84
- @cipher_text = ""
100
+ @cipher_text = ''
85
101
  while (s = unencrypted_source.read(4096)) != nil do @cipher_text << aes.update(s); end
86
102
  @cipher_text << aes.final
87
103
  end
88
-
89
- #Converts encrypted source String, @cipher_text, into Base64 String
90
- # @param unencrypted_source [String|File] If present, then this source is encrypted, otherwise assumes already encrypted.
91
- # @return [String] Base64 string representing encrypted source
104
+
105
+ # Converts encrypted source String, @cipher_text, into Base64 String
106
+ #
107
+ # @param unencrypted_source [String|File] If present, then this source is encrypted, otherwise assumes already encrypted.
108
+ # @return [String] Base64 string representing encrypted source
92
109
  def cipher_to_s(unencrypted_source = nil)
93
- encrypt(unencrypted_source) if(unencrypted_source != nil)
94
- return [@cipher_text].pack('m').chomp
110
+ encrypt(unencrypted_source) if unencrypted_source != nil
111
+ return [ @cipher_text ].pack('m').chomp
95
112
  end
96
113
 
97
- #Decrypts source using AES 256 CBC, using @key and @iv
98
- # @param encrypted_source [String|File]
99
- # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
100
- # @return [String] String representing the original unencypted source
114
+ # Decrypts source using AES 256 CBC, using @key and @iv
115
+ #
116
+ # @param encrypted_source [String|File]
117
+ # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
118
+ # @return [String] String representing the original unencypted source
101
119
  def decrypt(encrypted_source, base64_source = false)
102
- encrypted_source = StringIO.new(encrypted_source) if(encrypted_source.class == String)
103
- read_count = base64_source ? 5464:4096
120
+ encrypted_source = StringIO.new(encrypted_source) if encrypted_source.instance_of?(String)
121
+ read_count = base64_source ? 5464 : 4096
104
122
  decode_cipher = OpenSSL::Cipher.new(AES_256_CBC)
105
123
  decode_cipher.decrypt
106
124
  decode_cipher.key = @key
107
125
  decode_cipher.iv = @iv
108
- @plain_text = ""
109
- while (et = encrypted_source.read(read_count)) != nil do
110
- @plain_text << (base64_source ? decode_cipher.update(et.unpack('m')[0]) : decode_cipher.update(et))
126
+ @plain_text = ''
127
+ while (et = encrypted_source.read(read_count)) != nil
128
+ @plain_text << (base64_source ? decode_cipher.update(et.unpack1('m')) : decode_cipher.update(et))
111
129
  end
112
130
  @plain_text << decode_cipher.final
113
131
  end
114
-
115
- #Generates a new key using Digest SHA256 in @key.
116
- # @return [String] Base64 encoded string, @key
117
- def self.gen_key_to_s
118
- digest = Digest::SHA256.new
119
- digest.update("symetric key")
120
- return ([digest.digest].pack('m')).chomp
121
- end
122
-
123
- #Generate random AES_256_CBC initialization vector.
124
- # @return [String] Base64 encoded initialization vector @iv
125
- def self.gen_iv_to_s
126
- return ([OpenSSL::Cipher.new(AES_256_CBC).random_iv].pack('m')).chomp
127
- end
128
-
129
- #Generates a new key using Digest SHA256 in @key, and random AES_256_CBC initialization vector in @iv
130
- # @return [String] Base64 encoded string, @key
131
- # @return [String] Base64 encoded initialization vector @iv
132
+
133
+ # Generates a random base64 key.
134
+ #
135
+ # @return [String] Base64 encoded string, @key
136
+ def self.gen_key_to_s(key_length: 32)
137
+ SecureRandom.base64(key_length)
138
+ end
139
+
140
+ # Generate random AES_256_CBC initialization vector.
141
+ #
142
+ # @return [String] Base64 encoded initialization vector @iv
143
+ def self.gen_iv_to_s
144
+ return [ OpenSSL::Cipher.new(AES_256_CBC).random_iv ].pack('m').chomp
145
+ end
146
+
147
+ # Generates a new key using Random string in @key, and random AES_256_CBC initialization vector in @iv
148
+ #
149
+ # @return [String,String] Base64 encoded string, @key;
150
+ # Base64 encoded initialization vector @iv
132
151
  def self.gen_key_iv_to_s
133
- return self.gen_key_to_s, self.gen_iv_to_s
152
+ return self.gen_key_to_s, self.gen_iv_to_s
134
153
  end
135
-
136
- #Encrypts source using AES 256 CBC, using @key and @iv
137
- # @param unencrypted_source [String|File]
138
- # @param key_string [String] optional base64 key to be used in encryption or decryption.
154
+
155
+ # Encrypts source using AES 256 CBC, using @key and @iv
156
+ #
157
+ # @param unencrypted_source [String|File]
158
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
139
159
  # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
140
- # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
160
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
141
161
  # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
142
- # @return [String] Binary string representing encrypted source
143
- # @return [String] base64 key, @key, so later decryption can be done
144
- # @return [String] base64 initial vector, @iv, so later decryption can be done
162
+ # @return [String,String,String] Binary string representing encrypted source;
163
+ # base64 key, @key, so later decryption can be done;
164
+ # base64 initial vector, @iv, so later decryption can be done
145
165
  def self.encrypt(unencrypted_source, key_string = nil, iv_string = nil)
146
166
  aes = self.new(key_string, iv_string)
147
- return aes.encrypt(unencrypted_source), aes.key_to_s, aes.iv_to_s
167
+ return aes.encrypt(unencrypted_source), aes.key_to_s, aes.iv_to_s
148
168
  end
149
-
150
- #Converts encrypted source String, @cipher_text, into Base64 String
151
- # @param unencrypted_source [String|File] which must be present, as AES_256 class is created here.
152
- # @param key_string [String] optional base64 key to be used in encryption or decryption.
169
+
170
+ # Converts encrypted source String, @cipher_text, into Base64 String
171
+ #
172
+ # @param unencrypted_source [String|File] which must be present, as AES_256 class is created here.
173
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
153
174
  # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
154
- # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
175
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
155
176
  # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
156
- # @return [String] Base64 string representing encrypted source
157
- # @return [String] base64 key, @key, so later decryption can be done
158
- # @return [String] base64 initial vector, @iv, so later decryption can be done
177
+ # @return [String,String,String] Base64 string representing encrypted source;
178
+ # base64 key, @key, so later decryption can be done;
179
+ # base64 initial vector, @iv, so later decryption can be done
159
180
  def self.cipher_to_s(unencrypted_source, key_string = nil, iv_string = nil)
160
181
  aes = self.new(key_string, iv_string)
161
- return aes.cipher_to_s(unencrypted_source), aes.key_to_s, aes.iv_to_s
182
+ return aes.cipher_to_s(unencrypted_source), aes.key_to_s, aes.iv_to_s
162
183
  end
163
-
164
- #Creates an AES class and then Decrypts source using AES 256 CBC, using @key and @iv
165
- # @param encrypted_source [String|File]
166
- # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
167
- # @param key_string [String] optional base64 key to be used in encryption or decryption.
184
+
185
+ # Creates an AES class and then Decrypts source using AES 256 CBC, using @key and @iv
186
+ #
187
+ # @param encrypted_source [String|File]
188
+ # @param base64_source [Boolean] if true, then source is assumed to be base64 encoded.
189
+ # @param key_string [String] optional base64 key to be used in encryption or decryption.
168
190
  # if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()
169
- # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
191
+ # @param iv_string [String ] optional base64 iv (initial vector) to be used in the encryption or decryption
170
192
  # Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().
171
- # @return [String] String representing the original unencypted source
172
- def self.decrypt(encrypted_source, base64_source=false, key_string = nil, iv_string = nil)
193
+ # @return [String] String representing the original unencypted source
194
+ def self.decrypt(encrypted_source, base64_source = false, key_string = nil, iv_string = nil)
173
195
  aes = self.new(key_string, iv_string)
174
196
  return aes.decrypt(encrypted_source, base64_source)
175
197
  end
176
198
  end
177
199
  end
178
-
179
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikk_aes_256
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Burrowes
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-10 00:00:00.000000000 Z
11
+ date: 2023-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hoe-yard
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.16'
33
+ version: '3.25'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.16'
40
+ version: '3.25'
41
41
  description: Class for AES 256 encryption of text.
42
42
  email:
43
43
  - r.burrowes@auckland.ac.nz
@@ -53,11 +53,11 @@ files:
53
53
  - README.md
54
54
  - Rakefile
55
55
  - lib/wikk_aes_256.rb
56
- homepage: http://wikarekare.github.com/wikk_aes/
56
+ homepage: https://wikarekare.github.io/wikk_aes_256/
57
57
  licenses:
58
58
  - MIT
59
59
  metadata: {}
60
- post_install_message:
60
+ post_install_message:
61
61
  rdoc_options:
62
62
  - "--markup"
63
63
  - markdown
@@ -78,9 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.6.13
83
- signing_key:
81
+ rubygems_version: 3.3.7
82
+ signing_key:
84
83
  specification_version: 4
85
84
  summary: Class for AES 256 encryption of text.
86
85
  test_files: []