| Module | Aes |
| In: |
lib/ruby-aes.rb
|
# File lib/ruby-aes.rb, line 46
46: def Aes.bs=(bs); @@bs = bs.to_i; @@bs==0 ? 4096 : @@bs = @@bs - @@bs%16 end
# File lib/ruby-aes.rb, line 61
61: def Aes.check_iv(iv_string)
62: k = iv_string.length
63: hex = (iv_string =~ /[a-f0-9A-F]{#{k}}/) == 0
64: bin = ! hex
65: if k == 32 && hex
66: return [iv_string].pack("H*")
67: elsif k == 16 && bin
68: return iv_string
69: else
70: raise "Bad IV string"
71: end
72: end
# File lib/ruby-aes.rb, line 48
48: def Aes.check_key(key_string, kl = 128)
49: kl = Aes.check_kl(kl)
50: k = key_string ? key_string.length : 0
51: raise "Bad key string or bad key length" if (k != kl/8) && (k != kl/4)
52: hex = (key_string =~ /[a-f0-9A-F]{#{k}}/) == 0 && (k == kl/4)
53: bin = ! hex
54: if ! (([32, 48, 64].include?(k) && hex) ||
55: ([16, 24, 32].include?(k) && bin))
56: raise "Bad key string"
57: end
58: hex ? [key_string].pack("H*") : key_string
59: end
# File lib/ruby-aes.rb, line 82
82: def Aes.check_kl(key_length)
83: case key_length
84: when 128, 192, 256
85: else raise "Bad key length"
86: end
87: key_length
88: end
# File lib/ruby-aes.rb, line 74
74: def Aes.check_mode (mode)
75: case mode
76: when 'ECB', 'CBC', 'OFB', 'CFB'
77: else raise "Bad cipher mode"
78: end
79: mode
80: end
# File lib/ruby-aes.rb, line 106
106: def Aes.decrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT")
107: Aes.init(keyl, mode, key, iv)
108: @@aes.decrypt_block(block)
109: end
# File lib/ruby-aes.rb, line 116
116: def Aes.decrypt_buffer(keyl, mode, key, iv, buffer = "DEFAULT PLAINTXT")
117: raise "Bad Block size" if buffer.length < 16
118: Aes.init(keyl, mode, key, iv)
119: @@aes.decrypt_buffer(buffer)
120: end
# File lib/ruby-aes.rb, line 145
145: def Aes.decrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)
146: Aes.init(keyl, mode, key, iv)
147: case sout
148: when String, Array, IO
149: else
150: raise "Bad output stream (String, Array, IO)"
151: end
152: case sin
153: when String
154: sout << @@aes.decrypt_buffer(sin)
155: when IO
156: while buf = sin.read(@@bs)#+1)
157: if buf.length == @@bs
158: sout << @@aes.decrypt_blocks(buf)
159: else
160: sout << @@aes.decrypt_buffer(buf)
161: end
162: end
163: else
164: raise "Bad input stream (String, IO)"
165: end
166: end
# File lib/ruby-aes.rb, line 100
100: def Aes.encrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT")
101: raise "Bad Block size" if block.length < 16 || block.length > 16
102: Aes.init(keyl, mode, key, iv)
103: @@aes.encrypt_block(block)
104: end
# File lib/ruby-aes.rb, line 111
111: def Aes.encrypt_buffer(keyl, mode, key, iv, buffer = "PLAINTEXT")
112: Aes.init(keyl, mode, key, iv)
113: @@aes.encrypt_buffer(buffer)
114: end
# File lib/ruby-aes.rb, line 122
122: def Aes.encrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)
123: Aes.init(keyl, mode, key, iv)
124: case sout
125: when String, Array, IO
126: else
127: raise "Bad output stream (String, Array, IO)"
128: end
129: case sin
130: when String
131: sout << @@aes.encrypt_buffer(sin)
132: when IO
133: while buf = sin.read(@@bs)
134: if buf.length == @@bs
135: sout << @@aes.encrypt_blocks(buf)
136: else
137: sout << @@aes.encrypt_buffer(buf)
138: end
139: end
140: else
141: raise "Bad input stream (String, IO)"
142: end
143: end
# File lib/ruby-aes.rb, line 90
90: def Aes.init(keyl, mode, key, iv)
91: unless @@aes
92: @@aes = AesAlg.new(Aes.check_kl(keyl), Aes.check_mode(mode),
93: Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil)
94: else
95: @@aes.init(Aes.check_kl(keyl), Aes.check_mode(mode),
96: Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil)
97: end
98: end