ruby-aes-table2 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ = 0.1
2
+
3
+ === 2007-07-30
4
+ * Initial release: RAA project import
5
+ * Added a C extension for speed along the 6 other versions (see README)
6
+ * Minor changes in the API
data/COPYING ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Alex Boussinet
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 ADDED
@@ -0,0 +1,77 @@
1
+ README for ruby-aes
2
+ ===================
3
+
4
+ Ruby AES <http://rubyforge.org/projects/ruby-aes> is an implementation of the Rijndael algorithm.
5
+
6
+ Written by Alex Boussinet <mailto:alex.boussinet@gmail.com>
7
+
8
+ This release is mainly an import from the Ruby Application Archive (RAA).
9
+ I've added all the versions I was working on (algorithm variations) and a new
10
+ C extension for improved performance.
11
+ 6 variations are available:
12
+
13
+ * "Normal":
14
+ Pure Ruby implementation of the Rijndael algorithm specifications.
15
+ Useful for understanding the algorithm.
16
+
17
+ * "Optimized":
18
+ Pure Ruby implementation based on the "Normal" code but optimized for speed.
19
+ The SubBytes and ShiftRows methods have been combined.
20
+
21
+ * "Table Optimized 1":
22
+ Pure Ruby implementation based on the C code from the Rijndael website.
23
+ The arrays of constants are bigger because all the operations are
24
+ already computed so it's mainly based on table look ups.
25
+
26
+ * "Table Optimized 2":
27
+ Pure Ruby implementation based on the "Table Optimized 1" code.
28
+ The arrays of constants are bigger because all the operations are
29
+ already computed and table look ups are also combined.
30
+
31
+ * "Table Unroll Optimized 1":
32
+ Pure Ruby implementation based on the "Table Optimized 1" code.
33
+ The change here is that the loops are unrolled.
34
+
35
+ * "Table Unroll Optimized 2":
36
+ Pure Ruby implementation based on the "Table Optimized 2" code.
37
+ The change here is that the loops are unrolled.
38
+
39
+ * "EXT Table Unroll Optimized 2":
40
+ C extension based on the "Table Unroll Optimized 2" code.
41
+ This extension is provided for major speed improvement.
42
+
43
+ All those variations share the same API:
44
+ Default key_length: 128
45
+ Default mode: 'ECB'
46
+ Default IV: 16 null chars ("00" * 16 in hex format)
47
+ Default key: 16 null chars ("00" * 16 in hex format)
48
+ Default input text: "PLAINTEXT"
49
+
50
+ Aes.check_key(key_string, key_length)
51
+ Aes.check_iv(iv_string)
52
+ Aes.check_kl(key_length)
53
+ Aes.check_mode(mode)
54
+ Aes.init(key_length, mode, key, iv)
55
+ Aes.encrypt_block(key_length, mode, key, iv, block) # no padding
56
+ Aes.decrypt_block(key_length, mode, key, iv, block) # no padding
57
+ Aes.encrypt_buffer(key_length, mode, key, iv, block) # padding
58
+ Aes.decrypt_buffer(key_length, mode, key, iv, block) # padding
59
+ Aes.encrypt_stream(key_length, mode, key, iv, sin, sout)
60
+ Aes.decrypt_stream(key_length, mode, key, iv, sin, sout)
61
+ Aes.bs() # block size for read operations (stream)
62
+ Aes.bs=(bs)
63
+
64
+ Valid modes are:
65
+ * ECB (Electronic Code Book)
66
+ * CBC (Cipher Block Chaining)
67
+ * OFB (Output Feedback)
68
+ * CFB (Cipher Feedback)
69
+
70
+ Valid key length:
71
+ * 128 bits
72
+ * 192 bits
73
+ * 256 bits
74
+
75
+ For a really good encryption, 256 bits CBC is recommanded.
76
+
77
+ For more information on AES-Rijndael, see: <http://csrc.nist.gov/encryption/aes/rijndael/>
data/Rakefile ADDED
@@ -0,0 +1,154 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'fileutils'
7
+
8
+ include FileUtils
9
+
10
+ @name = 'ruby-aes'
11
+ @version = '1.0'
12
+ @native = false
13
+
14
+ @lib = "lib/#{@name}"
15
+ @ext = "ext/#{@name}"
16
+ @ext_o = 'aes_alg.o'
17
+ @ext_so = "aes_alg.#{Config::CONFIG['DLEXT']}"
18
+
19
+ RDOC_OPTS = ['--quiet',
20
+ '--title', 'ruby-aes reference',
21
+ '--opname', 'index.html',
22
+ '--exclude', 'ext',
23
+ '--line-numbers',
24
+ '--main', 'README',
25
+ '--inline-source']
26
+
27
+ CLEAN.include [
28
+ '**/.*.sw?', '*.gem', '.config', '**/.DS_Store',
29
+ "#{@ext}/#{@ext_so}", "#{@ext}/#{@ext_o}",
30
+ "#{@ext}/Makefile", "#{@ext}/aes_cons.h", "#{@ext}/mkmf.log",
31
+ "#{@lib}/aes_alg.rb", "#{@lib}/aes_cons.rb", "#{@lib}/aes_gencons.rb"
32
+ ]
33
+
34
+ SPEC = Gem::Specification.new do |s|
35
+ s.name = @name
36
+ s.version = @version
37
+ s.platform = Gem::Platform::RUBY
38
+ s.has_rdoc = true
39
+ s.rdoc_options += RDOC_OPTS
40
+ s.extra_rdoc_files = ['README', 'CHANGELOG', 'COPYING']
41
+ s.summary = 'ruby-aes is an implementation of the Rijndael algorithm (AES)'
42
+ s.description = s.summary
43
+ s.author = 'Alex Boussinet'
44
+ s.email = 'alex.boussinet@gmail.com'
45
+ s.homepage = "http://#{@name}.rubyforge.org"
46
+ s.rubyforge_project = @name
47
+ s.test_files = FileList['test/test_*.rb']
48
+ s.require_paths = ['lib']
49
+ # s.bindir = 'bin'
50
+ s.files = %w(CHANGELOG COPYING README Rakefile) +
51
+ Dir.glob('{doc,examples,lib,test}/**/*')
52
+ end
53
+
54
+ def task_gem
55
+ desc 'Build the gem'
56
+ Rake::GemPackageTask.new(SPEC) do |p|
57
+ p.need_tar = true
58
+ p.gem_spec = SPEC
59
+ end
60
+ end
61
+
62
+ Dir.glob('extras/*').each do |project|
63
+ desc "Specify the project to use"
64
+ task File.basename(project).to_sym do |t|
65
+ @type = t.name
66
+
67
+ @gem_name = "#{@name}-#{@type}"
68
+ SPEC.name = @gem_name
69
+ SPEC.files += [ "#{@lib}/aes_alg.rb", "#{@lib}/aes_cons.rb" ]
70
+ task_gem
71
+ end
72
+ end
73
+
74
+ desc "Specify the project to use"
75
+ task :cext do |t|
76
+ @type = t.name
77
+
78
+ @gem_name = "#{@name}-#{@type}"
79
+ SPEC.name = @gem_name
80
+ SPEC.require_paths += ['ext']
81
+ if @native
82
+ SPEC.files += ["#{@ext}/#{@ext_so}"]
83
+ SPEC.platform = Gem::Platform::CURRENT
84
+ else
85
+ SPEC.files += Dir.glob("#{@ext}/*")
86
+ SPEC.extensions = FileList["#{@ext}/extconf.rb"].to_a
87
+ end
88
+ task_gem
89
+ end
90
+ desc "Use the native version of cext"
91
+ task :native do
92
+ @native = true
93
+ Rake::Task[:cext].invoke
94
+ end
95
+
96
+ task :prepare do
97
+ if @type == 'cext'
98
+ Dir.chdir(@ext) do
99
+ ruby 'aes_gencons.rb'
100
+ if @native
101
+ ruby 'extconf.rb'
102
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
103
+ end
104
+ end
105
+ else
106
+ cp "extras/#{@type}/aes_alg.rb", "#{@lib}/"
107
+ cp "extras/#{@type}/aes_gencons.rb", "#{@lib}/"
108
+ Dir.chdir(@lib) do
109
+ ruby 'aes_gencons.rb'
110
+ rm_f 'aes_gencons.rb'
111
+ end
112
+ end
113
+ end
114
+
115
+ task :package => [:clean, :prepare, :rerdoc]
116
+
117
+ task :default do
118
+ STDERR.puts <<-EOM
119
+ You must call rake with one of this task as first param:
120
+ normal
121
+ optimized
122
+ table1
123
+ table2
124
+ unroll1
125
+ unroll2
126
+ cext
127
+ native (imply cext)
128
+ EOM
129
+ end
130
+
131
+ desc 'Run all the tests'
132
+ Rake::TestTask.new do |t|
133
+ t.libs << "test"
134
+ t.test_files = FileList['test/test_*.rb']
135
+ t.verbose = true
136
+ end
137
+
138
+ desc 'Build the documentation'
139
+ Rake::RDocTask.new do |rdoc|
140
+ rdoc.rdoc_dir = 'doc/rdoc'
141
+ rdoc.options += RDOC_OPTS
142
+ rdoc.main = 'README'
143
+ rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb']
144
+ end
145
+
146
+ desc 'Install the package'
147
+ task :install do |t|
148
+ sh %{sudo gem install pkg/#{@gem_name}}
149
+ end
150
+
151
+ desc 'Uninstall the package'
152
+ task :uninstall do
153
+ sh %{sudo gem uninstall #{@gem_name}}
154
+ end
@@ -0,0 +1,494 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: Aes</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">Aes</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/ruby-aes_rb.html">
59
+ lib/ruby-aes.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+
76
+
77
+ </div>
78
+
79
+ <div id="method-list">
80
+ <h3 class="section-bar">Methods</h3>
81
+
82
+ <div class="name-list">
83
+ <a href="#M000014">bs</a>&nbsp;&nbsp;
84
+ <a href="#M000015">bs=</a>&nbsp;&nbsp;
85
+ <a href="#M000017">check_iv</a>&nbsp;&nbsp;
86
+ <a href="#M000016">check_key</a>&nbsp;&nbsp;
87
+ <a href="#M000019">check_kl</a>&nbsp;&nbsp;
88
+ <a href="#M000018">check_mode</a>&nbsp;&nbsp;
89
+ <a href="#M000022">decrypt_block</a>&nbsp;&nbsp;
90
+ <a href="#M000024">decrypt_buffer</a>&nbsp;&nbsp;
91
+ <a href="#M000026">decrypt_stream</a>&nbsp;&nbsp;
92
+ <a href="#M000021">encrypt_block</a>&nbsp;&nbsp;
93
+ <a href="#M000023">encrypt_buffer</a>&nbsp;&nbsp;
94
+ <a href="#M000025">encrypt_stream</a>&nbsp;&nbsp;
95
+ <a href="#M000020">init</a>&nbsp;&nbsp;
96
+ </div>
97
+ </div>
98
+
99
+ </div>
100
+
101
+
102
+ <!-- if includes -->
103
+
104
+ <div id="section">
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ <!-- if method_list -->
114
+ <div id="methods">
115
+ <h3 class="section-bar">Public Class methods</h3>
116
+
117
+ <div id="method-M000014" class="method-detail">
118
+ <a name="M000014"></a>
119
+
120
+ <div class="method-heading">
121
+ <a href="#M000014" class="method-signature">
122
+ <span class="method-name">bs</span><span class="method-args">()</span>
123
+ </a>
124
+ </div>
125
+
126
+ <div class="method-description">
127
+ <p><a class="source-toggle" href="#"
128
+ onclick="toggleCode('M000014-source');return false;">[Source]</a></p>
129
+ <div class="method-source-code" id="M000014-source">
130
+ <pre>
131
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 45</span>
132
+ 45: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">bs</span>(); <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@@bs</span> <span class="ruby-keyword kw">end</span>
133
+ </pre>
134
+ </div>
135
+ </div>
136
+ </div>
137
+
138
+ <div id="method-M000015" class="method-detail">
139
+ <a name="M000015"></a>
140
+
141
+ <div class="method-heading">
142
+ <a href="#M000015" class="method-signature">
143
+ <span class="method-name">bs=</span><span class="method-args">(bs)</span>
144
+ </a>
145
+ </div>
146
+
147
+ <div class="method-description">
148
+ <p><a class="source-toggle" href="#"
149
+ onclick="toggleCode('M000015-source');return false;">[Source]</a></p>
150
+ <div class="method-source-code" id="M000015-source">
151
+ <pre>
152
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 46</span>
153
+ 46: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">bs=</span>(<span class="ruby-identifier">bs</span>); <span class="ruby-ivar">@@bs</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>; <span class="ruby-ivar">@@bs</span><span class="ruby-operator">==</span><span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-value">4096</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@@bs</span> = <span class="ruby-ivar">@@bs</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@@bs</span><span class="ruby-operator">%</span><span class="ruby-value">16</span> <span class="ruby-keyword kw">end</span>
154
+ </pre>
155
+ </div>
156
+ </div>
157
+ </div>
158
+
159
+ <div id="method-M000017" class="method-detail">
160
+ <a name="M000017"></a>
161
+
162
+ <div class="method-heading">
163
+ <a href="#M000017" class="method-signature">
164
+ <span class="method-name">check_iv</span><span class="method-args">(iv_string)</span>
165
+ </a>
166
+ </div>
167
+
168
+ <div class="method-description">
169
+ <p><a class="source-toggle" href="#"
170
+ onclick="toggleCode('M000017-source');return false;">[Source]</a></p>
171
+ <div class="method-source-code" id="M000017-source">
172
+ <pre>
173
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 61</span>
174
+ 61: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_iv</span>(<span class="ruby-identifier">iv_string</span>)
175
+ 62: <span class="ruby-identifier">k</span> = <span class="ruby-identifier">iv_string</span>.<span class="ruby-identifier">length</span>
176
+ 63: <span class="ruby-identifier">hex</span> = (<span class="ruby-identifier">iv_string</span> <span class="ruby-operator">=~</span> <span class="ruby-node">/[a-f0-9A-F]{#{k}}/</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
177
+ 64: <span class="ruby-identifier">bin</span> = <span class="ruby-operator">!</span> <span class="ruby-identifier">hex</span>
178
+ 65: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">k</span> <span class="ruby-operator">==</span> <span class="ruby-value">32</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">hex</span>
179
+ 66: <span class="ruby-keyword kw">return</span> [<span class="ruby-identifier">iv_string</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)
180
+ 67: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">k</span> <span class="ruby-operator">==</span> <span class="ruby-value">16</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">bin</span>
181
+ 68: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">iv_string</span>
182
+ 69: <span class="ruby-keyword kw">else</span>
183
+ 70: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad IV string&quot;</span>
184
+ 71: <span class="ruby-keyword kw">end</span>
185
+ 72: <span class="ruby-keyword kw">end</span>
186
+ </pre>
187
+ </div>
188
+ </div>
189
+ </div>
190
+
191
+ <div id="method-M000016" class="method-detail">
192
+ <a name="M000016"></a>
193
+
194
+ <div class="method-heading">
195
+ <a href="#M000016" class="method-signature">
196
+ <span class="method-name">check_key</span><span class="method-args">(key_string, kl = 128)</span>
197
+ </a>
198
+ </div>
199
+
200
+ <div class="method-description">
201
+ <p><a class="source-toggle" href="#"
202
+ onclick="toggleCode('M000016-source');return false;">[Source]</a></p>
203
+ <div class="method-source-code" id="M000016-source">
204
+ <pre>
205
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 48</span>
206
+ 48: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_key</span>(<span class="ruby-identifier">key_string</span>, <span class="ruby-identifier">kl</span> = <span class="ruby-value">128</span>)
207
+ 49: <span class="ruby-identifier">kl</span> = <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_kl</span>(<span class="ruby-identifier">kl</span>)
208
+ 50: <span class="ruby-identifier">k</span> = <span class="ruby-identifier">key_string</span>.<span class="ruby-identifier">length</span>
209
+ 51: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad key string or bad key length&quot;</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">k</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">kl</span><span class="ruby-operator">/</span><span class="ruby-value">8</span>) <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">k</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">kl</span><span class="ruby-operator">/</span><span class="ruby-value">4</span>)
210
+ 52: <span class="ruby-identifier">hex</span> = (<span class="ruby-identifier">key_string</span> <span class="ruby-operator">=~</span> <span class="ruby-node">/[a-f0-9A-F]{#{k}}/</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">k</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">kl</span><span class="ruby-operator">/</span><span class="ruby-value">4</span>)
211
+ 53: <span class="ruby-identifier">bin</span> = <span class="ruby-operator">!</span> <span class="ruby-identifier">hex</span>
212
+ 54: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span> (([<span class="ruby-value">32</span>, <span class="ruby-value">48</span>, <span class="ruby-value">64</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">k</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">hex</span>) <span class="ruby-operator">||</span>
213
+ 55: ([<span class="ruby-value">16</span>, <span class="ruby-value">24</span>, <span class="ruby-value">32</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">k</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">bin</span>))
214
+ 56: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad key string&quot;</span>
215
+ 57: <span class="ruby-keyword kw">end</span>
216
+ 58: <span class="ruby-identifier">hex</span> <span class="ruby-value">? </span>[<span class="ruby-identifier">key_string</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;H*&quot;</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">key_string</span>
217
+ 59: <span class="ruby-keyword kw">end</span>
218
+ </pre>
219
+ </div>
220
+ </div>
221
+ </div>
222
+
223
+ <div id="method-M000019" class="method-detail">
224
+ <a name="M000019"></a>
225
+
226
+ <div class="method-heading">
227
+ <a href="#M000019" class="method-signature">
228
+ <span class="method-name">check_kl</span><span class="method-args">(key_length)</span>
229
+ </a>
230
+ </div>
231
+
232
+ <div class="method-description">
233
+ <p><a class="source-toggle" href="#"
234
+ onclick="toggleCode('M000019-source');return false;">[Source]</a></p>
235
+ <div class="method-source-code" id="M000019-source">
236
+ <pre>
237
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 82</span>
238
+ 82: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_kl</span>(<span class="ruby-identifier">key_length</span>)
239
+ 83: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">key_length</span>
240
+ 84: <span class="ruby-keyword kw">when</span> <span class="ruby-value">128</span>, <span class="ruby-value">192</span>, <span class="ruby-value">256</span>
241
+ 85: <span class="ruby-keyword kw">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad key length&quot;</span>
242
+ 86: <span class="ruby-keyword kw">end</span>
243
+ 87: <span class="ruby-identifier">key_length</span>
244
+ 88: <span class="ruby-keyword kw">end</span>
245
+ </pre>
246
+ </div>
247
+ </div>
248
+ </div>
249
+
250
+ <div id="method-M000018" class="method-detail">
251
+ <a name="M000018"></a>
252
+
253
+ <div class="method-heading">
254
+ <a href="#M000018" class="method-signature">
255
+ <span class="method-name">check_mode</span><span class="method-args">(mode)</span>
256
+ </a>
257
+ </div>
258
+
259
+ <div class="method-description">
260
+ <p><a class="source-toggle" href="#"
261
+ onclick="toggleCode('M000018-source');return false;">[Source]</a></p>
262
+ <div class="method-source-code" id="M000018-source">
263
+ <pre>
264
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 74</span>
265
+ 74: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_mode</span> (<span class="ruby-identifier">mode</span>)
266
+ 75: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">mode</span>
267
+ 76: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">'ECB'</span>, <span class="ruby-value str">'CBC'</span>, <span class="ruby-value str">'OFB'</span>, <span class="ruby-value str">'CFB'</span>
268
+ 77: <span class="ruby-keyword kw">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad cipher mode&quot;</span>
269
+ 78: <span class="ruby-keyword kw">end</span>
270
+ 79: <span class="ruby-identifier">mode</span>
271
+ 80: <span class="ruby-keyword kw">end</span>
272
+ </pre>
273
+ </div>
274
+ </div>
275
+ </div>
276
+
277
+ <div id="method-M000022" class="method-detail">
278
+ <a name="M000022"></a>
279
+
280
+ <div class="method-heading">
281
+ <a href="#M000022" class="method-signature">
282
+ <span class="method-name">decrypt_block</span><span class="method-args">(keyl, mode, key, iv, block = &quot;DEFAULT PLAINTXT&quot;)</span>
283
+ </a>
284
+ </div>
285
+
286
+ <div class="method-description">
287
+ <p><a class="source-toggle" href="#"
288
+ onclick="toggleCode('M000022-source');return false;">[Source]</a></p>
289
+ <div class="method-source-code" id="M000022-source">
290
+ <pre>
291
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 106</span>
292
+ 106: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">decrypt_block</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;DEFAULT PLAINTXT&quot;</span>)
293
+ 107: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
294
+ 108: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">decrypt_block</span>(<span class="ruby-identifier">block</span>)
295
+ 109: <span class="ruby-keyword kw">end</span>
296
+ </pre>
297
+ </div>
298
+ </div>
299
+ </div>
300
+
301
+ <div id="method-M000024" class="method-detail">
302
+ <a name="M000024"></a>
303
+
304
+ <div class="method-heading">
305
+ <a href="#M000024" class="method-signature">
306
+ <span class="method-name">decrypt_buffer</span><span class="method-args">(keyl, mode, key, iv, buffer = &quot;DEFAULT PLAINTXT&quot;)</span>
307
+ </a>
308
+ </div>
309
+
310
+ <div class="method-description">
311
+ <p><a class="source-toggle" href="#"
312
+ onclick="toggleCode('M000024-source');return false;">[Source]</a></p>
313
+ <div class="method-source-code" id="M000024-source">
314
+ <pre>
315
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 116</span>
316
+ 116: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">decrypt_buffer</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">buffer</span> = <span class="ruby-value str">&quot;DEFAULT PLAINTXT&quot;</span>)
317
+ 117: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad Block size&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">16</span>
318
+ 118: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
319
+ 119: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">decrypt_buffer</span>(<span class="ruby-identifier">buffer</span>)
320
+ 120: <span class="ruby-keyword kw">end</span>
321
+ </pre>
322
+ </div>
323
+ </div>
324
+ </div>
325
+
326
+ <div id="method-M000026" class="method-detail">
327
+ <a name="M000026"></a>
328
+
329
+ <div class="method-heading">
330
+ <a href="#M000026" class="method-signature">
331
+ <span class="method-name">decrypt_stream</span><span class="method-args">(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)</span>
332
+ </a>
333
+ </div>
334
+
335
+ <div class="method-description">
336
+ <p><a class="source-toggle" href="#"
337
+ onclick="toggleCode('M000026-source');return false;">[Source]</a></p>
338
+ <div class="method-source-code" id="M000026-source">
339
+ <pre>
340
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 142</span>
341
+ 142: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">decrypt_stream</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">sin</span> = <span class="ruby-constant">STDIN</span>, <span class="ruby-identifier">sout</span> = <span class="ruby-constant">STDOUT</span>)
342
+ 143: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
343
+ 144: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">sout</span>
344
+ 145: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">String</span>, <span class="ruby-constant">Array</span>, <span class="ruby-constant">IO</span>
345
+ 146: <span class="ruby-keyword kw">else</span>
346
+ 147: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad output stream (String, Array, IO)&quot;</span>
347
+ 148: <span class="ruby-keyword kw">end</span>
348
+ 149: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">sin</span>
349
+ 150: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">String</span>
350
+ 151: <span class="ruby-identifier">sout</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">decrypt_buffer</span>(<span class="ruby-identifier">sin</span>)
351
+ 152: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">IO</span>
352
+ 153: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">buf</span> = <span class="ruby-identifier">sin</span>.<span class="ruby-identifier">read</span>(<span class="ruby-ivar">@@bs</span>)
353
+ 154: <span class="ruby-identifier">sout</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">sin</span>.<span class="ruby-identifier">eof?</span> <span class="ruby-value">? </span><span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">decrypt_buffer</span>(<span class="ruby-identifier">buf</span>) <span class="ruby-operator">:</span>
354
+ 155: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">decrypt_blocks</span>(<span class="ruby-identifier">buf</span>))
355
+ 156: <span class="ruby-keyword kw">end</span>
356
+ 157: <span class="ruby-keyword kw">else</span>
357
+ 158: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad input stream (String, IO)&quot;</span>
358
+ 159: <span class="ruby-keyword kw">end</span>
359
+ 160: <span class="ruby-keyword kw">end</span>
360
+ </pre>
361
+ </div>
362
+ </div>
363
+ </div>
364
+
365
+ <div id="method-M000021" class="method-detail">
366
+ <a name="M000021"></a>
367
+
368
+ <div class="method-heading">
369
+ <a href="#M000021" class="method-signature">
370
+ <span class="method-name">encrypt_block</span><span class="method-args">(keyl, mode, key, iv, block = &quot;DEFAULT PLAINTXT&quot;)</span>
371
+ </a>
372
+ </div>
373
+
374
+ <div class="method-description">
375
+ <p><a class="source-toggle" href="#"
376
+ onclick="toggleCode('M000021-source');return false;">[Source]</a></p>
377
+ <div class="method-source-code" id="M000021-source">
378
+ <pre>
379
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 100</span>
380
+ 100: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">encrypt_block</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;DEFAULT PLAINTXT&quot;</span>)
381
+ 101: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad Block size&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">16</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">16</span>
382
+ 102: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
383
+ 103: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">encrypt_block</span>(<span class="ruby-identifier">block</span>)
384
+ 104: <span class="ruby-keyword kw">end</span>
385
+ </pre>
386
+ </div>
387
+ </div>
388
+ </div>
389
+
390
+ <div id="method-M000023" class="method-detail">
391
+ <a name="M000023"></a>
392
+
393
+ <div class="method-heading">
394
+ <a href="#M000023" class="method-signature">
395
+ <span class="method-name">encrypt_buffer</span><span class="method-args">(keyl, mode, key, iv, buffer = &quot;PLAINTEXT&quot;)</span>
396
+ </a>
397
+ </div>
398
+
399
+ <div class="method-description">
400
+ <p><a class="source-toggle" href="#"
401
+ onclick="toggleCode('M000023-source');return false;">[Source]</a></p>
402
+ <div class="method-source-code" id="M000023-source">
403
+ <pre>
404
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 111</span>
405
+ 111: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">encrypt_buffer</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">buffer</span> = <span class="ruby-value str">&quot;PLAINTEXT&quot;</span>)
406
+ 112: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
407
+ 113: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">encrypt_buffer</span>(<span class="ruby-identifier">buffer</span>)
408
+ 114: <span class="ruby-keyword kw">end</span>
409
+ </pre>
410
+ </div>
411
+ </div>
412
+ </div>
413
+
414
+ <div id="method-M000025" class="method-detail">
415
+ <a name="M000025"></a>
416
+
417
+ <div class="method-heading">
418
+ <a href="#M000025" class="method-signature">
419
+ <span class="method-name">encrypt_stream</span><span class="method-args">(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)</span>
420
+ </a>
421
+ </div>
422
+
423
+ <div class="method-description">
424
+ <p><a class="source-toggle" href="#"
425
+ onclick="toggleCode('M000025-source');return false;">[Source]</a></p>
426
+ <div class="method-source-code" id="M000025-source">
427
+ <pre>
428
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 122</span>
429
+ 122: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">encrypt_stream</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>, <span class="ruby-identifier">sin</span> = <span class="ruby-constant">STDIN</span>, <span class="ruby-identifier">sout</span> = <span class="ruby-constant">STDOUT</span>)
430
+ 123: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
431
+ 124: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">sout</span>
432
+ 125: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">String</span>, <span class="ruby-constant">Array</span>, <span class="ruby-constant">IO</span>
433
+ 126: <span class="ruby-keyword kw">else</span>
434
+ 127: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad output stream (String, Array, IO)&quot;</span>
435
+ 128: <span class="ruby-keyword kw">end</span>
436
+ 129: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">sin</span>
437
+ 130: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">String</span>
438
+ 131: <span class="ruby-identifier">sout</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">encrypt_buffer</span>(<span class="ruby-identifier">sin</span>)
439
+ 132: <span class="ruby-keyword kw">when</span> <span class="ruby-constant">IO</span>
440
+ 133: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">buf</span> = <span class="ruby-identifier">sin</span>.<span class="ruby-identifier">read</span>(<span class="ruby-ivar">@@bs</span>)
441
+ 134: <span class="ruby-identifier">sout</span> <span class="ruby-operator">&lt;&lt;</span> ((<span class="ruby-identifier">buf</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">%</span> <span class="ruby-value">16</span>).<span class="ruby-identifier">zero?</span> <span class="ruby-value">? </span><span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">encrypt_blocks</span>(<span class="ruby-identifier">buf</span>) <span class="ruby-operator">:</span>
442
+ 135: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">encrypt_buffer</span>(<span class="ruby-identifier">buf</span>))
443
+ 136: <span class="ruby-keyword kw">end</span>
444
+ 137: <span class="ruby-keyword kw">else</span>
445
+ 138: <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad input stream (String, IO)&quot;</span>
446
+ 139: <span class="ruby-keyword kw">end</span>
447
+ 140: <span class="ruby-keyword kw">end</span>
448
+ </pre>
449
+ </div>
450
+ </div>
451
+ </div>
452
+
453
+ <div id="method-M000020" class="method-detail">
454
+ <a name="M000020"></a>
455
+
456
+ <div class="method-heading">
457
+ <a href="#M000020" class="method-signature">
458
+ <span class="method-name">init</span><span class="method-args">(keyl, mode, key, iv)</span>
459
+ </a>
460
+ </div>
461
+
462
+ <div class="method-description">
463
+ <p><a class="source-toggle" href="#"
464
+ onclick="toggleCode('M000020-source');return false;">[Source]</a></p>
465
+ <div class="method-source-code" id="M000020-source">
466
+ <pre>
467
+ <span class="ruby-comment cmt"># File lib/ruby-aes.rb, line 90</span>
468
+ 90: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-identifier">keyl</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">iv</span>)
469
+ 91: <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@@aes</span>
470
+ 92: <span class="ruby-ivar">@@aes</span> = <span class="ruby-constant">AesAlg</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_kl</span>(<span class="ruby-identifier">keyl</span>), <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_mode</span>(<span class="ruby-identifier">mode</span>),
471
+ 93: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_key</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">keyl</span>), <span class="ruby-identifier">iv</span> <span class="ruby-value">? </span><span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_iv</span>(<span class="ruby-identifier">iv</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>)
472
+ 94: <span class="ruby-keyword kw">else</span>
473
+ 95: <span class="ruby-ivar">@@aes</span>.<span class="ruby-identifier">init</span>(<span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_kl</span>(<span class="ruby-identifier">keyl</span>), <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_mode</span>(<span class="ruby-identifier">mode</span>),
474
+ 96: <span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_key</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">keyl</span>), <span class="ruby-identifier">iv</span> <span class="ruby-value">? </span><span class="ruby-constant">Aes</span>.<span class="ruby-identifier">check_iv</span>(<span class="ruby-identifier">iv</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>)
475
+ 97: <span class="ruby-keyword kw">end</span>
476
+ 98: <span class="ruby-keyword kw">end</span>
477
+ </pre>
478
+ </div>
479
+ </div>
480
+ </div>
481
+
482
+
483
+ </div>
484
+
485
+
486
+ </div>
487
+
488
+
489
+ <div id="validator-badges">
490
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
491
+ </div>
492
+
493
+ </body>
494
+ </html>