class Xmlenc::Algorithms::AESCBC

Public Class Methods

[](size) click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 5
def [](size)
  new(size)
end
new(size) click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 10
def initialize(size)
  @size = size
end

Public Instance Methods

decrypt(cipher_value, options = {}) click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 21
def decrypt(cipher_value, options = {})
  cipher.decrypt
  cipher.padding = 0
  cipher.key     = @key
  cipher.iv      = cipher_value[0...iv_len]
  result         = cipher.update(cipher_value[iv_len..-1]) << cipher.final

  padding_size   = result.last.unpack('c').first
  result[0...-padding_size]
end
encrypt(data, options = {}) click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 32
def encrypt(data, options = {})
  cipher.encrypt
  cipher.key = @key
  cipher.iv  = iv
  iv << cipher.update(data) << cipher.final
end
key() click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 39
def key
  @key
end
setup(key = nil) click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 14
def setup(key = nil)
  @cipher= nil
  @iv    = nil
  @key   = key || cipher.random_key
  self
end

Private Instance Methods

cipher() click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 53
def cipher
  @cipher ||= OpenSSL::Cipher.new("aes-#{@size}-cbc")
end
iv() click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 45
def iv
  @iv ||= cipher.random_iv
end
iv_len() click to toggle source
# File lib/xmlenc/algorithms/aes_cbc.rb, line 49
def iv_len
  cipher.iv_len
end