class AES::AES
Attributes
cipher[R]
cipher_text[R]
iv[R]
key[R]
options[R]
plain_text[R]
Public Class Methods
new(key, opts={})
click to toggle source
# File lib/aes/aes.rb, line 43 def initialize(key, opts={}) merge_options opts @cipher = nil @key = key @iv ||= random_iv self end
Public Instance Methods
decrypt(cipher_text)
click to toggle source
Decrypts
# File lib/aes/aes.rb, line 66 def decrypt(cipher_text) @cipher_text = cipher_text _setup(:decrypt) case @options[:format] when :base_64 ctext = b64_d(@cipher_text) else ctext = @cipher_text end @cipher.iv = ctext[0] @plain_text = @cipher.update(ctext[1]) + @cipher.final end
encrypt(plain_text)
click to toggle source
Encrypts
# File lib/aes/aes.rb, line 52 def encrypt(plain_text) @plain_text = plain_text _setup(:encrypt) @cipher.iv = @iv case @options[:format] when :base_64 @cipher_text = b64_e(@iv) << "$" << b64_e(_encrypt) else @cipher_text = [@iv, _encrypt] end @cipher_text end
random_iv()
click to toggle source
Generate a random initialization vector
# File lib/aes/aes.rb, line 80 def random_iv _setup(:encrypt) @cipher.random_iv end
random_key(length=256)
click to toggle source
Generate a random key
# File lib/aes/aes.rb, line 86 def random_key(length=256) _random_seed.unpack('H*')[0][0..((length/8)-1)] end
Private Instance Methods
_encrypt()
click to toggle source
Encrypts @plain_text
# File lib/aes/aes.rb, line 118 def _encrypt @cipher.update(@plain_text) + @cipher.final end
_handle_iv()
click to toggle source
# File lib/aes/aes.rb, line 134 def _handle_iv @iv = @options[:iv] return if @iv.nil? case @options[:format] when :base_64 @iv = Base64.decode64(@options[:iv]) end end
_handle_padding()
click to toggle source
# File lib/aes/aes.rb, line 144 def _handle_padding # convert value to what OpenSSL module format expects @options[:padding] = @options[:padding] ? 1 : 0 end
_random_seed(size=32)
click to toggle source
Generates a random seed value
# File lib/aes/aes.rb, line 93 def _random_seed(size=32) if defined? OpenSSL::Random return OpenSSL::Random.random_bytes(size) else chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a (1..size).collect{|a| chars[rand(chars.size)] }.join end end
_setup(action)
click to toggle source
Create a new cipher using the cipher type specified
# File lib/aes/aes.rb, line 150 def _setup(action) @cipher ||= OpenSSL::Cipher.new(@options[:cipher]) # Toggles encryption mode @cipher.send(action) @cipher.padding = @options[:padding] @cipher.key = @key.unpack('a2'*32).map{|x| x.hex}.pack('c'*32) end
b64_d(data)
click to toggle source
Un-Base64's the IV and CipherText Returns an array containing the IV, and CipherText
# File lib/aes/aes.rb, line 104 def b64_d(data) iv_and_ctext = [] data.split('$').each do |part| iv_and_ctext << Base64.decode64(part) end iv_and_ctext end
b64_e(data)
click to toggle source
Base64 Encodes a string
# File lib/aes/aes.rb, line 113 def b64_e(data) Base64.encode64(data).chomp end
merge_options(opts)
click to toggle source
Merge init options with defaults
# File lib/aes/aes.rb, line 123 def merge_options(opts) @options = { :format => :base_64, :cipher => "AES-256-CBC", :iv => nil, :padding => true, # use cipher padding by default }.merge! opts _handle_iv _handle_padding end