Class: Xmlenc::Algorithms::AESCBC

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlenc/algorithms/aes_cbc.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ AESCBC

Returns a new instance of AESCBC.


10
11
12
# File 'lib/xmlenc/algorithms/aes_cbc.rb', line 10

def initialize(size)
  @size = size
end

Class Method Details

.[](size) ⇒ Object


5
6
7
# File 'lib/xmlenc/algorithms/aes_cbc.rb', line 5

def [](size)
  new(size)
end

Instance Method Details

#decrypt(cipher_value, options = {}) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# 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 = {}) ⇒ Object


32
33
34
35
36
37
# 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

#keyObject


39
40
41
# File 'lib/xmlenc/algorithms/aes_cbc.rb', line 39

def key
  @key
end

#setup(key = nil) ⇒ Object


14
15
16
17
18
19
# File 'lib/xmlenc/algorithms/aes_cbc.rb', line 14

def setup(key = nil)
  @cipher= nil
  @iv    = nil
  @key   = key || cipher.random_key
  self
end